Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 2 | /* |
Mika Westerberg | 15c6784 | 2018-10-01 12:31:22 +0300 | [diff] [blame] | 3 | * Thunderbolt driver - control channel and configuration commands |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 4 | * |
| 5 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> |
Mika Westerberg | 15c6784 | 2018-10-01 12:31:22 +0300 | [diff] [blame] | 6 | * Copyright (C) 2018, Intel Corporation |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/crc32.h> |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 10 | #include <linux/delay.h> |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 11 | #include <linux/slab.h> |
| 12 | #include <linux/pci.h> |
| 13 | #include <linux/dmapool.h> |
| 14 | #include <linux/workqueue.h> |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 15 | |
| 16 | #include "ctl.h" |
| 17 | |
| 18 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 19 | #define TB_CTL_RX_PKG_COUNT 10 |
Sanjay R Mehta | 641cdbe | 2021-09-24 02:44:24 -0500 | [diff] [blame] | 20 | #define TB_CTL_RETRIES 4 |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 21 | |
| 22 | /** |
Mika Westerberg | 9c3db0b | 2021-01-28 13:05:55 +0300 | [diff] [blame] | 23 | * struct tb_ctl - Thunderbolt control channel |
| 24 | * @nhi: Pointer to the NHI structure |
| 25 | * @tx: Transmit ring |
| 26 | * @rx: Receive ring |
| 27 | * @frame_pool: DMA pool for control messages |
| 28 | * @rx_packets: Received control messages |
| 29 | * @request_queue_lock: Lock protecting @request_queue |
| 30 | * @request_queue: List of outstanding requests |
| 31 | * @running: Is the control channel running at the moment |
Mika Westerberg | 7f0a34d | 2020-12-29 13:44:57 +0200 | [diff] [blame] | 32 | * @timeout_msec: Default timeout for non-raw control messages |
Mika Westerberg | 9c3db0b | 2021-01-28 13:05:55 +0300 | [diff] [blame] | 33 | * @callback: Callback called when hotplug message is received |
| 34 | * @callback_data: Data passed to @callback |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 35 | */ |
| 36 | struct tb_ctl { |
| 37 | struct tb_nhi *nhi; |
| 38 | struct tb_ring *tx; |
| 39 | struct tb_ring *rx; |
| 40 | |
| 41 | struct dma_pool *frame_pool; |
| 42 | struct ctl_pkg *rx_packets[TB_CTL_RX_PKG_COUNT]; |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 43 | struct mutex request_queue_lock; |
| 44 | struct list_head request_queue; |
| 45 | bool running; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 46 | |
Mika Westerberg | 7f0a34d | 2020-12-29 13:44:57 +0200 | [diff] [blame] | 47 | int timeout_msec; |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 48 | event_cb callback; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 49 | void *callback_data; |
| 50 | }; |
| 51 | |
| 52 | |
| 53 | #define tb_ctl_WARN(ctl, format, arg...) \ |
| 54 | dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg) |
| 55 | |
| 56 | #define tb_ctl_err(ctl, format, arg...) \ |
| 57 | dev_err(&(ctl)->nhi->pdev->dev, format, ## arg) |
| 58 | |
| 59 | #define tb_ctl_warn(ctl, format, arg...) \ |
| 60 | dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg) |
| 61 | |
| 62 | #define tb_ctl_info(ctl, format, arg...) \ |
| 63 | dev_info(&(ctl)->nhi->pdev->dev, format, ## arg) |
| 64 | |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 65 | #define tb_ctl_dbg(ctl, format, arg...) \ |
| 66 | dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg) |
| 67 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 68 | static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue); |
| 69 | /* Serializes access to request kref_get/put */ |
| 70 | static DEFINE_MUTEX(tb_cfg_request_lock); |
| 71 | |
| 72 | /** |
| 73 | * tb_cfg_request_alloc() - Allocates a new config request |
| 74 | * |
| 75 | * This is refcounted object so when you are done with this, call |
| 76 | * tb_cfg_request_put() to it. |
| 77 | */ |
| 78 | struct tb_cfg_request *tb_cfg_request_alloc(void) |
| 79 | { |
| 80 | struct tb_cfg_request *req; |
| 81 | |
| 82 | req = kzalloc(sizeof(*req), GFP_KERNEL); |
| 83 | if (!req) |
| 84 | return NULL; |
| 85 | |
| 86 | kref_init(&req->kref); |
| 87 | |
| 88 | return req; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * tb_cfg_request_get() - Increase refcount of a request |
| 93 | * @req: Request whose refcount is increased |
| 94 | */ |
| 95 | void tb_cfg_request_get(struct tb_cfg_request *req) |
| 96 | { |
| 97 | mutex_lock(&tb_cfg_request_lock); |
| 98 | kref_get(&req->kref); |
| 99 | mutex_unlock(&tb_cfg_request_lock); |
| 100 | } |
| 101 | |
| 102 | static void tb_cfg_request_destroy(struct kref *kref) |
| 103 | { |
| 104 | struct tb_cfg_request *req = container_of(kref, typeof(*req), kref); |
| 105 | |
| 106 | kfree(req); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * tb_cfg_request_put() - Decrease refcount and possibly release the request |
| 111 | * @req: Request whose refcount is decreased |
| 112 | * |
| 113 | * Call this function when you are done with the request. When refcount |
| 114 | * goes to %0 the object is released. |
| 115 | */ |
| 116 | void tb_cfg_request_put(struct tb_cfg_request *req) |
| 117 | { |
| 118 | mutex_lock(&tb_cfg_request_lock); |
| 119 | kref_put(&req->kref, tb_cfg_request_destroy); |
| 120 | mutex_unlock(&tb_cfg_request_lock); |
| 121 | } |
| 122 | |
| 123 | static int tb_cfg_request_enqueue(struct tb_ctl *ctl, |
| 124 | struct tb_cfg_request *req) |
| 125 | { |
| 126 | WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags)); |
| 127 | WARN_ON(req->ctl); |
| 128 | |
| 129 | mutex_lock(&ctl->request_queue_lock); |
| 130 | if (!ctl->running) { |
| 131 | mutex_unlock(&ctl->request_queue_lock); |
| 132 | return -ENOTCONN; |
| 133 | } |
| 134 | req->ctl = ctl; |
| 135 | list_add_tail(&req->list, &ctl->request_queue); |
| 136 | set_bit(TB_CFG_REQUEST_ACTIVE, &req->flags); |
| 137 | mutex_unlock(&ctl->request_queue_lock); |
| 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | static void tb_cfg_request_dequeue(struct tb_cfg_request *req) |
| 142 | { |
| 143 | struct tb_ctl *ctl = req->ctl; |
| 144 | |
| 145 | mutex_lock(&ctl->request_queue_lock); |
| 146 | list_del(&req->list); |
| 147 | clear_bit(TB_CFG_REQUEST_ACTIVE, &req->flags); |
| 148 | if (test_bit(TB_CFG_REQUEST_CANCELED, &req->flags)) |
| 149 | wake_up(&tb_cfg_request_cancel_queue); |
| 150 | mutex_unlock(&ctl->request_queue_lock); |
| 151 | } |
| 152 | |
| 153 | static bool tb_cfg_request_is_active(struct tb_cfg_request *req) |
| 154 | { |
| 155 | return test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags); |
| 156 | } |
| 157 | |
| 158 | static struct tb_cfg_request * |
| 159 | tb_cfg_request_find(struct tb_ctl *ctl, struct ctl_pkg *pkg) |
| 160 | { |
| 161 | struct tb_cfg_request *req; |
| 162 | bool found = false; |
| 163 | |
| 164 | mutex_lock(&pkg->ctl->request_queue_lock); |
| 165 | list_for_each_entry(req, &pkg->ctl->request_queue, list) { |
| 166 | tb_cfg_request_get(req); |
| 167 | if (req->match(req, pkg)) { |
| 168 | found = true; |
| 169 | break; |
| 170 | } |
| 171 | tb_cfg_request_put(req); |
| 172 | } |
| 173 | mutex_unlock(&pkg->ctl->request_queue_lock); |
| 174 | |
| 175 | return found ? req : NULL; |
| 176 | } |
| 177 | |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 178 | /* utility functions */ |
| 179 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 180 | |
| 181 | static int check_header(const struct ctl_pkg *pkg, u32 len, |
| 182 | enum tb_cfg_pkg_type type, u64 route) |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 183 | { |
| 184 | struct tb_cfg_header *header = pkg->buffer; |
| 185 | |
| 186 | /* check frame, TODO: frame flags */ |
| 187 | if (WARN(len != pkg->frame.size, |
| 188 | "wrong framesize (expected %#x, got %#x)\n", |
| 189 | len, pkg->frame.size)) |
| 190 | return -EIO; |
| 191 | if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n", |
| 192 | type, pkg->frame.eof)) |
| 193 | return -EIO; |
| 194 | if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n", |
| 195 | pkg->frame.sof)) |
| 196 | return -EIO; |
| 197 | |
| 198 | /* check header */ |
| 199 | if (WARN(header->unknown != 1 << 9, |
| 200 | "header->unknown is %#x\n", header->unknown)) |
| 201 | return -EIO; |
Mika Westerberg | ac6c44d | 2017-06-06 15:25:07 +0300 | [diff] [blame] | 202 | if (WARN(route != tb_cfg_get_route(header), |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 203 | "wrong route (expected %llx, got %llx)", |
Mika Westerberg | ac6c44d | 2017-06-06 15:25:07 +0300 | [diff] [blame] | 204 | route, tb_cfg_get_route(header))) |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 205 | return -EIO; |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | static int check_config_address(struct tb_cfg_address addr, |
| 210 | enum tb_cfg_space space, u32 offset, |
| 211 | u32 length) |
| 212 | { |
| 213 | if (WARN(addr.zero, "addr.zero is %#x\n", addr.zero)) |
| 214 | return -EIO; |
| 215 | if (WARN(space != addr.space, "wrong space (expected %x, got %x\n)", |
| 216 | space, addr.space)) |
| 217 | return -EIO; |
| 218 | if (WARN(offset != addr.offset, "wrong offset (expected %x, got %x\n)", |
| 219 | offset, addr.offset)) |
| 220 | return -EIO; |
| 221 | if (WARN(length != addr.length, "wrong space (expected %x, got %x\n)", |
| 222 | length, addr.length)) |
| 223 | return -EIO; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 224 | /* |
| 225 | * We cannot check addr->port as it is set to the upstream port of the |
| 226 | * sender. |
| 227 | */ |
| 228 | return 0; |
| 229 | } |
| 230 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 231 | static struct tb_cfg_result decode_error(const struct ctl_pkg *response) |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 232 | { |
| 233 | struct cfg_error_pkg *pkg = response->buffer; |
Mika Westerberg | 22255be | 2019-11-29 15:49:28 +0300 | [diff] [blame] | 234 | struct tb_ctl *ctl = response->ctl; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 235 | struct tb_cfg_result res = { 0 }; |
Mika Westerberg | ac6c44d | 2017-06-06 15:25:07 +0300 | [diff] [blame] | 236 | res.response_route = tb_cfg_get_route(&pkg->header); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 237 | res.response_port = 0; |
| 238 | res.err = check_header(response, sizeof(*pkg), TB_CFG_PKG_ERROR, |
Mika Westerberg | ac6c44d | 2017-06-06 15:25:07 +0300 | [diff] [blame] | 239 | tb_cfg_get_route(&pkg->header)); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 240 | if (res.err) |
| 241 | return res; |
| 242 | |
Mika Westerberg | 22255be | 2019-11-29 15:49:28 +0300 | [diff] [blame] | 243 | if (pkg->zero1) |
| 244 | tb_ctl_warn(ctl, "pkg->zero1 is %#x\n", pkg->zero1); |
| 245 | if (pkg->zero2) |
| 246 | tb_ctl_warn(ctl, "pkg->zero2 is %#x\n", pkg->zero2); |
| 247 | if (pkg->zero3) |
| 248 | tb_ctl_warn(ctl, "pkg->zero3 is %#x\n", pkg->zero3); |
| 249 | |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 250 | res.err = 1; |
| 251 | res.tb_error = pkg->error; |
| 252 | res.response_port = pkg->port; |
| 253 | return res; |
| 254 | |
| 255 | } |
| 256 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 257 | static struct tb_cfg_result parse_header(const struct ctl_pkg *pkg, u32 len, |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 258 | enum tb_cfg_pkg_type type, u64 route) |
| 259 | { |
| 260 | struct tb_cfg_header *header = pkg->buffer; |
| 261 | struct tb_cfg_result res = { 0 }; |
| 262 | |
| 263 | if (pkg->frame.eof == TB_CFG_PKG_ERROR) |
| 264 | return decode_error(pkg); |
| 265 | |
| 266 | res.response_port = 0; /* will be updated later for cfg_read/write */ |
Mika Westerberg | ac6c44d | 2017-06-06 15:25:07 +0300 | [diff] [blame] | 267 | res.response_route = tb_cfg_get_route(header); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 268 | res.err = check_header(pkg, len, type, route); |
| 269 | return res; |
| 270 | } |
| 271 | |
| 272 | static void tb_cfg_print_error(struct tb_ctl *ctl, |
| 273 | const struct tb_cfg_result *res) |
| 274 | { |
| 275 | WARN_ON(res->err != 1); |
| 276 | switch (res->tb_error) { |
| 277 | case TB_CFG_ERROR_PORT_NOT_CONNECTED: |
| 278 | /* Port is not connected. This can happen during surprise |
| 279 | * removal. Do not warn. */ |
| 280 | return; |
| 281 | case TB_CFG_ERROR_INVALID_CONFIG_SPACE: |
| 282 | /* |
| 283 | * Invalid cfg_space/offset/length combination in |
| 284 | * cfg_read/cfg_write. |
| 285 | */ |
Mika Westerberg | fa1653d | 2020-06-29 20:23:58 +0300 | [diff] [blame] | 286 | tb_ctl_dbg(ctl, "%llx:%x: invalid config space or offset\n", |
| 287 | res->response_route, res->response_port); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 288 | return; |
| 289 | case TB_CFG_ERROR_NO_SUCH_PORT: |
| 290 | /* |
| 291 | * - The route contains a non-existent port. |
| 292 | * - The route contains a non-PHY port (e.g. PCIe). |
| 293 | * - The port in cfg_read/cfg_write does not exist. |
| 294 | */ |
| 295 | tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n", |
| 296 | res->response_route, res->response_port); |
| 297 | return; |
| 298 | case TB_CFG_ERROR_LOOP: |
| 299 | tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Route contains a loop\n", |
| 300 | res->response_route, res->response_port); |
| 301 | return; |
Mika Westerberg | 80e7c5d | 2019-09-19 15:22:20 +0300 | [diff] [blame] | 302 | case TB_CFG_ERROR_LOCK: |
| 303 | tb_ctl_warn(ctl, "%llx:%x: downstream port is locked\n", |
| 304 | res->response_route, res->response_port); |
| 305 | return; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 306 | default: |
| 307 | /* 5,6,7,9 and 11 are also valid error codes */ |
| 308 | tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Unknown error\n", |
| 309 | res->response_route, res->response_port); |
| 310 | return; |
| 311 | } |
| 312 | } |
| 313 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 314 | static __be32 tb_crc(const void *data, size_t len) |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 315 | { |
| 316 | return cpu_to_be32(~__crc32c_le(~0, data, len)); |
| 317 | } |
| 318 | |
| 319 | static void tb_ctl_pkg_free(struct ctl_pkg *pkg) |
| 320 | { |
| 321 | if (pkg) { |
| 322 | dma_pool_free(pkg->ctl->frame_pool, |
| 323 | pkg->buffer, pkg->frame.buffer_phy); |
| 324 | kfree(pkg); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl) |
| 329 | { |
| 330 | struct ctl_pkg *pkg = kzalloc(sizeof(*pkg), GFP_KERNEL); |
| 331 | if (!pkg) |
Sachin Kamat | 8db353b | 2014-06-20 14:32:32 +0530 | [diff] [blame] | 332 | return NULL; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 333 | pkg->ctl = ctl; |
| 334 | pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL, |
| 335 | &pkg->frame.buffer_phy); |
| 336 | if (!pkg->buffer) { |
| 337 | kfree(pkg); |
Sachin Kamat | 8db353b | 2014-06-20 14:32:32 +0530 | [diff] [blame] | 338 | return NULL; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 339 | } |
| 340 | return pkg; |
| 341 | } |
| 342 | |
| 343 | |
| 344 | /* RX/TX handling */ |
| 345 | |
| 346 | static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame, |
| 347 | bool canceled) |
| 348 | { |
| 349 | struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame); |
| 350 | tb_ctl_pkg_free(pkg); |
| 351 | } |
| 352 | |
Lee Jones | aa60825 | 2021-01-27 11:25:45 +0000 | [diff] [blame] | 353 | /* |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 354 | * tb_cfg_tx() - transmit a packet on the control channel |
| 355 | * |
| 356 | * len must be a multiple of four. |
| 357 | * |
| 358 | * Return: Returns 0 on success or an error code on failure. |
| 359 | */ |
Mika Westerberg | 16a1258 | 2017-06-06 15:24:53 +0300 | [diff] [blame] | 360 | static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len, |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 361 | enum tb_cfg_pkg_type type) |
| 362 | { |
| 363 | int res; |
| 364 | struct ctl_pkg *pkg; |
| 365 | if (len % 4 != 0) { /* required for le->be conversion */ |
| 366 | tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len); |
| 367 | return -EINVAL; |
| 368 | } |
| 369 | if (len > TB_FRAME_SIZE - 4) { /* checksum is 4 bytes */ |
| 370 | tb_ctl_WARN(ctl, "TX: packet too large: %zu/%d\n", |
| 371 | len, TB_FRAME_SIZE - 4); |
| 372 | return -EINVAL; |
| 373 | } |
| 374 | pkg = tb_ctl_pkg_alloc(ctl); |
| 375 | if (!pkg) |
| 376 | return -ENOMEM; |
| 377 | pkg->frame.callback = tb_ctl_tx_callback; |
| 378 | pkg->frame.size = len + 4; |
| 379 | pkg->frame.sof = type; |
| 380 | pkg->frame.eof = type; |
| 381 | cpu_to_be32_array(pkg->buffer, data, len / 4); |
Andreas Noever | 801dba5 | 2014-06-20 21:42:22 +0200 | [diff] [blame] | 382 | *(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 383 | |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 384 | res = tb_ring_tx(ctl->tx, &pkg->frame); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 385 | if (res) /* ring is stopped */ |
| 386 | tb_ctl_pkg_free(pkg); |
| 387 | return res; |
| 388 | } |
| 389 | |
Lee Jones | aa60825 | 2021-01-27 11:25:45 +0000 | [diff] [blame] | 390 | /* |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 391 | * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 392 | */ |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 393 | static bool tb_ctl_handle_event(struct tb_ctl *ctl, enum tb_cfg_pkg_type type, |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 394 | struct ctl_pkg *pkg, size_t size) |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 395 | { |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 396 | return ctl->callback(ctl->callback_data, type, pkg->buffer, size); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 397 | } |
| 398 | |
| 399 | static void tb_ctl_rx_submit(struct ctl_pkg *pkg) |
| 400 | { |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 401 | tb_ring_rx(pkg->ctl->rx, &pkg->frame); /* |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 402 | * We ignore failures during stop. |
| 403 | * All rx packets are referenced |
| 404 | * from ctl->rx_packets, so we do |
| 405 | * not loose them. |
| 406 | */ |
| 407 | } |
| 408 | |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 409 | static int tb_async_error(const struct ctl_pkg *pkg) |
| 410 | { |
| 411 | const struct cfg_error_pkg *error = (const struct cfg_error_pkg *)pkg; |
| 412 | |
| 413 | if (pkg->frame.eof != TB_CFG_PKG_ERROR) |
| 414 | return false; |
| 415 | |
| 416 | switch (error->error) { |
| 417 | case TB_CFG_ERROR_LINK_ERROR: |
| 418 | case TB_CFG_ERROR_HEC_ERROR_DETECTED: |
| 419 | case TB_CFG_ERROR_FLOW_CONTROL_ERROR: |
| 420 | return true; |
| 421 | |
| 422 | default: |
| 423 | return false; |
| 424 | } |
| 425 | } |
| 426 | |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 427 | static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame, |
| 428 | bool canceled) |
| 429 | { |
| 430 | struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame); |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 431 | struct tb_cfg_request *req; |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 432 | __be32 crc32; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 433 | |
| 434 | if (canceled) |
| 435 | return; /* |
| 436 | * ring is stopped, packet is referenced from |
| 437 | * ctl->rx_packets. |
| 438 | */ |
| 439 | |
| 440 | if (frame->size < 4 || frame->size % 4 != 0) { |
| 441 | tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n", |
| 442 | frame->size); |
| 443 | goto rx; |
| 444 | } |
| 445 | |
| 446 | frame->size -= 4; /* remove checksum */ |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 447 | crc32 = tb_crc(pkg->buffer, frame->size); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 448 | be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4); |
| 449 | |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 450 | switch (frame->eof) { |
| 451 | case TB_CFG_PKG_READ: |
| 452 | case TB_CFG_PKG_WRITE: |
| 453 | case TB_CFG_PKG_ERROR: |
| 454 | case TB_CFG_PKG_OVERRIDE: |
| 455 | case TB_CFG_PKG_RESET: |
| 456 | if (*(__be32 *)(pkg->buffer + frame->size) != crc32) { |
| 457 | tb_ctl_err(pkg->ctl, |
| 458 | "RX: checksum mismatch, dropping packet\n"); |
| 459 | goto rx; |
| 460 | } |
| 461 | if (tb_async_error(pkg)) { |
| 462 | tb_ctl_handle_event(pkg->ctl, frame->eof, |
| 463 | pkg, frame->size); |
| 464 | goto rx; |
| 465 | } |
| 466 | break; |
| 467 | |
| 468 | case TB_CFG_PKG_EVENT: |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 469 | case TB_CFG_PKG_XDOMAIN_RESP: |
| 470 | case TB_CFG_PKG_XDOMAIN_REQ: |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 471 | if (*(__be32 *)(pkg->buffer + frame->size) != crc32) { |
| 472 | tb_ctl_err(pkg->ctl, |
| 473 | "RX: checksum mismatch, dropping packet\n"); |
| 474 | goto rx; |
| 475 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 476 | fallthrough; |
Mika Westerberg | f67cf49 | 2017-06-06 15:25:16 +0300 | [diff] [blame] | 477 | case TB_CFG_PKG_ICM_EVENT: |
Mika Westerberg | d1ff702 | 2017-10-02 13:38:34 +0300 | [diff] [blame] | 478 | if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size)) |
| 479 | goto rx; |
| 480 | break; |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 481 | |
| 482 | default: |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 483 | break; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 484 | } |
Mika Westerberg | 81a54b5 | 2017-06-06 15:25:09 +0300 | [diff] [blame] | 485 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 486 | /* |
| 487 | * The received packet will be processed only if there is an |
| 488 | * active request and that the packet is what is expected. This |
| 489 | * prevents packets such as replies coming after timeout has |
| 490 | * triggered from messing with the active requests. |
| 491 | */ |
| 492 | req = tb_cfg_request_find(pkg->ctl, pkg); |
| 493 | if (req) { |
| 494 | if (req->copy(req, pkg)) |
| 495 | schedule_work(&req->work); |
| 496 | tb_cfg_request_put(req); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 497 | } |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 498 | |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 499 | rx: |
| 500 | tb_ctl_rx_submit(pkg); |
| 501 | } |
| 502 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 503 | static void tb_cfg_request_work(struct work_struct *work) |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 504 | { |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 505 | struct tb_cfg_request *req = container_of(work, typeof(*req), work); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 506 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 507 | if (!test_bit(TB_CFG_REQUEST_CANCELED, &req->flags)) |
| 508 | req->callback(req->callback_data); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 509 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 510 | tb_cfg_request_dequeue(req); |
| 511 | tb_cfg_request_put(req); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 512 | } |
| 513 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 514 | /** |
| 515 | * tb_cfg_request() - Start control request not waiting for it to complete |
| 516 | * @ctl: Control channel to use |
| 517 | * @req: Request to start |
| 518 | * @callback: Callback called when the request is completed |
| 519 | * @callback_data: Data to be passed to @callback |
| 520 | * |
| 521 | * This queues @req on the given control channel without waiting for it |
| 522 | * to complete. When the request completes @callback is called. |
| 523 | */ |
| 524 | int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req, |
| 525 | void (*callback)(void *), void *callback_data) |
| 526 | { |
| 527 | int ret; |
| 528 | |
| 529 | req->flags = 0; |
| 530 | req->callback = callback; |
| 531 | req->callback_data = callback_data; |
| 532 | INIT_WORK(&req->work, tb_cfg_request_work); |
| 533 | INIT_LIST_HEAD(&req->list); |
| 534 | |
| 535 | tb_cfg_request_get(req); |
| 536 | ret = tb_cfg_request_enqueue(ctl, req); |
| 537 | if (ret) |
| 538 | goto err_put; |
| 539 | |
| 540 | ret = tb_ctl_tx(ctl, req->request, req->request_size, |
| 541 | req->request_type); |
| 542 | if (ret) |
| 543 | goto err_dequeue; |
| 544 | |
| 545 | if (!req->response) |
| 546 | schedule_work(&req->work); |
| 547 | |
| 548 | return 0; |
| 549 | |
| 550 | err_dequeue: |
| 551 | tb_cfg_request_dequeue(req); |
| 552 | err_put: |
| 553 | tb_cfg_request_put(req); |
| 554 | |
| 555 | return ret; |
| 556 | } |
| 557 | |
| 558 | /** |
| 559 | * tb_cfg_request_cancel() - Cancel a control request |
| 560 | * @req: Request to cancel |
| 561 | * @err: Error to assign to the request |
| 562 | * |
| 563 | * This function can be used to cancel ongoing request. It will wait |
| 564 | * until the request is not active anymore. |
| 565 | */ |
| 566 | void tb_cfg_request_cancel(struct tb_cfg_request *req, int err) |
| 567 | { |
| 568 | set_bit(TB_CFG_REQUEST_CANCELED, &req->flags); |
| 569 | schedule_work(&req->work); |
| 570 | wait_event(tb_cfg_request_cancel_queue, !tb_cfg_request_is_active(req)); |
| 571 | req->result.err = err; |
| 572 | } |
| 573 | |
| 574 | static void tb_cfg_request_complete(void *data) |
| 575 | { |
| 576 | complete(data); |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * tb_cfg_request_sync() - Start control request and wait until it completes |
| 581 | * @ctl: Control channel to use |
| 582 | * @req: Request to start |
| 583 | * @timeout_msec: Timeout how long to wait @req to complete |
| 584 | * |
| 585 | * Starts a control request and waits until it completes. If timeout |
| 586 | * triggers the request is canceled before function returns. Note the |
| 587 | * caller needs to make sure only one message for given switch is active |
| 588 | * at a time. |
| 589 | */ |
| 590 | struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl, |
| 591 | struct tb_cfg_request *req, |
| 592 | int timeout_msec) |
| 593 | { |
| 594 | unsigned long timeout = msecs_to_jiffies(timeout_msec); |
| 595 | struct tb_cfg_result res = { 0 }; |
| 596 | DECLARE_COMPLETION_ONSTACK(done); |
| 597 | int ret; |
| 598 | |
| 599 | ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done); |
| 600 | if (ret) { |
| 601 | res.err = ret; |
| 602 | return res; |
| 603 | } |
| 604 | |
| 605 | if (!wait_for_completion_timeout(&done, timeout)) |
| 606 | tb_cfg_request_cancel(req, -ETIMEDOUT); |
| 607 | |
| 608 | flush_work(&req->work); |
| 609 | |
| 610 | return req->result; |
| 611 | } |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 612 | |
| 613 | /* public interface, alloc/start/stop/free */ |
| 614 | |
| 615 | /** |
| 616 | * tb_ctl_alloc() - allocate a control channel |
Mika Westerberg | 9c3db0b | 2021-01-28 13:05:55 +0300 | [diff] [blame] | 617 | * @nhi: Pointer to NHI |
Mika Westerberg | 7f0a34d | 2020-12-29 13:44:57 +0200 | [diff] [blame] | 618 | * @timeout_msec: Default timeout used with non-raw control messages |
Mika Westerberg | 9c3db0b | 2021-01-28 13:05:55 +0300 | [diff] [blame] | 619 | * @cb: Callback called for plug events |
| 620 | * @cb_data: Data passed to @cb |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 621 | * |
| 622 | * cb will be invoked once for every hot plug event. |
| 623 | * |
| 624 | * Return: Returns a pointer on success or NULL on failure. |
| 625 | */ |
Mika Westerberg | 7f0a34d | 2020-12-29 13:44:57 +0200 | [diff] [blame] | 626 | struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, int timeout_msec, event_cb cb, |
| 627 | void *cb_data) |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 628 | { |
| 629 | int i; |
| 630 | struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL); |
| 631 | if (!ctl) |
| 632 | return NULL; |
| 633 | ctl->nhi = nhi; |
Mika Westerberg | 7f0a34d | 2020-12-29 13:44:57 +0200 | [diff] [blame] | 634 | ctl->timeout_msec = timeout_msec; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 635 | ctl->callback = cb; |
| 636 | ctl->callback_data = cb_data; |
| 637 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 638 | mutex_init(&ctl->request_queue_lock); |
| 639 | INIT_LIST_HEAD(&ctl->request_queue); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 640 | ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev, |
| 641 | TB_FRAME_SIZE, 4, 0); |
| 642 | if (!ctl->frame_pool) |
| 643 | goto err; |
| 644 | |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 645 | ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 646 | if (!ctl->tx) |
| 647 | goto err; |
| 648 | |
Mika Westerberg | afe704a | 2020-10-19 19:15:20 +0300 | [diff] [blame] | 649 | ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0, 0xffff, |
| 650 | 0xffff, NULL, NULL); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 651 | if (!ctl->rx) |
| 652 | goto err; |
| 653 | |
| 654 | for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) { |
| 655 | ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl); |
| 656 | if (!ctl->rx_packets[i]) |
| 657 | goto err; |
| 658 | ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback; |
| 659 | } |
| 660 | |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 661 | tb_ctl_dbg(ctl, "control channel created\n"); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 662 | return ctl; |
| 663 | err: |
| 664 | tb_ctl_free(ctl); |
| 665 | return NULL; |
| 666 | } |
| 667 | |
| 668 | /** |
| 669 | * tb_ctl_free() - free a control channel |
Mika Westerberg | 9c3db0b | 2021-01-28 13:05:55 +0300 | [diff] [blame] | 670 | * @ctl: Control channel to free |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 671 | * |
| 672 | * Must be called after tb_ctl_stop. |
| 673 | * |
| 674 | * Must NOT be called from ctl->callback. |
| 675 | */ |
| 676 | void tb_ctl_free(struct tb_ctl *ctl) |
| 677 | { |
| 678 | int i; |
Mika Westerberg | c9843eb | 2017-06-06 15:24:59 +0300 | [diff] [blame] | 679 | |
| 680 | if (!ctl) |
| 681 | return; |
| 682 | |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 683 | if (ctl->rx) |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 684 | tb_ring_free(ctl->rx); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 685 | if (ctl->tx) |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 686 | tb_ring_free(ctl->tx); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 687 | |
| 688 | /* free RX packets */ |
| 689 | for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) |
| 690 | tb_ctl_pkg_free(ctl->rx_packets[i]); |
| 691 | |
| 692 | |
zhong jiang | 0bb5a1a | 2018-10-01 12:31:18 +0300 | [diff] [blame] | 693 | dma_pool_destroy(ctl->frame_pool); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 694 | kfree(ctl); |
| 695 | } |
| 696 | |
| 697 | /** |
| 698 | * tb_cfg_start() - start/resume the control channel |
Mika Westerberg | 9c3db0b | 2021-01-28 13:05:55 +0300 | [diff] [blame] | 699 | * @ctl: Control channel to start |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 700 | */ |
| 701 | void tb_ctl_start(struct tb_ctl *ctl) |
| 702 | { |
| 703 | int i; |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 704 | tb_ctl_dbg(ctl, "control channel starting...\n"); |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 705 | tb_ring_start(ctl->tx); /* is used to ack hotplug packets, start first */ |
| 706 | tb_ring_start(ctl->rx); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 707 | for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) |
| 708 | tb_ctl_rx_submit(ctl->rx_packets[i]); |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 709 | |
| 710 | ctl->running = true; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | /** |
Mika Westerberg | 9c3db0b | 2021-01-28 13:05:55 +0300 | [diff] [blame] | 714 | * tb_ctrl_stop() - pause the control channel |
| 715 | * @ctl: Control channel to stop |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 716 | * |
| 717 | * All invocations of ctl->callback will have finished after this method |
| 718 | * returns. |
| 719 | * |
| 720 | * Must NOT be called from ctl->callback. |
| 721 | */ |
| 722 | void tb_ctl_stop(struct tb_ctl *ctl) |
| 723 | { |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 724 | mutex_lock(&ctl->request_queue_lock); |
| 725 | ctl->running = false; |
| 726 | mutex_unlock(&ctl->request_queue_lock); |
| 727 | |
Mika Westerberg | 3b3d9f4 | 2017-10-02 13:38:37 +0300 | [diff] [blame] | 728 | tb_ring_stop(ctl->rx); |
| 729 | tb_ring_stop(ctl->tx); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 730 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 731 | if (!list_empty(&ctl->request_queue)) |
| 732 | tb_ctl_WARN(ctl, "dangling request in request_queue\n"); |
| 733 | INIT_LIST_HEAD(&ctl->request_queue); |
Mika Westerberg | daa5140 | 2018-10-01 12:31:19 +0300 | [diff] [blame] | 734 | tb_ctl_dbg(ctl, "control channel stopped\n"); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 735 | } |
| 736 | |
| 737 | /* public interface, commands */ |
| 738 | |
| 739 | /** |
Mika Westerberg | 210e9f5 | 2019-12-17 15:33:39 +0300 | [diff] [blame] | 740 | * tb_cfg_ack_plug() - Ack hot plug/unplug event |
| 741 | * @ctl: Control channel to use |
| 742 | * @route: Router that originated the event |
| 743 | * @port: Port where the hot plug/unplug happened |
| 744 | * @unplug: Ack hot plug or unplug |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 745 | * |
Mika Westerberg | 210e9f5 | 2019-12-17 15:33:39 +0300 | [diff] [blame] | 746 | * Call this as response for hot plug/unplug event to ack it. |
| 747 | * Returns %0 on success or an error code on failure. |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 748 | */ |
Mika Westerberg | 210e9f5 | 2019-12-17 15:33:39 +0300 | [diff] [blame] | 749 | int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug) |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 750 | { |
| 751 | struct cfg_error_pkg pkg = { |
Mika Westerberg | 05c242e | 2017-06-06 15:25:08 +0300 | [diff] [blame] | 752 | .header = tb_cfg_make_header(route), |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 753 | .port = port, |
Mika Westerberg | 210e9f5 | 2019-12-17 15:33:39 +0300 | [diff] [blame] | 754 | .error = TB_CFG_ERROR_ACK_PLUG_EVENT, |
| 755 | .pg = unplug ? TB_CFG_ERROR_PG_HOT_UNPLUG |
| 756 | : TB_CFG_ERROR_PG_HOT_PLUG, |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 757 | }; |
Mika Westerberg | 210e9f5 | 2019-12-17 15:33:39 +0300 | [diff] [blame] | 758 | tb_ctl_dbg(ctl, "acking hot %splug event on %llx:%x\n", |
| 759 | unplug ? "un" : "", route, port); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 760 | return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR); |
| 761 | } |
| 762 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 763 | static bool tb_cfg_match(const struct tb_cfg_request *req, |
| 764 | const struct ctl_pkg *pkg) |
| 765 | { |
| 766 | u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63); |
| 767 | |
| 768 | if (pkg->frame.eof == TB_CFG_PKG_ERROR) |
| 769 | return true; |
| 770 | |
| 771 | if (pkg->frame.eof != req->response_type) |
| 772 | return false; |
| 773 | if (route != tb_cfg_get_route(req->request)) |
| 774 | return false; |
| 775 | if (pkg->frame.size != req->response_size) |
| 776 | return false; |
| 777 | |
| 778 | if (pkg->frame.eof == TB_CFG_PKG_READ || |
| 779 | pkg->frame.eof == TB_CFG_PKG_WRITE) { |
| 780 | const struct cfg_read_pkg *req_hdr = req->request; |
| 781 | const struct cfg_read_pkg *res_hdr = pkg->buffer; |
| 782 | |
| 783 | if (req_hdr->addr.seq != res_hdr->addr.seq) |
| 784 | return false; |
| 785 | } |
| 786 | |
| 787 | return true; |
| 788 | } |
| 789 | |
| 790 | static bool tb_cfg_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg) |
| 791 | { |
| 792 | struct tb_cfg_result res; |
| 793 | |
| 794 | /* Now make sure it is in expected format */ |
| 795 | res = parse_header(pkg, req->response_size, req->response_type, |
| 796 | tb_cfg_get_route(req->request)); |
| 797 | if (!res.err) |
| 798 | memcpy(req->response, pkg->buffer, req->response_size); |
| 799 | |
| 800 | req->result = res; |
| 801 | |
| 802 | /* Always complete when first response is received */ |
| 803 | return true; |
| 804 | } |
| 805 | |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 806 | /** |
| 807 | * tb_cfg_reset() - send a reset packet and wait for a response |
Mika Westerberg | 9c3db0b | 2021-01-28 13:05:55 +0300 | [diff] [blame] | 808 | * @ctl: Control channel pointer |
| 809 | * @route: Router string for the router to send reset |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 810 | * |
| 811 | * If the switch at route is incorrectly configured then we will not receive a |
| 812 | * reply (even though the switch will reset). The caller should check for |
| 813 | * -ETIMEDOUT and attempt to reconfigure the switch. |
| 814 | */ |
Mika Westerberg | bda83ae | 2020-12-22 13:40:31 +0200 | [diff] [blame] | 815 | struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route) |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 816 | { |
Mika Westerberg | 05c242e | 2017-06-06 15:25:08 +0300 | [diff] [blame] | 817 | struct cfg_reset_pkg request = { .header = tb_cfg_make_header(route) }; |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 818 | struct tb_cfg_result res = { 0 }; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 819 | struct tb_cfg_header reply; |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 820 | struct tb_cfg_request *req; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 821 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 822 | req = tb_cfg_request_alloc(); |
| 823 | if (!req) { |
| 824 | res.err = -ENOMEM; |
| 825 | return res; |
| 826 | } |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 827 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 828 | req->match = tb_cfg_match; |
| 829 | req->copy = tb_cfg_copy; |
| 830 | req->request = &request; |
| 831 | req->request_size = sizeof(request); |
| 832 | req->request_type = TB_CFG_PKG_RESET; |
| 833 | req->response = &reply; |
| 834 | req->response_size = sizeof(reply); |
Dan Carpenter | 02729d1 | 2017-08-16 11:54:17 +0300 | [diff] [blame] | 835 | req->response_type = TB_CFG_PKG_RESET; |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 836 | |
Mika Westerberg | 7f0a34d | 2020-12-29 13:44:57 +0200 | [diff] [blame] | 837 | res = tb_cfg_request_sync(ctl, req, ctl->timeout_msec); |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 838 | |
| 839 | tb_cfg_request_put(req); |
| 840 | |
| 841 | return res; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 842 | } |
| 843 | |
| 844 | /** |
Mika Westerberg | 9c3db0b | 2021-01-28 13:05:55 +0300 | [diff] [blame] | 845 | * tb_cfg_read_raw() - read from config space into buffer |
| 846 | * @ctl: Pointer to the control channel |
| 847 | * @buffer: Buffer where the data is read |
| 848 | * @route: Route string of the router |
| 849 | * @port: Port number when reading from %TB_CFG_PORT, %0 otherwise |
| 850 | * @space: Config space selector |
| 851 | * @offset: Dword word offset of the register to start reading |
| 852 | * @length: Number of dwords to read |
| 853 | * @timeout_msec: Timeout in ms how long to wait for the response |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 854 | * |
Mika Westerberg | 9c3db0b | 2021-01-28 13:05:55 +0300 | [diff] [blame] | 855 | * Reads from router config space without translating the possible error. |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 856 | */ |
| 857 | struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer, |
| 858 | u64 route, u32 port, enum tb_cfg_space space, |
| 859 | u32 offset, u32 length, int timeout_msec) |
| 860 | { |
| 861 | struct tb_cfg_result res = { 0 }; |
| 862 | struct cfg_read_pkg request = { |
Mika Westerberg | 05c242e | 2017-06-06 15:25:08 +0300 | [diff] [blame] | 863 | .header = tb_cfg_make_header(route), |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 864 | .addr = { |
| 865 | .port = port, |
| 866 | .space = space, |
| 867 | .offset = offset, |
| 868 | .length = length, |
| 869 | }, |
| 870 | }; |
| 871 | struct cfg_write_pkg reply; |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 872 | int retries = 0; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 873 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 874 | while (retries < TB_CTL_RETRIES) { |
| 875 | struct tb_cfg_request *req; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 876 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 877 | req = tb_cfg_request_alloc(); |
| 878 | if (!req) { |
| 879 | res.err = -ENOMEM; |
| 880 | return res; |
| 881 | } |
| 882 | |
| 883 | request.addr.seq = retries++; |
| 884 | |
| 885 | req->match = tb_cfg_match; |
| 886 | req->copy = tb_cfg_copy; |
| 887 | req->request = &request; |
| 888 | req->request_size = sizeof(request); |
| 889 | req->request_type = TB_CFG_PKG_READ; |
| 890 | req->response = &reply; |
| 891 | req->response_size = 12 + 4 * length; |
| 892 | req->response_type = TB_CFG_PKG_READ; |
| 893 | |
| 894 | res = tb_cfg_request_sync(ctl, req, timeout_msec); |
| 895 | |
| 896 | tb_cfg_request_put(req); |
| 897 | |
| 898 | if (res.err != -ETIMEDOUT) |
| 899 | break; |
| 900 | |
| 901 | /* Wait a bit (arbitrary time) until we send a retry */ |
| 902 | usleep_range(10, 100); |
| 903 | } |
| 904 | |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 905 | if (res.err) |
| 906 | return res; |
| 907 | |
| 908 | res.response_port = reply.addr.port; |
| 909 | res.err = check_config_address(reply.addr, space, offset, length); |
| 910 | if (!res.err) |
| 911 | memcpy(buffer, &reply.data, 4 * length); |
| 912 | return res; |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * tb_cfg_write() - write from buffer into config space |
Mika Westerberg | 9c3db0b | 2021-01-28 13:05:55 +0300 | [diff] [blame] | 917 | * @ctl: Pointer to the control channel |
| 918 | * @buffer: Data to write |
| 919 | * @route: Route string of the router |
| 920 | * @port: Port number when writing to %TB_CFG_PORT, %0 otherwise |
| 921 | * @space: Config space selector |
| 922 | * @offset: Dword word offset of the register to start writing |
| 923 | * @length: Number of dwords to write |
| 924 | * @timeout_msec: Timeout in ms how long to wait for the response |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 925 | * |
Mika Westerberg | 9c3db0b | 2021-01-28 13:05:55 +0300 | [diff] [blame] | 926 | * Writes to router config space without translating the possible error. |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 927 | */ |
Mika Westerberg | 16a1258 | 2017-06-06 15:24:53 +0300 | [diff] [blame] | 928 | struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer, |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 929 | u64 route, u32 port, enum tb_cfg_space space, |
| 930 | u32 offset, u32 length, int timeout_msec) |
| 931 | { |
| 932 | struct tb_cfg_result res = { 0 }; |
| 933 | struct cfg_write_pkg request = { |
Mika Westerberg | 05c242e | 2017-06-06 15:25:08 +0300 | [diff] [blame] | 934 | .header = tb_cfg_make_header(route), |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 935 | .addr = { |
| 936 | .port = port, |
| 937 | .space = space, |
| 938 | .offset = offset, |
| 939 | .length = length, |
| 940 | }, |
| 941 | }; |
| 942 | struct cfg_read_pkg reply; |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 943 | int retries = 0; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 944 | |
| 945 | memcpy(&request.data, buffer, length * 4); |
| 946 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 947 | while (retries < TB_CTL_RETRIES) { |
| 948 | struct tb_cfg_request *req; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 949 | |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 950 | req = tb_cfg_request_alloc(); |
| 951 | if (!req) { |
| 952 | res.err = -ENOMEM; |
| 953 | return res; |
| 954 | } |
| 955 | |
| 956 | request.addr.seq = retries++; |
| 957 | |
| 958 | req->match = tb_cfg_match; |
| 959 | req->copy = tb_cfg_copy; |
| 960 | req->request = &request; |
| 961 | req->request_size = 12 + 4 * length; |
| 962 | req->request_type = TB_CFG_PKG_WRITE; |
| 963 | req->response = &reply; |
| 964 | req->response_size = sizeof(reply); |
| 965 | req->response_type = TB_CFG_PKG_WRITE; |
| 966 | |
| 967 | res = tb_cfg_request_sync(ctl, req, timeout_msec); |
| 968 | |
| 969 | tb_cfg_request_put(req); |
| 970 | |
| 971 | if (res.err != -ETIMEDOUT) |
| 972 | break; |
| 973 | |
| 974 | /* Wait a bit (arbitrary time) until we send a retry */ |
| 975 | usleep_range(10, 100); |
| 976 | } |
| 977 | |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 978 | if (res.err) |
| 979 | return res; |
| 980 | |
| 981 | res.response_port = reply.addr.port; |
| 982 | res.err = check_config_address(reply.addr, space, offset, length); |
| 983 | return res; |
| 984 | } |
| 985 | |
Mika Westerberg | d94dcbb | 2018-07-04 08:50:01 +0300 | [diff] [blame] | 986 | static int tb_cfg_get_error(struct tb_ctl *ctl, enum tb_cfg_space space, |
| 987 | const struct tb_cfg_result *res) |
| 988 | { |
| 989 | /* |
| 990 | * For unimplemented ports access to port config space may return |
| 991 | * TB_CFG_ERROR_INVALID_CONFIG_SPACE (alternatively their type is |
| 992 | * set to TB_TYPE_INACTIVE). In the former case return -ENODEV so |
| 993 | * that the caller can mark the port as disabled. |
| 994 | */ |
| 995 | if (space == TB_CFG_PORT && |
| 996 | res->tb_error == TB_CFG_ERROR_INVALID_CONFIG_SPACE) |
| 997 | return -ENODEV; |
| 998 | |
| 999 | tb_cfg_print_error(ctl, res); |
Mika Westerberg | 80e7c5d | 2019-09-19 15:22:20 +0300 | [diff] [blame] | 1000 | |
| 1001 | if (res->tb_error == TB_CFG_ERROR_LOCK) |
| 1002 | return -EACCES; |
Mika Westerberg | 463e48f | 2020-11-17 13:19:41 +0300 | [diff] [blame] | 1003 | else if (res->tb_error == TB_CFG_ERROR_PORT_NOT_CONNECTED) |
| 1004 | return -ENOTCONN; |
| 1005 | |
Mika Westerberg | d94dcbb | 2018-07-04 08:50:01 +0300 | [diff] [blame] | 1006 | return -EIO; |
| 1007 | } |
| 1008 | |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 1009 | int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, |
| 1010 | enum tb_cfg_space space, u32 offset, u32 length) |
| 1011 | { |
| 1012 | struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port, |
Mika Westerberg | 7f0a34d | 2020-12-29 13:44:57 +0200 | [diff] [blame] | 1013 | space, offset, length, ctl->timeout_msec); |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 1014 | switch (res.err) { |
| 1015 | case 0: |
| 1016 | /* Success */ |
| 1017 | break; |
| 1018 | |
| 1019 | case 1: |
| 1020 | /* Thunderbolt error, tb_error holds the actual number */ |
Mika Westerberg | d94dcbb | 2018-07-04 08:50:01 +0300 | [diff] [blame] | 1021 | return tb_cfg_get_error(ctl, space, &res); |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 1022 | |
| 1023 | case -ETIMEDOUT: |
Mika Westerberg | 68b9129 | 2019-03-22 14:28:11 +0200 | [diff] [blame] | 1024 | tb_ctl_warn(ctl, "%llx: timeout reading config space %u from %#x\n", |
| 1025 | route, space, offset); |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 1026 | break; |
| 1027 | |
| 1028 | default: |
| 1029 | WARN(1, "tb_cfg_read: %d\n", res.err); |
| 1030 | break; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 1031 | } |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 1032 | return res.err; |
| 1033 | } |
| 1034 | |
Mika Westerberg | 16a1258 | 2017-06-06 15:24:53 +0300 | [diff] [blame] | 1035 | int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port, |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 1036 | enum tb_cfg_space space, u32 offset, u32 length) |
| 1037 | { |
| 1038 | struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port, |
Mika Westerberg | 7f0a34d | 2020-12-29 13:44:57 +0200 | [diff] [blame] | 1039 | space, offset, length, ctl->timeout_msec); |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 1040 | switch (res.err) { |
| 1041 | case 0: |
| 1042 | /* Success */ |
| 1043 | break; |
| 1044 | |
| 1045 | case 1: |
| 1046 | /* Thunderbolt error, tb_error holds the actual number */ |
Mika Westerberg | d94dcbb | 2018-07-04 08:50:01 +0300 | [diff] [blame] | 1047 | return tb_cfg_get_error(ctl, space, &res); |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 1048 | |
| 1049 | case -ETIMEDOUT: |
Mika Westerberg | 68b9129 | 2019-03-22 14:28:11 +0200 | [diff] [blame] | 1050 | tb_ctl_warn(ctl, "%llx: timeout writing config space %u to %#x\n", |
| 1051 | route, space, offset); |
Mika Westerberg | d7f781b | 2017-06-06 15:25:10 +0300 | [diff] [blame] | 1052 | break; |
| 1053 | |
| 1054 | default: |
| 1055 | WARN(1, "tb_cfg_write: %d\n", res.err); |
| 1056 | break; |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 1057 | } |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 1058 | return res.err; |
| 1059 | } |
| 1060 | |
| 1061 | /** |
| 1062 | * tb_cfg_get_upstream_port() - get upstream port number of switch at route |
Mika Westerberg | 9c3db0b | 2021-01-28 13:05:55 +0300 | [diff] [blame] | 1063 | * @ctl: Pointer to the control channel |
| 1064 | * @route: Route string of the router |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 1065 | * |
| 1066 | * Reads the first dword from the switches TB_CFG_SWITCH config area and |
| 1067 | * returns the port number from which the reply originated. |
| 1068 | * |
| 1069 | * Return: Returns the upstream port number on success or an error code on |
| 1070 | * failure. |
| 1071 | */ |
| 1072 | int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route) |
| 1073 | { |
| 1074 | u32 dummy; |
| 1075 | struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0, |
| 1076 | TB_CFG_SWITCH, 0, 1, |
Mika Westerberg | 7f0a34d | 2020-12-29 13:44:57 +0200 | [diff] [blame] | 1077 | ctl->timeout_msec); |
Andreas Noever | f25bf6f | 2014-06-03 22:03:59 +0200 | [diff] [blame] | 1078 | if (res.err == 1) |
| 1079 | return -EIO; |
| 1080 | if (res.err) |
| 1081 | return res.err; |
| 1082 | return res.response_port; |
| 1083 | } |