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