0001
0002
0003
0004
0005
0006
0007
0008
0009 #include <linux/crc32.h>
0010 #include <linux/delay.h>
0011 #include <linux/slab.h>
0012 #include <linux/pci.h>
0013 #include <linux/dmapool.h>
0014 #include <linux/workqueue.h>
0015
0016 #include "ctl.h"
0017
0018
0019 #define TB_CTL_RX_PKG_COUNT 10
0020 #define TB_CTL_RETRIES 4
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036 struct tb_ctl {
0037 struct tb_nhi *nhi;
0038 struct tb_ring *tx;
0039 struct tb_ring *rx;
0040
0041 struct dma_pool *frame_pool;
0042 struct ctl_pkg *rx_packets[TB_CTL_RX_PKG_COUNT];
0043 struct mutex request_queue_lock;
0044 struct list_head request_queue;
0045 bool running;
0046
0047 int timeout_msec;
0048 event_cb callback;
0049 void *callback_data;
0050 };
0051
0052
0053 #define tb_ctl_WARN(ctl, format, arg...) \
0054 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
0055
0056 #define tb_ctl_err(ctl, format, arg...) \
0057 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
0058
0059 #define tb_ctl_warn(ctl, format, arg...) \
0060 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
0061
0062 #define tb_ctl_info(ctl, format, arg...) \
0063 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
0064
0065 #define tb_ctl_dbg(ctl, format, arg...) \
0066 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
0067
0068 static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue);
0069
0070 static DEFINE_MUTEX(tb_cfg_request_lock);
0071
0072
0073
0074
0075
0076
0077
0078 struct tb_cfg_request *tb_cfg_request_alloc(void)
0079 {
0080 struct tb_cfg_request *req;
0081
0082 req = kzalloc(sizeof(*req), GFP_KERNEL);
0083 if (!req)
0084 return NULL;
0085
0086 kref_init(&req->kref);
0087
0088 return req;
0089 }
0090
0091
0092
0093
0094
0095 void tb_cfg_request_get(struct tb_cfg_request *req)
0096 {
0097 mutex_lock(&tb_cfg_request_lock);
0098 kref_get(&req->kref);
0099 mutex_unlock(&tb_cfg_request_lock);
0100 }
0101
0102 static void tb_cfg_request_destroy(struct kref *kref)
0103 {
0104 struct tb_cfg_request *req = container_of(kref, typeof(*req), kref);
0105
0106 kfree(req);
0107 }
0108
0109
0110
0111
0112
0113
0114
0115
0116 void tb_cfg_request_put(struct tb_cfg_request *req)
0117 {
0118 mutex_lock(&tb_cfg_request_lock);
0119 kref_put(&req->kref, tb_cfg_request_destroy);
0120 mutex_unlock(&tb_cfg_request_lock);
0121 }
0122
0123 static int tb_cfg_request_enqueue(struct tb_ctl *ctl,
0124 struct tb_cfg_request *req)
0125 {
0126 WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags));
0127 WARN_ON(req->ctl);
0128
0129 mutex_lock(&ctl->request_queue_lock);
0130 if (!ctl->running) {
0131 mutex_unlock(&ctl->request_queue_lock);
0132 return -ENOTCONN;
0133 }
0134 req->ctl = ctl;
0135 list_add_tail(&req->list, &ctl->request_queue);
0136 set_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
0137 mutex_unlock(&ctl->request_queue_lock);
0138 return 0;
0139 }
0140
0141 static void tb_cfg_request_dequeue(struct tb_cfg_request *req)
0142 {
0143 struct tb_ctl *ctl = req->ctl;
0144
0145 mutex_lock(&ctl->request_queue_lock);
0146 list_del(&req->list);
0147 clear_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
0148 if (test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
0149 wake_up(&tb_cfg_request_cancel_queue);
0150 mutex_unlock(&ctl->request_queue_lock);
0151 }
0152
0153 static bool tb_cfg_request_is_active(struct tb_cfg_request *req)
0154 {
0155 return test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags);
0156 }
0157
0158 static struct tb_cfg_request *
0159 tb_cfg_request_find(struct tb_ctl *ctl, struct ctl_pkg *pkg)
0160 {
0161 struct tb_cfg_request *req = NULL, *iter;
0162
0163 mutex_lock(&pkg->ctl->request_queue_lock);
0164 list_for_each_entry(iter, &pkg->ctl->request_queue, list) {
0165 tb_cfg_request_get(iter);
0166 if (iter->match(iter, pkg)) {
0167 req = iter;
0168 break;
0169 }
0170 tb_cfg_request_put(iter);
0171 }
0172 mutex_unlock(&pkg->ctl->request_queue_lock);
0173
0174 return req;
0175 }
0176
0177
0178
0179
0180 static int check_header(const struct ctl_pkg *pkg, u32 len,
0181 enum tb_cfg_pkg_type type, u64 route)
0182 {
0183 struct tb_cfg_header *header = pkg->buffer;
0184
0185
0186 if (WARN(len != pkg->frame.size,
0187 "wrong framesize (expected %#x, got %#x)\n",
0188 len, pkg->frame.size))
0189 return -EIO;
0190 if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n",
0191 type, pkg->frame.eof))
0192 return -EIO;
0193 if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n",
0194 pkg->frame.sof))
0195 return -EIO;
0196
0197
0198 if (WARN(header->unknown != 1 << 9,
0199 "header->unknown is %#x\n", header->unknown))
0200 return -EIO;
0201 if (WARN(route != tb_cfg_get_route(header),
0202 "wrong route (expected %llx, got %llx)",
0203 route, tb_cfg_get_route(header)))
0204 return -EIO;
0205 return 0;
0206 }
0207
0208 static int check_config_address(struct tb_cfg_address addr,
0209 enum tb_cfg_space space, u32 offset,
0210 u32 length)
0211 {
0212 if (WARN(addr.zero, "addr.zero is %#x\n", addr.zero))
0213 return -EIO;
0214 if (WARN(space != addr.space, "wrong space (expected %x, got %x\n)",
0215 space, addr.space))
0216 return -EIO;
0217 if (WARN(offset != addr.offset, "wrong offset (expected %x, got %x\n)",
0218 offset, addr.offset))
0219 return -EIO;
0220 if (WARN(length != addr.length, "wrong space (expected %x, got %x\n)",
0221 length, addr.length))
0222 return -EIO;
0223
0224
0225
0226
0227 return 0;
0228 }
0229
0230 static struct tb_cfg_result decode_error(const struct ctl_pkg *response)
0231 {
0232 struct cfg_error_pkg *pkg = response->buffer;
0233 struct tb_ctl *ctl = response->ctl;
0234 struct tb_cfg_result res = { 0 };
0235 res.response_route = tb_cfg_get_route(&pkg->header);
0236 res.response_port = 0;
0237 res.err = check_header(response, sizeof(*pkg), TB_CFG_PKG_ERROR,
0238 tb_cfg_get_route(&pkg->header));
0239 if (res.err)
0240 return res;
0241
0242 if (pkg->zero1)
0243 tb_ctl_warn(ctl, "pkg->zero1 is %#x\n", pkg->zero1);
0244 if (pkg->zero2)
0245 tb_ctl_warn(ctl, "pkg->zero2 is %#x\n", pkg->zero2);
0246 if (pkg->zero3)
0247 tb_ctl_warn(ctl, "pkg->zero3 is %#x\n", pkg->zero3);
0248
0249 res.err = 1;
0250 res.tb_error = pkg->error;
0251 res.response_port = pkg->port;
0252 return res;
0253
0254 }
0255
0256 static struct tb_cfg_result parse_header(const struct ctl_pkg *pkg, u32 len,
0257 enum tb_cfg_pkg_type type, u64 route)
0258 {
0259 struct tb_cfg_header *header = pkg->buffer;
0260 struct tb_cfg_result res = { 0 };
0261
0262 if (pkg->frame.eof == TB_CFG_PKG_ERROR)
0263 return decode_error(pkg);
0264
0265 res.response_port = 0;
0266 res.response_route = tb_cfg_get_route(header);
0267 res.err = check_header(pkg, len, type, route);
0268 return res;
0269 }
0270
0271 static void tb_cfg_print_error(struct tb_ctl *ctl,
0272 const struct tb_cfg_result *res)
0273 {
0274 WARN_ON(res->err != 1);
0275 switch (res->tb_error) {
0276 case TB_CFG_ERROR_PORT_NOT_CONNECTED:
0277
0278
0279 return;
0280 case TB_CFG_ERROR_INVALID_CONFIG_SPACE:
0281
0282
0283
0284
0285 tb_ctl_dbg(ctl, "%llx:%x: invalid config space or offset\n",
0286 res->response_route, res->response_port);
0287 return;
0288 case TB_CFG_ERROR_NO_SUCH_PORT:
0289
0290
0291
0292
0293
0294 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Invalid port\n",
0295 res->response_route, res->response_port);
0296 return;
0297 case TB_CFG_ERROR_LOOP:
0298 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Route contains a loop\n",
0299 res->response_route, res->response_port);
0300 return;
0301 case TB_CFG_ERROR_LOCK:
0302 tb_ctl_warn(ctl, "%llx:%x: downstream port is locked\n",
0303 res->response_route, res->response_port);
0304 return;
0305 default:
0306
0307 tb_ctl_WARN(ctl, "CFG_ERROR(%llx:%x): Unknown error\n",
0308 res->response_route, res->response_port);
0309 return;
0310 }
0311 }
0312
0313 static __be32 tb_crc(const void *data, size_t len)
0314 {
0315 return cpu_to_be32(~__crc32c_le(~0, data, len));
0316 }
0317
0318 static void tb_ctl_pkg_free(struct ctl_pkg *pkg)
0319 {
0320 if (pkg) {
0321 dma_pool_free(pkg->ctl->frame_pool,
0322 pkg->buffer, pkg->frame.buffer_phy);
0323 kfree(pkg);
0324 }
0325 }
0326
0327 static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl)
0328 {
0329 struct ctl_pkg *pkg = kzalloc(sizeof(*pkg), GFP_KERNEL);
0330 if (!pkg)
0331 return NULL;
0332 pkg->ctl = ctl;
0333 pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL,
0334 &pkg->frame.buffer_phy);
0335 if (!pkg->buffer) {
0336 kfree(pkg);
0337 return NULL;
0338 }
0339 return pkg;
0340 }
0341
0342
0343
0344
0345 static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame,
0346 bool canceled)
0347 {
0348 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
0349 tb_ctl_pkg_free(pkg);
0350 }
0351
0352
0353
0354
0355
0356
0357
0358
0359 static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len,
0360 enum tb_cfg_pkg_type type)
0361 {
0362 int res;
0363 struct ctl_pkg *pkg;
0364 if (len % 4 != 0) {
0365 tb_ctl_WARN(ctl, "TX: invalid size: %zu\n", len);
0366 return -EINVAL;
0367 }
0368 if (len > TB_FRAME_SIZE - 4) {
0369 tb_ctl_WARN(ctl, "TX: packet too large: %zu/%d\n",
0370 len, TB_FRAME_SIZE - 4);
0371 return -EINVAL;
0372 }
0373 pkg = tb_ctl_pkg_alloc(ctl);
0374 if (!pkg)
0375 return -ENOMEM;
0376 pkg->frame.callback = tb_ctl_tx_callback;
0377 pkg->frame.size = len + 4;
0378 pkg->frame.sof = type;
0379 pkg->frame.eof = type;
0380 cpu_to_be32_array(pkg->buffer, data, len / 4);
0381 *(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len);
0382
0383 res = tb_ring_tx(ctl->tx, &pkg->frame);
0384 if (res)
0385 tb_ctl_pkg_free(pkg);
0386 return res;
0387 }
0388
0389
0390
0391
0392 static bool tb_ctl_handle_event(struct tb_ctl *ctl, enum tb_cfg_pkg_type type,
0393 struct ctl_pkg *pkg, size_t size)
0394 {
0395 return ctl->callback(ctl->callback_data, type, pkg->buffer, size);
0396 }
0397
0398 static void tb_ctl_rx_submit(struct ctl_pkg *pkg)
0399 {
0400 tb_ring_rx(pkg->ctl->rx, &pkg->frame);
0401
0402
0403
0404
0405
0406 }
0407
0408 static int tb_async_error(const struct ctl_pkg *pkg)
0409 {
0410 const struct cfg_error_pkg *error = pkg->buffer;
0411
0412 if (pkg->frame.eof != TB_CFG_PKG_ERROR)
0413 return false;
0414
0415 switch (error->error) {
0416 case TB_CFG_ERROR_LINK_ERROR:
0417 case TB_CFG_ERROR_HEC_ERROR_DETECTED:
0418 case TB_CFG_ERROR_FLOW_CONTROL_ERROR:
0419 return true;
0420
0421 default:
0422 return false;
0423 }
0424 }
0425
0426 static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame,
0427 bool canceled)
0428 {
0429 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame);
0430 struct tb_cfg_request *req;
0431 __be32 crc32;
0432
0433 if (canceled)
0434 return;
0435
0436
0437
0438
0439 if (frame->size < 4 || frame->size % 4 != 0) {
0440 tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n",
0441 frame->size);
0442 goto rx;
0443 }
0444
0445 frame->size -= 4;
0446 crc32 = tb_crc(pkg->buffer, frame->size);
0447 be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4);
0448
0449 switch (frame->eof) {
0450 case TB_CFG_PKG_READ:
0451 case TB_CFG_PKG_WRITE:
0452 case TB_CFG_PKG_ERROR:
0453 case TB_CFG_PKG_OVERRIDE:
0454 case TB_CFG_PKG_RESET:
0455 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
0456 tb_ctl_err(pkg->ctl,
0457 "RX: checksum mismatch, dropping packet\n");
0458 goto rx;
0459 }
0460 if (tb_async_error(pkg)) {
0461 tb_ctl_handle_event(pkg->ctl, frame->eof,
0462 pkg, frame->size);
0463 goto rx;
0464 }
0465 break;
0466
0467 case TB_CFG_PKG_EVENT:
0468 case TB_CFG_PKG_XDOMAIN_RESP:
0469 case TB_CFG_PKG_XDOMAIN_REQ:
0470 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) {
0471 tb_ctl_err(pkg->ctl,
0472 "RX: checksum mismatch, dropping packet\n");
0473 goto rx;
0474 }
0475 fallthrough;
0476 case TB_CFG_PKG_ICM_EVENT:
0477 if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size))
0478 goto rx;
0479 break;
0480
0481 default:
0482 break;
0483 }
0484
0485
0486
0487
0488
0489
0490
0491 req = tb_cfg_request_find(pkg->ctl, pkg);
0492 if (req) {
0493 if (req->copy(req, pkg))
0494 schedule_work(&req->work);
0495 tb_cfg_request_put(req);
0496 }
0497
0498 rx:
0499 tb_ctl_rx_submit(pkg);
0500 }
0501
0502 static void tb_cfg_request_work(struct work_struct *work)
0503 {
0504 struct tb_cfg_request *req = container_of(work, typeof(*req), work);
0505
0506 if (!test_bit(TB_CFG_REQUEST_CANCELED, &req->flags))
0507 req->callback(req->callback_data);
0508
0509 tb_cfg_request_dequeue(req);
0510 tb_cfg_request_put(req);
0511 }
0512
0513
0514
0515
0516
0517
0518
0519
0520
0521
0522
0523 int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
0524 void (*callback)(void *), void *callback_data)
0525 {
0526 int ret;
0527
0528 req->flags = 0;
0529 req->callback = callback;
0530 req->callback_data = callback_data;
0531 INIT_WORK(&req->work, tb_cfg_request_work);
0532 INIT_LIST_HEAD(&req->list);
0533
0534 tb_cfg_request_get(req);
0535 ret = tb_cfg_request_enqueue(ctl, req);
0536 if (ret)
0537 goto err_put;
0538
0539 ret = tb_ctl_tx(ctl, req->request, req->request_size,
0540 req->request_type);
0541 if (ret)
0542 goto err_dequeue;
0543
0544 if (!req->response)
0545 schedule_work(&req->work);
0546
0547 return 0;
0548
0549 err_dequeue:
0550 tb_cfg_request_dequeue(req);
0551 err_put:
0552 tb_cfg_request_put(req);
0553
0554 return ret;
0555 }
0556
0557
0558
0559
0560
0561
0562
0563
0564
0565 void tb_cfg_request_cancel(struct tb_cfg_request *req, int err)
0566 {
0567 set_bit(TB_CFG_REQUEST_CANCELED, &req->flags);
0568 schedule_work(&req->work);
0569 wait_event(tb_cfg_request_cancel_queue, !tb_cfg_request_is_active(req));
0570 req->result.err = err;
0571 }
0572
0573 static void tb_cfg_request_complete(void *data)
0574 {
0575 complete(data);
0576 }
0577
0578
0579
0580
0581
0582
0583
0584
0585
0586
0587
0588
0589 struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
0590 struct tb_cfg_request *req,
0591 int timeout_msec)
0592 {
0593 unsigned long timeout = msecs_to_jiffies(timeout_msec);
0594 struct tb_cfg_result res = { 0 };
0595 DECLARE_COMPLETION_ONSTACK(done);
0596 int ret;
0597
0598 ret = tb_cfg_request(ctl, req, tb_cfg_request_complete, &done);
0599 if (ret) {
0600 res.err = ret;
0601 return res;
0602 }
0603
0604 if (!wait_for_completion_timeout(&done, timeout))
0605 tb_cfg_request_cancel(req, -ETIMEDOUT);
0606
0607 flush_work(&req->work);
0608
0609 return req->result;
0610 }
0611
0612
0613
0614
0615
0616
0617
0618
0619
0620
0621
0622
0623
0624
0625 struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, int timeout_msec, event_cb cb,
0626 void *cb_data)
0627 {
0628 int i;
0629 struct tb_ctl *ctl = kzalloc(sizeof(*ctl), GFP_KERNEL);
0630 if (!ctl)
0631 return NULL;
0632 ctl->nhi = nhi;
0633 ctl->timeout_msec = timeout_msec;
0634 ctl->callback = cb;
0635 ctl->callback_data = cb_data;
0636
0637 mutex_init(&ctl->request_queue_lock);
0638 INIT_LIST_HEAD(&ctl->request_queue);
0639 ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev,
0640 TB_FRAME_SIZE, 4, 0);
0641 if (!ctl->frame_pool)
0642 goto err;
0643
0644 ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND);
0645 if (!ctl->tx)
0646 goto err;
0647
0648 ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0, 0xffff,
0649 0xffff, NULL, NULL);
0650 if (!ctl->rx)
0651 goto err;
0652
0653 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++) {
0654 ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl);
0655 if (!ctl->rx_packets[i])
0656 goto err;
0657 ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback;
0658 }
0659
0660 tb_ctl_dbg(ctl, "control channel created\n");
0661 return ctl;
0662 err:
0663 tb_ctl_free(ctl);
0664 return NULL;
0665 }
0666
0667
0668
0669
0670
0671
0672
0673
0674
0675 void tb_ctl_free(struct tb_ctl *ctl)
0676 {
0677 int i;
0678
0679 if (!ctl)
0680 return;
0681
0682 if (ctl->rx)
0683 tb_ring_free(ctl->rx);
0684 if (ctl->tx)
0685 tb_ring_free(ctl->tx);
0686
0687
0688 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
0689 tb_ctl_pkg_free(ctl->rx_packets[i]);
0690
0691
0692 dma_pool_destroy(ctl->frame_pool);
0693 kfree(ctl);
0694 }
0695
0696
0697
0698
0699
0700 void tb_ctl_start(struct tb_ctl *ctl)
0701 {
0702 int i;
0703 tb_ctl_dbg(ctl, "control channel starting...\n");
0704 tb_ring_start(ctl->tx);
0705 tb_ring_start(ctl->rx);
0706 for (i = 0; i < TB_CTL_RX_PKG_COUNT; i++)
0707 tb_ctl_rx_submit(ctl->rx_packets[i]);
0708
0709 ctl->running = true;
0710 }
0711
0712
0713
0714
0715
0716
0717
0718
0719
0720
0721 void tb_ctl_stop(struct tb_ctl *ctl)
0722 {
0723 mutex_lock(&ctl->request_queue_lock);
0724 ctl->running = false;
0725 mutex_unlock(&ctl->request_queue_lock);
0726
0727 tb_ring_stop(ctl->rx);
0728 tb_ring_stop(ctl->tx);
0729
0730 if (!list_empty(&ctl->request_queue))
0731 tb_ctl_WARN(ctl, "dangling request in request_queue\n");
0732 INIT_LIST_HEAD(&ctl->request_queue);
0733 tb_ctl_dbg(ctl, "control channel stopped\n");
0734 }
0735
0736
0737
0738
0739
0740
0741
0742
0743
0744
0745
0746
0747
0748 int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug)
0749 {
0750 struct cfg_error_pkg pkg = {
0751 .header = tb_cfg_make_header(route),
0752 .port = port,
0753 .error = TB_CFG_ERROR_ACK_PLUG_EVENT,
0754 .pg = unplug ? TB_CFG_ERROR_PG_HOT_UNPLUG
0755 : TB_CFG_ERROR_PG_HOT_PLUG,
0756 };
0757 tb_ctl_dbg(ctl, "acking hot %splug event on %llx:%x\n",
0758 unplug ? "un" : "", route, port);
0759 return tb_ctl_tx(ctl, &pkg, sizeof(pkg), TB_CFG_PKG_ERROR);
0760 }
0761
0762 static bool tb_cfg_match(const struct tb_cfg_request *req,
0763 const struct ctl_pkg *pkg)
0764 {
0765 u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63);
0766
0767 if (pkg->frame.eof == TB_CFG_PKG_ERROR)
0768 return true;
0769
0770 if (pkg->frame.eof != req->response_type)
0771 return false;
0772 if (route != tb_cfg_get_route(req->request))
0773 return false;
0774 if (pkg->frame.size != req->response_size)
0775 return false;
0776
0777 if (pkg->frame.eof == TB_CFG_PKG_READ ||
0778 pkg->frame.eof == TB_CFG_PKG_WRITE) {
0779 const struct cfg_read_pkg *req_hdr = req->request;
0780 const struct cfg_read_pkg *res_hdr = pkg->buffer;
0781
0782 if (req_hdr->addr.seq != res_hdr->addr.seq)
0783 return false;
0784 }
0785
0786 return true;
0787 }
0788
0789 static bool tb_cfg_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg)
0790 {
0791 struct tb_cfg_result res;
0792
0793
0794 res = parse_header(pkg, req->response_size, req->response_type,
0795 tb_cfg_get_route(req->request));
0796 if (!res.err)
0797 memcpy(req->response, pkg->buffer, req->response_size);
0798
0799 req->result = res;
0800
0801
0802 return true;
0803 }
0804
0805
0806
0807
0808
0809
0810
0811
0812
0813
0814 struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route)
0815 {
0816 struct cfg_reset_pkg request = { .header = tb_cfg_make_header(route) };
0817 struct tb_cfg_result res = { 0 };
0818 struct tb_cfg_header reply;
0819 struct tb_cfg_request *req;
0820
0821 req = tb_cfg_request_alloc();
0822 if (!req) {
0823 res.err = -ENOMEM;
0824 return res;
0825 }
0826
0827 req->match = tb_cfg_match;
0828 req->copy = tb_cfg_copy;
0829 req->request = &request;
0830 req->request_size = sizeof(request);
0831 req->request_type = TB_CFG_PKG_RESET;
0832 req->response = &reply;
0833 req->response_size = sizeof(reply);
0834 req->response_type = TB_CFG_PKG_RESET;
0835
0836 res = tb_cfg_request_sync(ctl, req, ctl->timeout_msec);
0837
0838 tb_cfg_request_put(req);
0839
0840 return res;
0841 }
0842
0843
0844
0845
0846
0847
0848
0849
0850
0851
0852
0853
0854
0855
0856 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
0857 u64 route, u32 port, enum tb_cfg_space space,
0858 u32 offset, u32 length, int timeout_msec)
0859 {
0860 struct tb_cfg_result res = { 0 };
0861 struct cfg_read_pkg request = {
0862 .header = tb_cfg_make_header(route),
0863 .addr = {
0864 .port = port,
0865 .space = space,
0866 .offset = offset,
0867 .length = length,
0868 },
0869 };
0870 struct cfg_write_pkg reply;
0871 int retries = 0;
0872
0873 while (retries < TB_CTL_RETRIES) {
0874 struct tb_cfg_request *req;
0875
0876 req = tb_cfg_request_alloc();
0877 if (!req) {
0878 res.err = -ENOMEM;
0879 return res;
0880 }
0881
0882 request.addr.seq = retries++;
0883
0884 req->match = tb_cfg_match;
0885 req->copy = tb_cfg_copy;
0886 req->request = &request;
0887 req->request_size = sizeof(request);
0888 req->request_type = TB_CFG_PKG_READ;
0889 req->response = &reply;
0890 req->response_size = 12 + 4 * length;
0891 req->response_type = TB_CFG_PKG_READ;
0892
0893 res = tb_cfg_request_sync(ctl, req, timeout_msec);
0894
0895 tb_cfg_request_put(req);
0896
0897 if (res.err != -ETIMEDOUT)
0898 break;
0899
0900
0901 usleep_range(10, 100);
0902 }
0903
0904 if (res.err)
0905 return res;
0906
0907 res.response_port = reply.addr.port;
0908 res.err = check_config_address(reply.addr, space, offset, length);
0909 if (!res.err)
0910 memcpy(buffer, &reply.data, 4 * length);
0911 return res;
0912 }
0913
0914
0915
0916
0917
0918
0919
0920
0921
0922
0923
0924
0925
0926
0927 struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
0928 u64 route, u32 port, enum tb_cfg_space space,
0929 u32 offset, u32 length, int timeout_msec)
0930 {
0931 struct tb_cfg_result res = { 0 };
0932 struct cfg_write_pkg request = {
0933 .header = tb_cfg_make_header(route),
0934 .addr = {
0935 .port = port,
0936 .space = space,
0937 .offset = offset,
0938 .length = length,
0939 },
0940 };
0941 struct cfg_read_pkg reply;
0942 int retries = 0;
0943
0944 memcpy(&request.data, buffer, length * 4);
0945
0946 while (retries < TB_CTL_RETRIES) {
0947 struct tb_cfg_request *req;
0948
0949 req = tb_cfg_request_alloc();
0950 if (!req) {
0951 res.err = -ENOMEM;
0952 return res;
0953 }
0954
0955 request.addr.seq = retries++;
0956
0957 req->match = tb_cfg_match;
0958 req->copy = tb_cfg_copy;
0959 req->request = &request;
0960 req->request_size = 12 + 4 * length;
0961 req->request_type = TB_CFG_PKG_WRITE;
0962 req->response = &reply;
0963 req->response_size = sizeof(reply);
0964 req->response_type = TB_CFG_PKG_WRITE;
0965
0966 res = tb_cfg_request_sync(ctl, req, timeout_msec);
0967
0968 tb_cfg_request_put(req);
0969
0970 if (res.err != -ETIMEDOUT)
0971 break;
0972
0973
0974 usleep_range(10, 100);
0975 }
0976
0977 if (res.err)
0978 return res;
0979
0980 res.response_port = reply.addr.port;
0981 res.err = check_config_address(reply.addr, space, offset, length);
0982 return res;
0983 }
0984
0985 static int tb_cfg_get_error(struct tb_ctl *ctl, enum tb_cfg_space space,
0986 const struct tb_cfg_result *res)
0987 {
0988
0989
0990
0991
0992
0993
0994 if (space == TB_CFG_PORT &&
0995 res->tb_error == TB_CFG_ERROR_INVALID_CONFIG_SPACE)
0996 return -ENODEV;
0997
0998 tb_cfg_print_error(ctl, res);
0999
1000 if (res->tb_error == TB_CFG_ERROR_LOCK)
1001 return -EACCES;
1002 else if (res->tb_error == TB_CFG_ERROR_PORT_NOT_CONNECTED)
1003 return -ENOTCONN;
1004
1005 return -EIO;
1006 }
1007
1008 int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
1009 enum tb_cfg_space space, u32 offset, u32 length)
1010 {
1011 struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port,
1012 space, offset, length, ctl->timeout_msec);
1013 switch (res.err) {
1014 case 0:
1015
1016 break;
1017
1018 case 1:
1019
1020 return tb_cfg_get_error(ctl, space, &res);
1021
1022 case -ETIMEDOUT:
1023 tb_ctl_warn(ctl, "%llx: timeout reading config space %u from %#x\n",
1024 route, space, offset);
1025 break;
1026
1027 default:
1028 WARN(1, "tb_cfg_read: %d\n", res.err);
1029 break;
1030 }
1031 return res.err;
1032 }
1033
1034 int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
1035 enum tb_cfg_space space, u32 offset, u32 length)
1036 {
1037 struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port,
1038 space, offset, length, ctl->timeout_msec);
1039 switch (res.err) {
1040 case 0:
1041
1042 break;
1043
1044 case 1:
1045
1046 return tb_cfg_get_error(ctl, space, &res);
1047
1048 case -ETIMEDOUT:
1049 tb_ctl_warn(ctl, "%llx: timeout writing config space %u to %#x\n",
1050 route, space, offset);
1051 break;
1052
1053 default:
1054 WARN(1, "tb_cfg_write: %d\n", res.err);
1055 break;
1056 }
1057 return res.err;
1058 }
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071 int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route)
1072 {
1073 u32 dummy;
1074 struct tb_cfg_result res = tb_cfg_read_raw(ctl, &dummy, route, 0,
1075 TB_CFG_SWITCH, 0, 1,
1076 ctl->timeout_msec);
1077 if (res.err == 1)
1078 return -EIO;
1079 if (res.err)
1080 return res.err;
1081 return res.response_port;
1082 }