Tomas Winkler | 9fff042 | 2019-03-12 00:10:41 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 2 | /* |
Tomas Winkler | 1e55b60 | 2019-03-12 00:10:44 +0200 | [diff] [blame] | 3 | * Copyright (c) 2003-2018, Intel Corporation. All rights reserved. |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 4 | * Intel Management Engine Interface (Intel MEI) Linux driver |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 5 | */ |
| 6 | |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame] | 7 | #include <linux/export.h> |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 8 | #include <linux/kthread.h> |
| 9 | #include <linux/interrupt.h> |
| 10 | #include <linux/fs.h> |
| 11 | #include <linux/jiffies.h> |
Tomas Winkler | 1f18035 | 2014-09-29 16:31:46 +0300 | [diff] [blame] | 12 | #include <linux/slab.h> |
Alexander Usyskin | 34af191 | 2015-10-13 15:02:38 +0300 | [diff] [blame] | 13 | #include <linux/pm_runtime.h> |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 14 | |
Tomas Winkler | 4f3afe1 | 2012-05-09 16:38:59 +0300 | [diff] [blame] | 15 | #include <linux/mei.h> |
Tomas Winkler | 47a7380 | 2012-12-25 19:06:03 +0200 | [diff] [blame] | 16 | |
| 17 | #include "mei_dev.h" |
Tomas Winkler | 0edb23f | 2013-01-08 23:07:12 +0200 | [diff] [blame] | 18 | #include "hbm.h" |
Tomas Winkler | 90e0b5f | 2013-01-08 23:07:14 +0200 | [diff] [blame] | 19 | #include "client.h" |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 20 | |
| 21 | |
| 22 | /** |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 23 | * mei_irq_compl_handler - dispatch complete handlers |
Tomas Winkler | 4c6e22b | 2013-03-17 11:41:20 +0200 | [diff] [blame] | 24 | * for the completed callbacks |
| 25 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 26 | * @dev: mei device |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 27 | * @cmpl_list: list of completed cbs |
Tomas Winkler | 4c6e22b | 2013-03-17 11:41:20 +0200 | [diff] [blame] | 28 | */ |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 29 | void mei_irq_compl_handler(struct mei_device *dev, struct list_head *cmpl_list) |
Tomas Winkler | 4c6e22b | 2013-03-17 11:41:20 +0200 | [diff] [blame] | 30 | { |
| 31 | struct mei_cl_cb *cb, *next; |
| 32 | struct mei_cl *cl; |
| 33 | |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 34 | list_for_each_entry_safe(cb, next, cmpl_list, list) { |
Tomas Winkler | 4c6e22b | 2013-03-17 11:41:20 +0200 | [diff] [blame] | 35 | cl = cb->cl; |
Tomas Winkler | c54bf3a | 2015-02-10 10:39:39 +0200 | [diff] [blame] | 36 | list_del_init(&cb->list); |
Tomas Winkler | 4c6e22b | 2013-03-17 11:41:20 +0200 | [diff] [blame] | 37 | |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 38 | dev_dbg(dev->dev, "completing call back.\n"); |
Alexander Usyskin | 394a77d | 2017-03-20 15:04:03 +0200 | [diff] [blame] | 39 | mei_cl_complete(cl, cb); |
Tomas Winkler | 4c6e22b | 2013-03-17 11:41:20 +0200 | [diff] [blame] | 40 | } |
| 41 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame] | 42 | EXPORT_SYMBOL_GPL(mei_irq_compl_handler); |
Tomas Winkler | 6e0f180 | 2013-04-19 22:01:34 +0300 | [diff] [blame] | 43 | |
Tomas Winkler | 4c6e22b | 2013-03-17 11:41:20 +0200 | [diff] [blame] | 44 | /** |
Tomas Winkler | 6e0f180 | 2013-04-19 22:01:34 +0300 | [diff] [blame] | 45 | * mei_cl_hbm_equal - check if hbm is addressed to the client |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 46 | * |
Tomas Winkler | 6e0f180 | 2013-04-19 22:01:34 +0300 | [diff] [blame] | 47 | * @cl: host client |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 48 | * @mei_hdr: header of mei client message |
| 49 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 50 | * Return: true if matches, false otherwise |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 51 | */ |
Tomas Winkler | 6e0f180 | 2013-04-19 22:01:34 +0300 | [diff] [blame] | 52 | static inline int mei_cl_hbm_equal(struct mei_cl *cl, |
| 53 | struct mei_msg_hdr *mei_hdr) |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 54 | { |
Alexander Usyskin | 1df629e | 2015-05-04 09:43:56 +0300 | [diff] [blame] | 55 | return mei_cl_host_addr(cl) == mei_hdr->host_addr && |
Alexander Usyskin | d49ed64 | 2015-05-04 09:43:54 +0300 | [diff] [blame] | 56 | mei_cl_me_id(cl) == mei_hdr->me_addr; |
Tomas Winkler | 6e0f180 | 2013-04-19 22:01:34 +0300 | [diff] [blame] | 57 | } |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 58 | |
| 59 | /** |
Tomas Winkler | 331e418 | 2015-02-10 10:39:41 +0200 | [diff] [blame] | 60 | * mei_irq_discard_msg - discard received message |
| 61 | * |
| 62 | * @dev: mei device |
| 63 | * @hdr: message header |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 64 | * @discard_len: the length of the message to discard (excluding header) |
Tomas Winkler | 331e418 | 2015-02-10 10:39:41 +0200 | [diff] [blame] | 65 | */ |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 66 | static void mei_irq_discard_msg(struct mei_device *dev, struct mei_msg_hdr *hdr, |
| 67 | size_t discard_len) |
Tomas Winkler | 331e418 | 2015-02-10 10:39:41 +0200 | [diff] [blame] | 68 | { |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 69 | if (hdr->dma_ring) { |
| 70 | mei_dma_ring_read(dev, NULL, |
| 71 | hdr->extension[dev->rd_msg_hdr_count - 2]); |
| 72 | discard_len = 0; |
| 73 | } |
Tomas Winkler | 331e418 | 2015-02-10 10:39:41 +0200 | [diff] [blame] | 74 | /* |
Tomas Winkler | ae4cb6b | 2023-10-11 10:43:01 +0300 | [diff] [blame] | 75 | * no need to check for size as it is guaranteed |
Tomas Winkler | 331e418 | 2015-02-10 10:39:41 +0200 | [diff] [blame] | 76 | * that length fits into rd_msg_buf |
| 77 | */ |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 78 | mei_read_slots(dev, dev->rd_msg_buf, discard_len); |
Tomas Winkler | 331e418 | 2015-02-10 10:39:41 +0200 | [diff] [blame] | 79 | dev_dbg(dev->dev, "discarding message " MEI_HDR_FMT "\n", |
| 80 | MEI_HDR_PRM(hdr)); |
| 81 | } |
| 82 | |
| 83 | /** |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 84 | * mei_cl_irq_read_msg - process client message |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 85 | * |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 86 | * @cl: reading client |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 87 | * @mei_hdr: header of mei client message |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 88 | * @meta: extend meta header |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 89 | * @cmpl_list: completion list |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 90 | * |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 91 | * Return: always 0 |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 92 | */ |
Alexander Usyskin | 394a77d | 2017-03-20 15:04:03 +0200 | [diff] [blame] | 93 | static int mei_cl_irq_read_msg(struct mei_cl *cl, |
| 94 | struct mei_msg_hdr *mei_hdr, |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 95 | struct mei_ext_meta_hdr *meta, |
Alexander Usyskin | 394a77d | 2017-03-20 15:04:03 +0200 | [diff] [blame] | 96 | struct list_head *cmpl_list) |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 97 | { |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 98 | struct mei_device *dev = cl->dev; |
| 99 | struct mei_cl_cb *cb; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 100 | |
Tomas Winkler | 4ed1cc9 | 2022-09-27 17:41:31 -0700 | [diff] [blame] | 101 | struct mei_ext_hdr_vtag *vtag_hdr = NULL; |
| 102 | struct mei_ext_hdr_gsc_f2h *gsc_f2h = NULL; |
| 103 | |
Tomas Winkler | f862b6b | 2016-02-07 23:35:19 +0200 | [diff] [blame] | 104 | size_t buf_sz; |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 105 | u32 length; |
Tomas Winkler | 4ed1cc9 | 2022-09-27 17:41:31 -0700 | [diff] [blame] | 106 | u32 ext_len; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 107 | |
| 108 | length = mei_hdr->length; |
| 109 | ext_len = 0; |
| 110 | if (mei_hdr->extended) { |
| 111 | ext_len = sizeof(*meta) + mei_slots2data(meta->size); |
| 112 | length -= ext_len; |
| 113 | } |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 114 | |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 115 | cb = list_first_entry_or_null(&cl->rd_pending, struct mei_cl_cb, list); |
| 116 | if (!cb) { |
Alexander Usyskin | a808c80 | 2016-06-16 17:58:58 +0300 | [diff] [blame] | 117 | if (!mei_cl_is_fixed_address(cl)) { |
| 118 | cl_err(dev, cl, "pending read cb not found\n"); |
Alexander Usyskin | dfe5f75 | 2016-07-26 01:06:01 +0300 | [diff] [blame] | 119 | goto discard; |
Alexander Usyskin | a808c80 | 2016-06-16 17:58:58 +0300 | [diff] [blame] | 120 | } |
| 121 | cb = mei_cl_alloc_cb(cl, mei_cl_mtu(cl), MEI_FOP_READ, cl->fp); |
| 122 | if (!cb) |
Alexander Usyskin | dfe5f75 | 2016-07-26 01:06:01 +0300 | [diff] [blame] | 123 | goto discard; |
Alexander Usyskin | a808c80 | 2016-06-16 17:58:58 +0300 | [diff] [blame] | 124 | list_add_tail(&cb->list, &cl->rd_pending); |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 125 | } |
| 126 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 127 | if (mei_hdr->extended) { |
Tomas Winkler | 4ed1cc9 | 2022-09-27 17:41:31 -0700 | [diff] [blame] | 128 | struct mei_ext_hdr *ext = mei_ext_begin(meta); |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 129 | do { |
| 130 | switch (ext->type) { |
| 131 | case MEI_EXT_HDR_VTAG: |
Tomas Winkler | 4029238 | 2021-06-21 22:37:56 +0300 | [diff] [blame] | 132 | vtag_hdr = (struct mei_ext_hdr_vtag *)ext; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 133 | break; |
Tomas Winkler | 4ed1cc9 | 2022-09-27 17:41:31 -0700 | [diff] [blame] | 134 | case MEI_EXT_HDR_GSC: |
| 135 | gsc_f2h = (struct mei_ext_hdr_gsc_f2h *)ext; |
| 136 | cb->ext_hdr = kzalloc(sizeof(*gsc_f2h), GFP_KERNEL); |
| 137 | if (!cb->ext_hdr) { |
| 138 | cb->status = -ENOMEM; |
| 139 | goto discard; |
| 140 | } |
| 141 | break; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 142 | case MEI_EXT_HDR_NONE: |
| 143 | fallthrough; |
| 144 | default: |
Tomas Winkler | 4ed1cc9 | 2022-09-27 17:41:31 -0700 | [diff] [blame] | 145 | cl_err(dev, cl, "unknown extended header\n"); |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 146 | cb->status = -EPROTO; |
| 147 | break; |
| 148 | } |
| 149 | |
| 150 | ext = mei_ext_next(ext); |
| 151 | } while (!mei_ext_last(meta, ext)); |
| 152 | |
Tomas Winkler | 4ed1cc9 | 2022-09-27 17:41:31 -0700 | [diff] [blame] | 153 | if (!vtag_hdr && !gsc_f2h) { |
| 154 | cl_dbg(dev, cl, "no vtag or gsc found in extended header.\n"); |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 155 | cb->status = -EPROTO; |
| 156 | goto discard; |
| 157 | } |
Tomas Winkler | 4ed1cc9 | 2022-09-27 17:41:31 -0700 | [diff] [blame] | 158 | } |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 159 | |
Tomas Winkler | 4ed1cc9 | 2022-09-27 17:41:31 -0700 | [diff] [blame] | 160 | if (vtag_hdr) { |
Tomas Winkler | 4029238 | 2021-06-21 22:37:56 +0300 | [diff] [blame] | 161 | cl_dbg(dev, cl, "vtag: %d\n", vtag_hdr->vtag); |
| 162 | if (cb->vtag && cb->vtag != vtag_hdr->vtag) { |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 163 | cl_err(dev, cl, "mismatched tag: %d != %d\n", |
Tomas Winkler | 4029238 | 2021-06-21 22:37:56 +0300 | [diff] [blame] | 164 | cb->vtag, vtag_hdr->vtag); |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 165 | cb->status = -EPROTO; |
| 166 | goto discard; |
| 167 | } |
Tomas Winkler | 4029238 | 2021-06-21 22:37:56 +0300 | [diff] [blame] | 168 | cb->vtag = vtag_hdr->vtag; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 169 | } |
| 170 | |
Tomas Winkler | 4ed1cc9 | 2022-09-27 17:41:31 -0700 | [diff] [blame] | 171 | if (gsc_f2h) { |
| 172 | u32 ext_hdr_len = mei_ext_hdr_len(&gsc_f2h->hdr); |
| 173 | |
| 174 | if (!dev->hbm_f_gsc_supported) { |
| 175 | cl_err(dev, cl, "gsc extended header is not supported\n"); |
| 176 | cb->status = -EPROTO; |
| 177 | goto discard; |
| 178 | } |
| 179 | |
| 180 | if (length) { |
| 181 | cl_err(dev, cl, "no data allowed in cb with gsc\n"); |
| 182 | cb->status = -EPROTO; |
| 183 | goto discard; |
| 184 | } |
| 185 | if (ext_hdr_len > sizeof(*gsc_f2h)) { |
| 186 | cl_err(dev, cl, "gsc extended header is too big %u\n", ext_hdr_len); |
| 187 | cb->status = -EPROTO; |
| 188 | goto discard; |
| 189 | } |
| 190 | memcpy(cb->ext_hdr, gsc_f2h, ext_hdr_len); |
| 191 | } |
| 192 | |
Tomas Winkler | f3de9b6 | 2015-03-27 00:27:58 +0200 | [diff] [blame] | 193 | if (!mei_cl_is_connected(cl)) { |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 194 | cl_dbg(dev, cl, "not connected\n"); |
| 195 | cb->status = -ENODEV; |
Alexander Usyskin | dfe5f75 | 2016-07-26 01:06:01 +0300 | [diff] [blame] | 196 | goto discard; |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 197 | } |
| 198 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 199 | if (mei_hdr->dma_ring) |
| 200 | length = mei_hdr->extension[mei_data2slots(ext_len)]; |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 201 | |
| 202 | buf_sz = length + cb->buf_idx; |
Tomas Winkler | f862b6b | 2016-02-07 23:35:19 +0200 | [diff] [blame] | 203 | /* catch for integer overflow */ |
| 204 | if (buf_sz < cb->buf_idx) { |
Alexander Usyskin | 35bf769 | 2016-02-17 18:27:34 +0200 | [diff] [blame] | 205 | cl_err(dev, cl, "message is too big len %d idx %zu\n", |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 206 | length, cb->buf_idx); |
Tomas Winkler | f862b6b | 2016-02-07 23:35:19 +0200 | [diff] [blame] | 207 | cb->status = -EMSGSIZE; |
Alexander Usyskin | dfe5f75 | 2016-07-26 01:06:01 +0300 | [diff] [blame] | 208 | goto discard; |
Tomas Winkler | f862b6b | 2016-02-07 23:35:19 +0200 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | if (cb->buf.size < buf_sz) { |
Alexander Usyskin | 35bf769 | 2016-02-17 18:27:34 +0200 | [diff] [blame] | 212 | cl_dbg(dev, cl, "message overflow. size %zu len %d idx %zu\n", |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 213 | cb->buf.size, length, cb->buf_idx); |
Alexander Usyskin | dfe5f75 | 2016-07-26 01:06:01 +0300 | [diff] [blame] | 214 | cb->status = -EMSGSIZE; |
| 215 | goto discard; |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 216 | } |
| 217 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 218 | if (mei_hdr->dma_ring) { |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 219 | mei_dma_ring_read(dev, cb->buf.data + cb->buf_idx, length); |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 220 | /* for DMA read 0 length to generate interrupt to the device */ |
| 221 | mei_read_slots(dev, cb->buf.data + cb->buf_idx, 0); |
| 222 | } else { |
| 223 | mei_read_slots(dev, cb->buf.data + cb->buf_idx, length); |
| 224 | } |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 225 | |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 226 | cb->buf_idx += length; |
Tomas Winkler | 331e418 | 2015-02-10 10:39:41 +0200 | [diff] [blame] | 227 | |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 228 | if (mei_hdr->msg_complete) { |
Alexander Usyskin | 35bf769 | 2016-02-17 18:27:34 +0200 | [diff] [blame] | 229 | cl_dbg(dev, cl, "completed read length = %zu\n", cb->buf_idx); |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 230 | list_move_tail(&cb->list, cmpl_list); |
Alexander Usyskin | 34af191 | 2015-10-13 15:02:38 +0300 | [diff] [blame] | 231 | } else { |
| 232 | pm_runtime_mark_last_busy(dev->dev); |
| 233 | pm_request_autosuspend(dev->dev); |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 234 | } |
| 235 | |
Alexander Usyskin | dfe5f75 | 2016-07-26 01:06:01 +0300 | [diff] [blame] | 236 | return 0; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 237 | |
Alexander Usyskin | dfe5f75 | 2016-07-26 01:06:01 +0300 | [diff] [blame] | 238 | discard: |
Tomas Winkler | 17ba8a0 | 2016-11-11 03:00:07 +0200 | [diff] [blame] | 239 | if (cb) |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 240 | list_move_tail(&cb->list, cmpl_list); |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 241 | mei_irq_discard_msg(dev, mei_hdr, length); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 242 | return 0; |
| 243 | } |
| 244 | |
| 245 | /** |
Tomas Winkler | 6bb948c | 2014-02-12 21:41:52 +0200 | [diff] [blame] | 246 | * mei_cl_irq_disconnect_rsp - send disconnection response message |
| 247 | * |
| 248 | * @cl: client |
| 249 | * @cb: callback block. |
Tomas Winkler | 6bb948c | 2014-02-12 21:41:52 +0200 | [diff] [blame] | 250 | * @cmpl_list: complete list. |
| 251 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 252 | * Return: 0, OK; otherwise, error. |
Tomas Winkler | 6bb948c | 2014-02-12 21:41:52 +0200 | [diff] [blame] | 253 | */ |
| 254 | static int mei_cl_irq_disconnect_rsp(struct mei_cl *cl, struct mei_cl_cb *cb, |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 255 | struct list_head *cmpl_list) |
Tomas Winkler | 6bb948c | 2014-02-12 21:41:52 +0200 | [diff] [blame] | 256 | { |
| 257 | struct mei_device *dev = cl->dev; |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 258 | u32 msg_slots; |
| 259 | int slots; |
Tomas Winkler | 6bb948c | 2014-02-12 21:41:52 +0200 | [diff] [blame] | 260 | int ret; |
| 261 | |
Tomas Winkler | 98e7086 | 2018-07-31 09:35:33 +0300 | [diff] [blame] | 262 | msg_slots = mei_hbm2slots(sizeof(struct hbm_client_connect_response)); |
Tomas Winkler | de87743 | 2018-07-12 17:10:08 +0300 | [diff] [blame] | 263 | slots = mei_hbuf_empty_slots(dev); |
| 264 | if (slots < 0) |
| 265 | return -EOVERFLOW; |
Tomas Winkler | 6bb948c | 2014-02-12 21:41:52 +0200 | [diff] [blame] | 266 | |
Tomas Winkler | de87743 | 2018-07-12 17:10:08 +0300 | [diff] [blame] | 267 | if ((u32)slots < msg_slots) |
Tomas Winkler | 6bb948c | 2014-02-12 21:41:52 +0200 | [diff] [blame] | 268 | return -EMSGSIZE; |
| 269 | |
Tomas Winkler | 6bb948c | 2014-02-12 21:41:52 +0200 | [diff] [blame] | 270 | ret = mei_hbm_cl_disconnect_rsp(dev, cl); |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 271 | list_move_tail(&cb->list, cmpl_list); |
Tomas Winkler | 6bb948c | 2014-02-12 21:41:52 +0200 | [diff] [blame] | 272 | |
| 273 | return ret; |
| 274 | } |
| 275 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 276 | /** |
Alexander Usyskin | ce23139 | 2014-09-29 16:31:50 +0300 | [diff] [blame] | 277 | * mei_cl_irq_read - processes client read related operation from the |
Tomas Winkler | 6220d6a | 2013-05-12 15:34:46 +0300 | [diff] [blame] | 278 | * interrupt thread context - request for flow control credits |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 279 | * |
Tomas Winkler | 6220d6a | 2013-05-12 15:34:46 +0300 | [diff] [blame] | 280 | * @cl: client |
| 281 | * @cb: callback block. |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 282 | * @cmpl_list: complete list. |
| 283 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 284 | * Return: 0, OK; otherwise, error. |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 285 | */ |
Tomas Winkler | 6220d6a | 2013-05-12 15:34:46 +0300 | [diff] [blame] | 286 | static int mei_cl_irq_read(struct mei_cl *cl, struct mei_cl_cb *cb, |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 287 | struct list_head *cmpl_list) |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 288 | { |
Tomas Winkler | 6220d6a | 2013-05-12 15:34:46 +0300 | [diff] [blame] | 289 | struct mei_device *dev = cl->dev; |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 290 | u32 msg_slots; |
| 291 | int slots; |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 292 | int ret; |
| 293 | |
Alexander Usyskin | ff1586a | 2016-07-26 01:06:06 +0300 | [diff] [blame] | 294 | if (!list_empty(&cl->rd_pending)) |
| 295 | return 0; |
| 296 | |
Tomas Winkler | 98e7086 | 2018-07-31 09:35:33 +0300 | [diff] [blame] | 297 | msg_slots = mei_hbm2slots(sizeof(struct hbm_flow_control)); |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 298 | slots = mei_hbuf_empty_slots(dev); |
Tomas Winkler | de87743 | 2018-07-12 17:10:08 +0300 | [diff] [blame] | 299 | if (slots < 0) |
| 300 | return -EOVERFLOW; |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 301 | |
Tomas Winkler | de87743 | 2018-07-12 17:10:08 +0300 | [diff] [blame] | 302 | if ((u32)slots < msg_slots) |
Tomas Winkler | c8c8d08 | 2013-03-11 18:27:02 +0200 | [diff] [blame] | 303 | return -EMSGSIZE; |
Tomas Winkler | 7bdf72d | 2012-07-04 19:24:52 +0300 | [diff] [blame] | 304 | |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 305 | ret = mei_hbm_cl_flow_control_req(dev, cl); |
| 306 | if (ret) { |
| 307 | cl->status = ret; |
Tomas Winkler | 6220d6a | 2013-05-12 15:34:46 +0300 | [diff] [blame] | 308 | cb->buf_idx = 0; |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 309 | list_move_tail(&cb->list, cmpl_list); |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 310 | return ret; |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 311 | } |
Tomas Winkler | 2ebf8c9 | 2013-09-16 23:44:43 +0300 | [diff] [blame] | 312 | |
Alexander Usyskin | bbf0a94 | 2021-05-26 22:33:34 +0300 | [diff] [blame] | 313 | pm_runtime_mark_last_busy(dev->dev); |
| 314 | pm_request_autosuspend(dev->dev); |
| 315 | |
Tomas Winkler | a9bed61 | 2015-02-10 10:39:46 +0200 | [diff] [blame] | 316 | list_move_tail(&cb->list, &cl->rd_pending); |
Tomas Winkler | 1ccb7b6 | 2012-03-14 14:39:42 +0200 | [diff] [blame] | 317 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 318 | return 0; |
| 319 | } |
| 320 | |
Alexander Usyskin | 603c53e | 2016-02-07 23:35:36 +0200 | [diff] [blame] | 321 | static inline bool hdr_is_hbm(struct mei_msg_hdr *mei_hdr) |
| 322 | { |
| 323 | return mei_hdr->host_addr == 0 && mei_hdr->me_addr == 0; |
| 324 | } |
| 325 | |
| 326 | static inline bool hdr_is_fixed(struct mei_msg_hdr *mei_hdr) |
| 327 | { |
| 328 | return mei_hdr->host_addr == 0 && mei_hdr->me_addr != 0; |
| 329 | } |
| 330 | |
Tomas Winkler | 99c0686 | 2017-06-12 12:15:55 +0300 | [diff] [blame] | 331 | static inline int hdr_is_valid(u32 msg_hdr) |
| 332 | { |
| 333 | struct mei_msg_hdr *mei_hdr; |
Alexander Usyskin | 1309ecc | 2021-01-29 14:07:46 +0200 | [diff] [blame] | 334 | u32 expected_len = 0; |
Tomas Winkler | 99c0686 | 2017-06-12 12:15:55 +0300 | [diff] [blame] | 335 | |
| 336 | mei_hdr = (struct mei_msg_hdr *)&msg_hdr; |
| 337 | if (!msg_hdr || mei_hdr->reserved) |
| 338 | return -EBADMSG; |
| 339 | |
Alexander Usyskin | 1309ecc | 2021-01-29 14:07:46 +0200 | [diff] [blame] | 340 | if (mei_hdr->dma_ring) |
| 341 | expected_len += MEI_SLOT_SIZE; |
| 342 | if (mei_hdr->extended) |
| 343 | expected_len += MEI_SLOT_SIZE; |
| 344 | if (mei_hdr->length < expected_len) |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 345 | return -EBADMSG; |
| 346 | |
Tomas Winkler | 99c0686 | 2017-06-12 12:15:55 +0300 | [diff] [blame] | 347 | return 0; |
| 348 | } |
| 349 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 350 | /** |
Masanari Iida | 393b148 | 2013-04-05 01:05:05 +0900 | [diff] [blame] | 351 | * mei_irq_read_handler - bottom half read routine after ISR to |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 352 | * handle the read processing. |
| 353 | * |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 354 | * @dev: the device structure |
Tomas Winkler | 06ecd64 | 2013-02-06 14:06:42 +0200 | [diff] [blame] | 355 | * @cmpl_list: An instance of our list structure |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 356 | * @slots: slots to read. |
| 357 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 358 | * Return: 0 on success, <0 on failure. |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 359 | */ |
Tomas Winkler | 06ecd64 | 2013-02-06 14:06:42 +0200 | [diff] [blame] | 360 | int mei_irq_read_handler(struct mei_device *dev, |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 361 | struct list_head *cmpl_list, s32 *slots) |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 362 | { |
| 363 | struct mei_msg_hdr *mei_hdr; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 364 | struct mei_ext_meta_hdr *meta_hdr = NULL; |
Tomas Winkler | 10ee907 | 2013-11-11 13:26:08 +0200 | [diff] [blame] | 365 | struct mei_cl *cl; |
| 366 | int ret; |
Alexander Usyskin | 1309ecc | 2021-01-29 14:07:46 +0200 | [diff] [blame] | 367 | u32 hdr_size_left; |
| 368 | u32 hdr_size_ext; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 369 | int i; |
| 370 | int ext_hdr_end; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 371 | |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 372 | if (!dev->rd_msg_hdr[0]) { |
| 373 | dev->rd_msg_hdr[0] = mei_read_hdr(dev); |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 374 | dev->rd_msg_hdr_count = 1; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 375 | (*slots)--; |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 376 | dev_dbg(dev->dev, "slots =%08x.\n", *slots); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 377 | |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 378 | ret = hdr_is_valid(dev->rd_msg_hdr[0]); |
Tomas Winkler | 99c0686 | 2017-06-12 12:15:55 +0300 | [diff] [blame] | 379 | if (ret) { |
| 380 | dev_err(dev->dev, "corrupted message header 0x%08X\n", |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 381 | dev->rd_msg_hdr[0]); |
Tomas Winkler | 99c0686 | 2017-06-12 12:15:55 +0300 | [diff] [blame] | 382 | goto end; |
| 383 | } |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 384 | } |
| 385 | |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 386 | mei_hdr = (struct mei_msg_hdr *)dev->rd_msg_hdr; |
Tomas Winkler | 99c0686 | 2017-06-12 12:15:55 +0300 | [diff] [blame] | 387 | dev_dbg(dev->dev, MEI_HDR_FMT, MEI_HDR_PRM(mei_hdr)); |
| 388 | |
Tomas Winkler | 10ee907 | 2013-11-11 13:26:08 +0200 | [diff] [blame] | 389 | if (mei_slots2data(*slots) < mei_hdr->length) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 390 | dev_err(dev->dev, "less data available than length=%08x.\n", |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 391 | *slots); |
| 392 | /* we can't read the message */ |
Tomas Winkler | b1b94b5 | 2014-03-03 00:21:28 +0200 | [diff] [blame] | 393 | ret = -ENODATA; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 394 | goto end; |
| 395 | } |
| 396 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 397 | ext_hdr_end = 1; |
Alexander Usyskin | 1309ecc | 2021-01-29 14:07:46 +0200 | [diff] [blame] | 398 | hdr_size_left = mei_hdr->length; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 399 | |
| 400 | if (mei_hdr->extended) { |
| 401 | if (!dev->rd_msg_hdr[1]) { |
Tomas Winkler | 4029238 | 2021-06-21 22:37:56 +0300 | [diff] [blame] | 402 | dev->rd_msg_hdr[1] = mei_read_hdr(dev); |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 403 | dev->rd_msg_hdr_count++; |
| 404 | (*slots)--; |
Tomas Winkler | 4029238 | 2021-06-21 22:37:56 +0300 | [diff] [blame] | 405 | dev_dbg(dev->dev, "extended header is %08x\n", dev->rd_msg_hdr[1]); |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 406 | } |
Tomas Winkler | 4029238 | 2021-06-21 22:37:56 +0300 | [diff] [blame] | 407 | meta_hdr = ((struct mei_ext_meta_hdr *)&dev->rd_msg_hdr[1]); |
Alexander Usyskin | 1309ecc | 2021-01-29 14:07:46 +0200 | [diff] [blame] | 408 | if (check_add_overflow((u32)sizeof(*meta_hdr), |
| 409 | mei_slots2data(meta_hdr->size), |
| 410 | &hdr_size_ext)) { |
| 411 | dev_err(dev->dev, "extended message size too big %d\n", |
| 412 | meta_hdr->size); |
| 413 | return -EBADMSG; |
| 414 | } |
| 415 | if (hdr_size_left < hdr_size_ext) { |
| 416 | dev_err(dev->dev, "corrupted message header len %d\n", |
| 417 | mei_hdr->length); |
| 418 | return -EBADMSG; |
| 419 | } |
| 420 | hdr_size_left -= hdr_size_ext; |
| 421 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 422 | ext_hdr_end = meta_hdr->size + 2; |
| 423 | for (i = dev->rd_msg_hdr_count; i < ext_hdr_end; i++) { |
| 424 | dev->rd_msg_hdr[i] = mei_read_hdr(dev); |
| 425 | dev_dbg(dev->dev, "extended header %d is %08x\n", i, |
| 426 | dev->rd_msg_hdr[i]); |
| 427 | dev->rd_msg_hdr_count++; |
| 428 | (*slots)--; |
| 429 | } |
| 430 | } |
| 431 | |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 432 | if (mei_hdr->dma_ring) { |
Alexander Usyskin | 1309ecc | 2021-01-29 14:07:46 +0200 | [diff] [blame] | 433 | if (hdr_size_left != sizeof(dev->rd_msg_hdr[ext_hdr_end])) { |
| 434 | dev_err(dev->dev, "corrupted message header len %d\n", |
| 435 | mei_hdr->length); |
| 436 | return -EBADMSG; |
| 437 | } |
| 438 | |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 439 | dev->rd_msg_hdr[ext_hdr_end] = mei_read_hdr(dev); |
| 440 | dev->rd_msg_hdr_count++; |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 441 | (*slots)--; |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 442 | mei_hdr->length -= sizeof(dev->rd_msg_hdr[ext_hdr_end]); |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 443 | } |
| 444 | |
Tomas Winkler | 10ee907 | 2013-11-11 13:26:08 +0200 | [diff] [blame] | 445 | /* HBM message */ |
Alexander Usyskin | 603c53e | 2016-02-07 23:35:36 +0200 | [diff] [blame] | 446 | if (hdr_is_hbm(mei_hdr)) { |
Tomas Winkler | 544f946 | 2014-01-08 20:19:21 +0200 | [diff] [blame] | 447 | ret = mei_hbm_dispatch(dev, mei_hdr); |
| 448 | if (ret) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 449 | dev_dbg(dev->dev, "mei_hbm_dispatch failed ret = %d\n", |
Tomas Winkler | 544f946 | 2014-01-08 20:19:21 +0200 | [diff] [blame] | 450 | ret); |
| 451 | goto end; |
| 452 | } |
Tomas Winkler | 10ee907 | 2013-11-11 13:26:08 +0200 | [diff] [blame] | 453 | goto reset_slots; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 454 | } |
| 455 | |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 456 | /* find recipient cl */ |
Tomas Winkler | 10ee907 | 2013-11-11 13:26:08 +0200 | [diff] [blame] | 457 | list_for_each_entry(cl, &dev->file_list, link) { |
| 458 | if (mei_cl_hbm_equal(cl, mei_hdr)) { |
| 459 | cl_dbg(dev, cl, "got a message\n"); |
Alexander Usyskin | c10187b | 2022-03-08 11:59:26 +0200 | [diff] [blame] | 460 | ret = mei_cl_irq_read_msg(cl, mei_hdr, meta_hdr, cmpl_list); |
| 461 | goto reset_slots; |
Tomas Winkler | 10ee907 | 2013-11-11 13:26:08 +0200 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | |
Alexander Usyskin | 83ce074 | 2014-01-08 22:31:46 +0200 | [diff] [blame] | 465 | /* if no recipient cl was found we assume corrupted header */ |
Alexander Usyskin | c10187b | 2022-03-08 11:59:26 +0200 | [diff] [blame] | 466 | /* A message for not connected fixed address clients |
| 467 | * should be silently discarded |
| 468 | * On power down client may be force cleaned, |
| 469 | * silently discard such messages |
| 470 | */ |
| 471 | if (hdr_is_fixed(mei_hdr) || |
| 472 | dev->dev_state == MEI_DEV_POWER_DOWN) { |
| 473 | mei_irq_discard_msg(dev, mei_hdr, mei_hdr->length); |
| 474 | ret = 0; |
| 475 | goto reset_slots; |
Tomas Winkler | 10ee907 | 2013-11-11 13:26:08 +0200 | [diff] [blame] | 476 | } |
Alexander Usyskin | c10187b | 2022-03-08 11:59:26 +0200 | [diff] [blame] | 477 | dev_err(dev->dev, "no destination client found 0x%08X\n", dev->rd_msg_hdr[0]); |
| 478 | ret = -EBADMSG; |
| 479 | goto end; |
Tomas Winkler | 3d33ff2 | 2015-02-10 10:39:36 +0200 | [diff] [blame] | 480 | |
Tomas Winkler | 10ee907 | 2013-11-11 13:26:08 +0200 | [diff] [blame] | 481 | reset_slots: |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 482 | /* reset the number of slots and header */ |
Tomas Winkler | 6316321 | 2018-11-22 13:11:39 +0200 | [diff] [blame] | 483 | memset(dev->rd_msg_hdr, 0, sizeof(dev->rd_msg_hdr)); |
Tomas Winkler | 0cd7c01 | 2020-08-18 14:51:38 +0300 | [diff] [blame] | 484 | dev->rd_msg_hdr_count = 0; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 485 | *slots = mei_count_full_read_slots(dev); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 486 | if (*slots == -EOVERFLOW) { |
| 487 | /* overflow - reset */ |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 488 | dev_err(dev->dev, "resetting due to slots overflow.\n"); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 489 | /* set the event since message has been read */ |
| 490 | ret = -ERANGE; |
| 491 | goto end; |
| 492 | } |
| 493 | end: |
| 494 | return ret; |
| 495 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame] | 496 | EXPORT_SYMBOL_GPL(mei_irq_read_handler); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 497 | |
| 498 | |
| 499 | /** |
Tomas Winkler | 06ecd64 | 2013-02-06 14:06:42 +0200 | [diff] [blame] | 500 | * mei_irq_write_handler - dispatch write requests |
| 501 | * after irq received |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 502 | * |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 503 | * @dev: the device structure |
Tomas Winkler | 9a84d61 | 2012-11-18 15:13:18 +0200 | [diff] [blame] | 504 | * @cmpl_list: An instance of our list structure |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 505 | * |
Alexander Usyskin | a8605ea | 2014-09-29 16:31:49 +0300 | [diff] [blame] | 506 | * Return: 0 on success, <0 on failure. |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 507 | */ |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 508 | int mei_irq_write_handler(struct mei_device *dev, struct list_head *cmpl_list) |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 509 | { |
| 510 | |
| 511 | struct mei_cl *cl; |
Tomas Winkler | 6220d6a | 2013-05-12 15:34:46 +0300 | [diff] [blame] | 512 | struct mei_cl_cb *cb, *next; |
Tomas Winkler | 9a84d61 | 2012-11-18 15:13:18 +0200 | [diff] [blame] | 513 | s32 slots; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 514 | int ret; |
| 515 | |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 516 | |
| 517 | if (!mei_hbuf_acquire(dev)) |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 518 | return 0; |
Tomas Winkler | 6aae48f | 2014-02-19 17:35:47 +0200 | [diff] [blame] | 519 | |
Tomas Winkler | 9a84d61 | 2012-11-18 15:13:18 +0200 | [diff] [blame] | 520 | slots = mei_hbuf_empty_slots(dev); |
Tomas Winkler | de87743 | 2018-07-12 17:10:08 +0300 | [diff] [blame] | 521 | if (slots < 0) |
| 522 | return -EOVERFLOW; |
| 523 | |
| 524 | if (slots == 0) |
Tomas Winkler | 7d5e0e5 | 2012-06-19 09:13:36 +0300 | [diff] [blame] | 525 | return -EMSGSIZE; |
| 526 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 527 | /* complete all waiting for write CB */ |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 528 | dev_dbg(dev->dev, "complete all waiting for write cb.\n"); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 529 | |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 530 | list_for_each_entry_safe(cb, next, &dev->write_waiting_list, list) { |
Tomas Winkler | 6220d6a | 2013-05-12 15:34:46 +0300 | [diff] [blame] | 531 | cl = cb->cl; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 532 | |
Tomas Winkler | b7cd2d9 | 2011-11-27 21:43:34 +0200 | [diff] [blame] | 533 | cl->status = 0; |
Tomas Winkler | c54bf3a | 2015-02-10 10:39:39 +0200 | [diff] [blame] | 534 | cl_dbg(dev, cl, "MEI WRITE COMPLETE\n"); |
| 535 | cl->writing_state = MEI_WRITE_COMPLETE; |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 536 | list_move_tail(&cb->list, cmpl_list); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 537 | } |
| 538 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 539 | /* complete control write list CB */ |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 540 | dev_dbg(dev->dev, "complete control write list cb.\n"); |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 541 | list_for_each_entry_safe(cb, next, &dev->ctrl_wr_list, list) { |
Tomas Winkler | 6220d6a | 2013-05-12 15:34:46 +0300 | [diff] [blame] | 542 | cl = cb->cl; |
Tomas Winkler | 6220d6a | 2013-05-12 15:34:46 +0300 | [diff] [blame] | 543 | switch (cb->fop_type) { |
Tomas Winkler | 5a8373f | 2014-08-21 14:29:17 +0300 | [diff] [blame] | 544 | case MEI_FOP_DISCONNECT: |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 545 | /* send disconnect message */ |
Tomas Winkler | 5a8373f | 2014-08-21 14:29:17 +0300 | [diff] [blame] | 546 | ret = mei_cl_irq_disconnect(cl, cb, cmpl_list); |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 547 | if (ret) |
| 548 | return ret; |
| 549 | |
| 550 | break; |
Tomas Winkler | 4b8960b | 2012-11-11 17:38:00 +0200 | [diff] [blame] | 551 | case MEI_FOP_READ: |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 552 | /* send flow control message */ |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 553 | ret = mei_cl_irq_read(cl, cb, cmpl_list); |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 554 | if (ret) |
| 555 | return ret; |
| 556 | |
| 557 | break; |
Tomas Winkler | 02a7eec | 2014-02-12 21:41:51 +0200 | [diff] [blame] | 558 | case MEI_FOP_CONNECT: |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 559 | /* connect message */ |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 560 | ret = mei_cl_irq_connect(cl, cb, cmpl_list); |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 561 | if (ret) |
| 562 | return ret; |
| 563 | |
| 564 | break; |
Tomas Winkler | 6bb948c | 2014-02-12 21:41:52 +0200 | [diff] [blame] | 565 | case MEI_FOP_DISCONNECT_RSP: |
| 566 | /* send disconnect resp */ |
Tomas Winkler | 9d09819 | 2014-02-19 17:35:48 +0200 | [diff] [blame] | 567 | ret = mei_cl_irq_disconnect_rsp(cl, cb, cmpl_list); |
Tomas Winkler | 6bb948c | 2014-02-12 21:41:52 +0200 | [diff] [blame] | 568 | if (ret) |
| 569 | return ret; |
Alexander Usyskin | 31a5ef2 | 2014-03-06 23:53:53 +0200 | [diff] [blame] | 570 | break; |
Tomas Winkler | 51678cc | 2015-07-26 09:54:18 +0300 | [diff] [blame] | 571 | |
| 572 | case MEI_FOP_NOTIFY_START: |
| 573 | case MEI_FOP_NOTIFY_STOP: |
| 574 | ret = mei_cl_irq_notify(cl, cb, cmpl_list); |
| 575 | if (ret) |
| 576 | return ret; |
| 577 | break; |
Alexander Usyskin | 369aea8 | 2021-02-06 16:43:24 +0200 | [diff] [blame] | 578 | case MEI_FOP_DMA_MAP: |
| 579 | ret = mei_cl_irq_dma_map(cl, cb, cmpl_list); |
| 580 | if (ret) |
| 581 | return ret; |
| 582 | break; |
| 583 | case MEI_FOP_DMA_UNMAP: |
| 584 | ret = mei_cl_irq_dma_unmap(cl, cb, cmpl_list); |
| 585 | if (ret) |
| 586 | return ret; |
| 587 | break; |
Tomas Winkler | c837209 | 2011-11-27 21:43:33 +0200 | [diff] [blame] | 588 | default: |
| 589 | BUG(); |
| 590 | } |
| 591 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 592 | } |
| 593 | /* complete write list CB */ |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 594 | dev_dbg(dev->dev, "complete write list cb.\n"); |
Alexander Usyskin | 962ff7b | 2017-01-27 16:32:45 +0200 | [diff] [blame] | 595 | list_for_each_entry_safe(cb, next, &dev->write_list, list) { |
Tomas Winkler | 6220d6a | 2013-05-12 15:34:46 +0300 | [diff] [blame] | 596 | cl = cb->cl; |
Alexander Usyskin | 394a77d | 2017-03-20 15:04:03 +0200 | [diff] [blame] | 597 | ret = mei_cl_irq_write(cl, cb, cmpl_list); |
Tomas Winkler | be9d87a | 2012-11-18 15:13:19 +0200 | [diff] [blame] | 598 | if (ret) |
| 599 | return ret; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 600 | } |
| 601 | return 0; |
| 602 | } |
Tomas Winkler | 40e0b67 | 2013-03-27 16:58:30 +0200 | [diff] [blame] | 603 | EXPORT_SYMBOL_GPL(mei_irq_write_handler); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 604 | |
| 605 | |
Alexander Usyskin | 1890135 | 2015-07-23 21:37:13 +0300 | [diff] [blame] | 606 | /** |
| 607 | * mei_connect_timeout - connect/disconnect timeouts |
| 608 | * |
| 609 | * @cl: host client |
| 610 | */ |
| 611 | static void mei_connect_timeout(struct mei_cl *cl) |
| 612 | { |
| 613 | struct mei_device *dev = cl->dev; |
| 614 | |
| 615 | if (cl->state == MEI_FILE_CONNECTING) { |
| 616 | if (dev->hbm_f_dot_supported) { |
| 617 | cl->state = MEI_FILE_DISCONNECT_REQUIRED; |
| 618 | wake_up(&cl->wait); |
| 619 | return; |
| 620 | } |
| 621 | } |
| 622 | mei_reset(dev); |
| 623 | } |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 624 | |
Alexander Usyskin | 1892fc2 | 2016-09-25 13:25:31 +0300 | [diff] [blame] | 625 | #define MEI_STALL_TIMER_FREQ (2 * HZ) |
| 626 | /** |
| 627 | * mei_schedule_stall_timer - re-arm stall_timer work |
| 628 | * |
Alexander Usyskin | 1892fc2 | 2016-09-25 13:25:31 +0300 | [diff] [blame] | 629 | * @dev: the device structure |
Randy Dunlap | 980dcc7 | 2023-10-11 19:48:44 -0700 | [diff] [blame] | 630 | * |
| 631 | * Schedule stall timer |
Alexander Usyskin | 1892fc2 | 2016-09-25 13:25:31 +0300 | [diff] [blame] | 632 | */ |
| 633 | void mei_schedule_stall_timer(struct mei_device *dev) |
| 634 | { |
| 635 | schedule_delayed_work(&dev->timer_work, MEI_STALL_TIMER_FREQ); |
| 636 | } |
| 637 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 638 | /** |
| 639 | * mei_timer - timer function. |
| 640 | * |
| 641 | * @work: pointer to the work_struct structure |
| 642 | * |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 643 | */ |
Oren Weil | a61c653 | 2011-09-07 09:03:13 +0300 | [diff] [blame] | 644 | void mei_timer(struct work_struct *work) |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 645 | { |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 646 | struct mei_cl *cl; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 647 | struct mei_device *dev = container_of(work, |
Oren Weil | a61c653 | 2011-09-07 09:03:13 +0300 | [diff] [blame] | 648 | struct mei_device, timer_work.work); |
Alexander Usyskin | 1892fc2 | 2016-09-25 13:25:31 +0300 | [diff] [blame] | 649 | bool reschedule_timer = false; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 650 | |
| 651 | mutex_lock(&dev->device_lock); |
Tomas Winkler | 66ae460 | 2014-01-08 20:19:22 +0200 | [diff] [blame] | 652 | |
| 653 | /* Catch interrupt stalls during HBM init handshake */ |
| 654 | if (dev->dev_state == MEI_DEV_INIT_CLIENTS && |
| 655 | dev->hbm_state != MEI_HBM_IDLE) { |
| 656 | |
| 657 | if (dev->init_clients_timer) { |
| 658 | if (--dev->init_clients_timer == 0) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 659 | dev_err(dev->dev, "timer: init clients timeout hbm_state = %d.\n", |
Tomas Winkler | 66ae460 | 2014-01-08 20:19:22 +0200 | [diff] [blame] | 660 | dev->hbm_state); |
Tomas Winkler | 33ec082 | 2014-01-12 00:36:09 +0200 | [diff] [blame] | 661 | mei_reset(dev); |
Tomas Winkler | 66ae460 | 2014-01-08 20:19:22 +0200 | [diff] [blame] | 662 | goto out; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 663 | } |
Alexander Usyskin | 1892fc2 | 2016-09-25 13:25:31 +0300 | [diff] [blame] | 664 | reschedule_timer = true; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 665 | } |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 666 | } |
Tomas Winkler | 66ae460 | 2014-01-08 20:19:22 +0200 | [diff] [blame] | 667 | |
| 668 | if (dev->dev_state != MEI_DEV_ENABLED) |
| 669 | goto out; |
| 670 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 671 | /*** connect/disconnect timeouts ***/ |
Tomas Winkler | 31f88f5 | 2014-02-17 15:13:25 +0200 | [diff] [blame] | 672 | list_for_each_entry(cl, &dev->file_list, link) { |
| 673 | if (cl->timer_count) { |
| 674 | if (--cl->timer_count == 0) { |
Tomas Winkler | 2bf94cab | 2014-09-29 16:31:42 +0300 | [diff] [blame] | 675 | dev_err(dev->dev, "timer: connect/disconnect timeout.\n"); |
Alexander Usyskin | 1890135 | 2015-07-23 21:37:13 +0300 | [diff] [blame] | 676 | mei_connect_timeout(cl); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 677 | goto out; |
| 678 | } |
Alexander Usyskin | 1892fc2 | 2016-09-25 13:25:31 +0300 | [diff] [blame] | 679 | reschedule_timer = true; |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 680 | } |
| 681 | } |
| 682 | |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 683 | out: |
Alexander Usyskin | 1892fc2 | 2016-09-25 13:25:31 +0300 | [diff] [blame] | 684 | if (dev->dev_state != MEI_DEV_DISABLED && reschedule_timer) |
| 685 | mei_schedule_stall_timer(dev); |
| 686 | |
Tomas Winkler | 441ab50 | 2011-12-13 23:39:34 +0200 | [diff] [blame] | 687 | mutex_unlock(&dev->device_lock); |
Oren Weil | fb7d879 | 2011-05-15 13:43:42 +0300 | [diff] [blame] | 688 | } |