Lines Matching +full:frame +full:- +full:buffer

1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - control channel and configuration commands
25 * struct tb_ctl - Thunderbolt control channel
34 * @timeout_msec: Default timeout for non-raw control messages
59 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
62 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
65 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
68 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
71 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
74 dev_dbg_once(&(ctl)->nhi->pdev->dev, format, ## arg)
81 * tb_cfg_request_alloc() - Allocates a new config request
94 kref_init(&req->kref); in tb_cfg_request_alloc()
100 * tb_cfg_request_get() - Increase refcount of a request
106 kref_get(&req->kref); in tb_cfg_request_get()
118 * tb_cfg_request_put() - Decrease refcount and possibly release the request
127 kref_put(&req->kref, tb_cfg_request_destroy); in tb_cfg_request_put()
134 WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags)); in tb_cfg_request_enqueue()
135 WARN_ON(req->ctl); in tb_cfg_request_enqueue()
137 mutex_lock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
138 if (!ctl->running) { in tb_cfg_request_enqueue()
139 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
140 return -ENOTCONN; in tb_cfg_request_enqueue()
142 req->ctl = ctl; in tb_cfg_request_enqueue()
143 list_add_tail(&req->list, &ctl->request_queue); in tb_cfg_request_enqueue()
144 set_bit(TB_CFG_REQUEST_ACTIVE, &req->flags); in tb_cfg_request_enqueue()
145 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
151 struct tb_ctl *ctl = req->ctl; in tb_cfg_request_dequeue()
153 mutex_lock(&ctl->request_queue_lock); in tb_cfg_request_dequeue()
154 list_del(&req->list); in tb_cfg_request_dequeue()
155 clear_bit(TB_CFG_REQUEST_ACTIVE, &req->flags); in tb_cfg_request_dequeue()
156 if (test_bit(TB_CFG_REQUEST_CANCELED, &req->flags)) in tb_cfg_request_dequeue()
158 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_dequeue()
163 return test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags); in tb_cfg_request_is_active()
171 mutex_lock(&pkg->ctl->request_queue_lock); in tb_cfg_request_find()
172 list_for_each_entry(iter, &pkg->ctl->request_queue, list) { in tb_cfg_request_find()
174 if (iter->match(iter, pkg)) { in tb_cfg_request_find()
180 mutex_unlock(&pkg->ctl->request_queue_lock); in tb_cfg_request_find()
191 struct tb_cfg_header *header = pkg->buffer; in check_header()
193 /* check frame, TODO: frame flags */ in check_header()
194 if (WARN(len != pkg->frame.size, in check_header()
196 len, pkg->frame.size)) in check_header()
197 return -EIO; in check_header()
198 if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n", in check_header()
199 type, pkg->frame.eof)) in check_header()
200 return -EIO; in check_header()
201 if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n", in check_header()
202 pkg->frame.sof)) in check_header()
203 return -EIO; in check_header()
206 if (WARN(header->unknown != 1 << 9, in check_header()
207 "header->unknown is %#x\n", header->unknown)) in check_header()
208 return -EIO; in check_header()
212 return -EIO; in check_header()
221 return -EIO; in check_config_address()
224 return -EIO; in check_config_address()
227 return -EIO; in check_config_address()
230 return -EIO; in check_config_address()
232 * We cannot check addr->port as it is set to the upstream port of the in check_config_address()
240 struct cfg_error_pkg *pkg = response->buffer; in decode_error()
242 res.response_route = tb_cfg_get_route(&pkg->header); in decode_error()
245 tb_cfg_get_route(&pkg->header)); in decode_error()
250 res.tb_error = pkg->error; in decode_error()
251 res.response_port = pkg->port; in decode_error()
259 struct tb_cfg_header *header = pkg->buffer; in parse_header()
262 if (pkg->frame.eof == TB_CFG_PKG_ERROR) in parse_header()
274 WARN_ON(res->err != 1); in tb_cfg_print_error()
275 switch (res->tb_error) { in tb_cfg_print_error()
286 res->response_route, res->response_port, space); in tb_cfg_print_error()
290 * - The route contains a non-existent port. in tb_cfg_print_error()
291 * - The route contains a non-PHY port (e.g. PCIe). in tb_cfg_print_error()
292 * - The port in cfg_read/cfg_write does not exist. in tb_cfg_print_error()
295 res->response_route, res->response_port); in tb_cfg_print_error()
299 res->response_route, res->response_port); in tb_cfg_print_error()
303 res->response_route, res->response_port); in tb_cfg_print_error()
308 res->response_route, res->response_port); in tb_cfg_print_error()
321 dma_pool_free(pkg->ctl->frame_pool, in tb_ctl_pkg_free()
322 pkg->buffer, pkg->frame.buffer_phy); in tb_ctl_pkg_free()
332 pkg->ctl = ctl; in tb_ctl_pkg_alloc()
333 pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL, in tb_ctl_pkg_alloc()
334 &pkg->frame.buffer_phy); in tb_ctl_pkg_alloc()
335 if (!pkg->buffer) { in tb_ctl_pkg_alloc()
345 static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame, in tb_ctl_tx_callback() argument
348 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame); in tb_ctl_tx_callback()
353 * tb_cfg_tx() - transmit a packet on the control channel
364 if (len % 4 != 0) { /* required for le->be conversion */ in tb_ctl_tx()
366 return -EINVAL; in tb_ctl_tx()
368 if (len > TB_FRAME_SIZE - 4) { /* checksum is 4 bytes */ in tb_ctl_tx()
370 len, TB_FRAME_SIZE - 4); in tb_ctl_tx()
371 return -EINVAL; in tb_ctl_tx()
375 return -ENOMEM; in tb_ctl_tx()
376 pkg->frame.callback = tb_ctl_tx_callback; in tb_ctl_tx()
377 pkg->frame.size = len + 4; in tb_ctl_tx()
378 pkg->frame.sof = type; in tb_ctl_tx()
379 pkg->frame.eof = type; in tb_ctl_tx()
381 trace_tb_tx(ctl->index, type, data, len); in tb_ctl_tx()
383 cpu_to_be32_array(pkg->buffer, data, len / 4); in tb_ctl_tx()
384 *(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len); in tb_ctl_tx()
386 res = tb_ring_tx(ctl->tx, &pkg->frame); in tb_ctl_tx()
393 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
398 trace_tb_event(ctl->index, type, pkg->buffer, size); in tb_ctl_handle_event()
399 return ctl->callback(ctl->callback_data, type, pkg->buffer, size); in tb_ctl_handle_event()
404 tb_ring_rx(pkg->ctl->rx, &pkg->frame); /* in tb_ctl_rx_submit()
407 * from ctl->rx_packets, so we do in tb_ctl_rx_submit()
414 const struct cfg_error_pkg *error = pkg->buffer; in tb_async_error()
416 if (pkg->frame.eof != TB_CFG_PKG_ERROR) in tb_async_error()
419 switch (error->error) { in tb_async_error()
438 static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame, in tb_ctl_rx_callback() argument
441 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame); in tb_ctl_rx_callback()
448 * ctl->rx_packets. in tb_ctl_rx_callback()
451 if (frame->size < 4 || frame->size % 4 != 0) { in tb_ctl_rx_callback()
452 tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n", in tb_ctl_rx_callback()
453 frame->size); in tb_ctl_rx_callback()
457 frame->size -= 4; /* remove checksum */ in tb_ctl_rx_callback()
458 crc32 = tb_crc(pkg->buffer, frame->size); in tb_ctl_rx_callback()
459 be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4); in tb_ctl_rx_callback()
461 switch (frame->eof) { in tb_ctl_rx_callback()
467 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) { in tb_ctl_rx_callback()
468 tb_ctl_err(pkg->ctl, in tb_ctl_rx_callback()
473 tb_ctl_handle_event(pkg->ctl, frame->eof, in tb_ctl_rx_callback()
474 pkg, frame->size); in tb_ctl_rx_callback()
482 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) { in tb_ctl_rx_callback()
483 tb_ctl_err(pkg->ctl, in tb_ctl_rx_callback()
489 if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size)) in tb_ctl_rx_callback()
503 req = tb_cfg_request_find(pkg->ctl, pkg); in tb_ctl_rx_callback()
505 trace_tb_rx(pkg->ctl->index, frame->eof, pkg->buffer, frame->size, !req); in tb_ctl_rx_callback()
508 if (req->copy(req, pkg)) in tb_ctl_rx_callback()
509 schedule_work(&req->work); in tb_ctl_rx_callback()
521 if (!test_bit(TB_CFG_REQUEST_CANCELED, &req->flags)) in tb_cfg_request_work()
522 req->callback(req->callback_data); in tb_cfg_request_work()
529 * tb_cfg_request() - Start control request not waiting for it to complete
543 req->flags = 0; in tb_cfg_request()
544 req->callback = callback; in tb_cfg_request()
545 req->callback_data = callback_data; in tb_cfg_request()
546 INIT_WORK(&req->work, tb_cfg_request_work); in tb_cfg_request()
547 INIT_LIST_HEAD(&req->list); in tb_cfg_request()
554 ret = tb_ctl_tx(ctl, req->request, req->request_size, in tb_cfg_request()
555 req->request_type); in tb_cfg_request()
559 if (!req->response) in tb_cfg_request()
560 schedule_work(&req->work); in tb_cfg_request()
573 * tb_cfg_request_cancel() - Cancel a control request
582 set_bit(TB_CFG_REQUEST_CANCELED, &req->flags); in tb_cfg_request_cancel()
583 schedule_work(&req->work); in tb_cfg_request_cancel()
585 req->result.err = err; in tb_cfg_request_cancel()
594 * tb_cfg_request_sync() - Start control request and wait until it completes
620 tb_cfg_request_cancel(req, -ETIMEDOUT); in tb_cfg_request_sync()
622 flush_work(&req->work); in tb_cfg_request_sync()
624 return req->result; in tb_cfg_request_sync()
630 * tb_ctl_alloc() - allocate a control channel
633 * @timeout_msec: Default timeout used with non-raw control messages
649 ctl->nhi = nhi; in tb_ctl_alloc()
650 ctl->index = index; in tb_ctl_alloc()
651 ctl->timeout_msec = timeout_msec; in tb_ctl_alloc()
652 ctl->callback = cb; in tb_ctl_alloc()
653 ctl->callback_data = cb_data; in tb_ctl_alloc()
655 mutex_init(&ctl->request_queue_lock); in tb_ctl_alloc()
656 INIT_LIST_HEAD(&ctl->request_queue); in tb_ctl_alloc()
657 ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev, in tb_ctl_alloc()
659 if (!ctl->frame_pool) in tb_ctl_alloc()
662 ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND); in tb_ctl_alloc()
663 if (!ctl->tx) in tb_ctl_alloc()
666 ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0, 0xffff, in tb_ctl_alloc()
668 if (!ctl->rx) in tb_ctl_alloc()
672 ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl); in tb_ctl_alloc()
673 if (!ctl->rx_packets[i]) in tb_ctl_alloc()
675 ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback; in tb_ctl_alloc()
686 * tb_ctl_free() - free a control channel
691 * Must NOT be called from ctl->callback.
700 if (ctl->rx) in tb_ctl_free()
701 tb_ring_free(ctl->rx); in tb_ctl_free()
702 if (ctl->tx) in tb_ctl_free()
703 tb_ring_free(ctl->tx); in tb_ctl_free()
707 tb_ctl_pkg_free(ctl->rx_packets[i]); in tb_ctl_free()
710 dma_pool_destroy(ctl->frame_pool); in tb_ctl_free()
715 * tb_ctl_start() - start/resume the control channel
722 tb_ring_start(ctl->tx); /* is used to ack hotplug packets, start first */ in tb_ctl_start()
723 tb_ring_start(ctl->rx); in tb_ctl_start()
725 tb_ctl_rx_submit(ctl->rx_packets[i]); in tb_ctl_start()
727 ctl->running = true; in tb_ctl_start()
731 * tb_ctl_stop() - pause the control channel
734 * All invocations of ctl->callback will have finished after this method
737 * Must NOT be called from ctl->callback.
741 mutex_lock(&ctl->request_queue_lock); in tb_ctl_stop()
742 ctl->running = false; in tb_ctl_stop()
743 mutex_unlock(&ctl->request_queue_lock); in tb_ctl_stop()
745 tb_ring_stop(ctl->rx); in tb_ctl_stop()
746 tb_ring_stop(ctl->tx); in tb_ctl_stop()
748 if (!list_empty(&ctl->request_queue)) in tb_ctl_stop()
750 INIT_LIST_HEAD(&ctl->request_queue); in tb_ctl_stop()
757 * tb_cfg_ack_notification() - Ack notification
762 * Call this as response for non-plug notification to ack it. Returns
773 switch (error->error) { in tb_cfg_ack_notification()
813 error->error, route); in tb_cfg_ack_notification()
819 * tb_cfg_ack_plug() - Ack hot plug/unplug event
845 u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63); in tb_cfg_match()
847 if (pkg->frame.eof == TB_CFG_PKG_ERROR) in tb_cfg_match()
850 if (pkg->frame.eof != req->response_type) in tb_cfg_match()
852 if (route != tb_cfg_get_route(req->request)) in tb_cfg_match()
854 if (pkg->frame.size != req->response_size) in tb_cfg_match()
857 if (pkg->frame.eof == TB_CFG_PKG_READ || in tb_cfg_match()
858 pkg->frame.eof == TB_CFG_PKG_WRITE) { in tb_cfg_match()
859 const struct cfg_read_pkg *req_hdr = req->request; in tb_cfg_match()
860 const struct cfg_read_pkg *res_hdr = pkg->buffer; in tb_cfg_match()
862 if (req_hdr->addr.seq != res_hdr->addr.seq) in tb_cfg_match()
874 res = parse_header(pkg, req->response_size, req->response_type, in tb_cfg_copy()
875 tb_cfg_get_route(req->request)); in tb_cfg_copy()
877 memcpy(req->response, pkg->buffer, req->response_size); in tb_cfg_copy()
879 req->result = res; in tb_cfg_copy()
886 * tb_cfg_reset() - send a reset packet and wait for a response
892 * -ETIMEDOUT and attempt to reconfigure the switch.
903 res.err = -ENOMEM; in tb_cfg_reset()
907 req->match = tb_cfg_match; in tb_cfg_reset()
908 req->copy = tb_cfg_copy; in tb_cfg_reset()
909 req->request = &request; in tb_cfg_reset()
910 req->request_size = sizeof(request); in tb_cfg_reset()
911 req->request_type = TB_CFG_PKG_RESET; in tb_cfg_reset()
912 req->response = &reply; in tb_cfg_reset()
913 req->response_size = sizeof(reply); in tb_cfg_reset()
914 req->response_type = TB_CFG_PKG_RESET; in tb_cfg_reset()
916 res = tb_cfg_request_sync(ctl, req, ctl->timeout_msec); in tb_cfg_reset()
924 * tb_cfg_read_raw() - read from config space into buffer
926 * @buffer: Buffer where the data is read
936 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer, in tb_cfg_read_raw() argument
958 res.err = -ENOMEM; in tb_cfg_read_raw()
964 req->match = tb_cfg_match; in tb_cfg_read_raw()
965 req->copy = tb_cfg_copy; in tb_cfg_read_raw()
966 req->request = &request; in tb_cfg_read_raw()
967 req->request_size = sizeof(request); in tb_cfg_read_raw()
968 req->request_type = TB_CFG_PKG_READ; in tb_cfg_read_raw()
969 req->response = &reply; in tb_cfg_read_raw()
970 req->response_size = 12 + 4 * length; in tb_cfg_read_raw()
971 req->response_type = TB_CFG_PKG_READ; in tb_cfg_read_raw()
977 if (res.err != -ETIMEDOUT) in tb_cfg_read_raw()
990 memcpy(buffer, &reply.data, 4 * length); in tb_cfg_read_raw()
995 * tb_cfg_write_raw() - write from buffer into config space
997 * @buffer: Data to write
1007 struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer, in tb_cfg_write_raw() argument
1024 memcpy(&request.data, buffer, length * 4); in tb_cfg_write_raw()
1031 res.err = -ENOMEM; in tb_cfg_write_raw()
1037 req->match = tb_cfg_match; in tb_cfg_write_raw()
1038 req->copy = tb_cfg_copy; in tb_cfg_write_raw()
1039 req->request = &request; in tb_cfg_write_raw()
1040 req->request_size = 12 + 4 * length; in tb_cfg_write_raw()
1041 req->request_type = TB_CFG_PKG_WRITE; in tb_cfg_write_raw()
1042 req->response = &reply; in tb_cfg_write_raw()
1043 req->response_size = sizeof(reply); in tb_cfg_write_raw()
1044 req->response_type = TB_CFG_PKG_WRITE; in tb_cfg_write_raw()
1050 if (res.err != -ETIMEDOUT) in tb_cfg_write_raw()
1071 * set to TB_TYPE_INACTIVE). In the former case return -ENODEV so in tb_cfg_get_error()
1075 res->tb_error == TB_CFG_ERROR_INVALID_CONFIG_SPACE) in tb_cfg_get_error()
1076 return -ENODEV; in tb_cfg_get_error()
1080 if (res->tb_error == TB_CFG_ERROR_LOCK) in tb_cfg_get_error()
1081 return -EACCES; in tb_cfg_get_error()
1082 if (res->tb_error == TB_CFG_ERROR_PORT_NOT_CONNECTED) in tb_cfg_get_error()
1083 return -ENOTCONN; in tb_cfg_get_error()
1085 return -EIO; in tb_cfg_get_error()
1088 int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, in tb_cfg_read() argument
1091 struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port, in tb_cfg_read()
1092 space, offset, length, ctl->timeout_msec); in tb_cfg_read()
1102 case -ETIMEDOUT: in tb_cfg_read()
1114 int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port, in tb_cfg_write() argument
1117 struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port, in tb_cfg_write()
1118 space, offset, length, ctl->timeout_msec); in tb_cfg_write()
1128 case -ETIMEDOUT: in tb_cfg_write()
1141 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
1156 ctl->timeout_msec); in tb_cfg_get_upstream_port()
1158 return -EIO; in tb_cfg_get_upstream_port()