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