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