Lines Matching +full:frame +full:- +full:buffer
1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - control channel and configuration commands
23 * struct tb_cfg - thunderbolt control channel
42 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
45 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
48 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
51 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
54 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
61 * tb_cfg_request_alloc() - Allocates a new config request
74 kref_init(&req->kref); in tb_cfg_request_alloc()
80 * tb_cfg_request_get() - Increase refcount of a request
86 kref_get(&req->kref); in tb_cfg_request_get()
98 * tb_cfg_request_put() - Decrease refcount and possibly release the request
107 kref_put(&req->kref, tb_cfg_request_destroy); in tb_cfg_request_put()
114 WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags)); in tb_cfg_request_enqueue()
115 WARN_ON(req->ctl); in tb_cfg_request_enqueue()
117 mutex_lock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
118 if (!ctl->running) { in tb_cfg_request_enqueue()
119 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
120 return -ENOTCONN; in tb_cfg_request_enqueue()
122 req->ctl = ctl; in tb_cfg_request_enqueue()
123 list_add_tail(&req->list, &ctl->request_queue); in tb_cfg_request_enqueue()
124 set_bit(TB_CFG_REQUEST_ACTIVE, &req->flags); in tb_cfg_request_enqueue()
125 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
131 struct tb_ctl *ctl = req->ctl; in tb_cfg_request_dequeue()
133 mutex_lock(&ctl->request_queue_lock); in tb_cfg_request_dequeue()
134 list_del(&req->list); in tb_cfg_request_dequeue()
135 clear_bit(TB_CFG_REQUEST_ACTIVE, &req->flags); in tb_cfg_request_dequeue()
136 if (test_bit(TB_CFG_REQUEST_CANCELED, &req->flags)) in tb_cfg_request_dequeue()
138 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_dequeue()
143 return test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags); in tb_cfg_request_is_active()
152 mutex_lock(&pkg->ctl->request_queue_lock); in tb_cfg_request_find()
153 list_for_each_entry(req, &pkg->ctl->request_queue, list) { in tb_cfg_request_find()
155 if (req->match(req, pkg)) { in tb_cfg_request_find()
161 mutex_unlock(&pkg->ctl->request_queue_lock); in tb_cfg_request_find()
172 struct tb_cfg_header *header = pkg->buffer; in check_header()
174 /* check frame, TODO: frame flags */ in check_header()
175 if (WARN(len != pkg->frame.size, in check_header()
177 len, pkg->frame.size)) in check_header()
178 return -EIO; in check_header()
179 if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n", in check_header()
180 type, pkg->frame.eof)) in check_header()
181 return -EIO; in check_header()
182 if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n", in check_header()
183 pkg->frame.sof)) in check_header()
184 return -EIO; in check_header()
187 if (WARN(header->unknown != 1 << 9, in check_header()
188 "header->unknown is %#x\n", header->unknown)) in check_header()
189 return -EIO; in check_header()
193 return -EIO; in check_header()
202 return -EIO; in check_config_address()
205 return -EIO; in check_config_address()
208 return -EIO; in check_config_address()
211 return -EIO; in check_config_address()
213 * We cannot check addr->port as it is set to the upstream port of the in check_config_address()
221 struct cfg_error_pkg *pkg = response->buffer; in decode_error()
222 struct tb_ctl *ctl = response->ctl; in decode_error()
224 res.response_route = tb_cfg_get_route(&pkg->header); in decode_error()
227 tb_cfg_get_route(&pkg->header)); in decode_error()
231 if (pkg->zero1) in decode_error()
232 tb_ctl_warn(ctl, "pkg->zero1 is %#x\n", pkg->zero1); in decode_error()
233 if (pkg->zero2) in decode_error()
234 tb_ctl_warn(ctl, "pkg->zero2 is %#x\n", pkg->zero2); in decode_error()
235 if (pkg->zero3) in decode_error()
236 tb_ctl_warn(ctl, "pkg->zero3 is %#x\n", pkg->zero3); in decode_error()
239 res.tb_error = pkg->error; in decode_error()
240 res.response_port = pkg->port; in decode_error()
248 struct tb_cfg_header *header = pkg->buffer; in parse_header()
251 if (pkg->frame.eof == TB_CFG_PKG_ERROR) in parse_header()
263 WARN_ON(res->err != 1); in tb_cfg_print_error()
264 switch (res->tb_error) { in tb_cfg_print_error()
275 res->response_route, res->response_port); in tb_cfg_print_error()
279 * - The route contains a non-existent port. in tb_cfg_print_error()
280 * - The route contains a non-PHY port (e.g. PCIe). in tb_cfg_print_error()
281 * - The port in cfg_read/cfg_write does not exist. in tb_cfg_print_error()
284 res->response_route, res->response_port); in tb_cfg_print_error()
288 res->response_route, res->response_port); in tb_cfg_print_error()
292 res->response_route, res->response_port); in tb_cfg_print_error()
297 res->response_route, res->response_port); in tb_cfg_print_error()
310 dma_pool_free(pkg->ctl->frame_pool, in tb_ctl_pkg_free()
311 pkg->buffer, pkg->frame.buffer_phy); in tb_ctl_pkg_free()
321 pkg->ctl = ctl; in tb_ctl_pkg_alloc()
322 pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL, in tb_ctl_pkg_alloc()
323 &pkg->frame.buffer_phy); in tb_ctl_pkg_alloc()
324 if (!pkg->buffer) { in tb_ctl_pkg_alloc()
334 static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame, in tb_ctl_tx_callback() argument
337 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame); in tb_ctl_tx_callback()
342 * tb_cfg_tx() - transmit a packet on the control channel
353 if (len % 4 != 0) { /* required for le->be conversion */ in tb_ctl_tx()
355 return -EINVAL; in tb_ctl_tx()
357 if (len > TB_FRAME_SIZE - 4) { /* checksum is 4 bytes */ in tb_ctl_tx()
359 len, TB_FRAME_SIZE - 4); in tb_ctl_tx()
360 return -EINVAL; in tb_ctl_tx()
364 return -ENOMEM; in tb_ctl_tx()
365 pkg->frame.callback = tb_ctl_tx_callback; in tb_ctl_tx()
366 pkg->frame.size = len + 4; in tb_ctl_tx()
367 pkg->frame.sof = type; in tb_ctl_tx()
368 pkg->frame.eof = type; in tb_ctl_tx()
369 cpu_to_be32_array(pkg->buffer, data, len / 4); in tb_ctl_tx()
370 *(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len); in tb_ctl_tx()
372 res = tb_ring_tx(ctl->tx, &pkg->frame); in tb_ctl_tx()
379 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
384 return ctl->callback(ctl->callback_data, type, pkg->buffer, size); in tb_ctl_handle_event()
389 tb_ring_rx(pkg->ctl->rx, &pkg->frame); /* in tb_ctl_rx_submit()
392 * from ctl->rx_packets, so we do in tb_ctl_rx_submit()
401 if (pkg->frame.eof != TB_CFG_PKG_ERROR) in tb_async_error()
404 switch (error->error) { in tb_async_error()
415 static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame, in tb_ctl_rx_callback() argument
418 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame); in tb_ctl_rx_callback()
425 * ctl->rx_packets. in tb_ctl_rx_callback()
428 if (frame->size < 4 || frame->size % 4 != 0) { in tb_ctl_rx_callback()
429 tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n", in tb_ctl_rx_callback()
430 frame->size); in tb_ctl_rx_callback()
434 frame->size -= 4; /* remove checksum */ in tb_ctl_rx_callback()
435 crc32 = tb_crc(pkg->buffer, frame->size); in tb_ctl_rx_callback()
436 be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4); in tb_ctl_rx_callback()
438 switch (frame->eof) { in tb_ctl_rx_callback()
444 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) { in tb_ctl_rx_callback()
445 tb_ctl_err(pkg->ctl, in tb_ctl_rx_callback()
450 tb_ctl_handle_event(pkg->ctl, frame->eof, in tb_ctl_rx_callback()
451 pkg, frame->size); in tb_ctl_rx_callback()
459 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) { in tb_ctl_rx_callback()
460 tb_ctl_err(pkg->ctl, in tb_ctl_rx_callback()
466 if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size)) in tb_ctl_rx_callback()
480 req = tb_cfg_request_find(pkg->ctl, pkg); in tb_ctl_rx_callback()
482 if (req->copy(req, pkg)) in tb_ctl_rx_callback()
483 schedule_work(&req->work); in tb_ctl_rx_callback()
495 if (!test_bit(TB_CFG_REQUEST_CANCELED, &req->flags)) in tb_cfg_request_work()
496 req->callback(req->callback_data); in tb_cfg_request_work()
503 * tb_cfg_request() - Start control request not waiting for it to complete
517 req->flags = 0; in tb_cfg_request()
518 req->callback = callback; in tb_cfg_request()
519 req->callback_data = callback_data; in tb_cfg_request()
520 INIT_WORK(&req->work, tb_cfg_request_work); in tb_cfg_request()
521 INIT_LIST_HEAD(&req->list); in tb_cfg_request()
528 ret = tb_ctl_tx(ctl, req->request, req->request_size, in tb_cfg_request()
529 req->request_type); in tb_cfg_request()
533 if (!req->response) in tb_cfg_request()
534 schedule_work(&req->work); in tb_cfg_request()
547 * tb_cfg_request_cancel() - Cancel a control request
556 set_bit(TB_CFG_REQUEST_CANCELED, &req->flags); in tb_cfg_request_cancel()
557 schedule_work(&req->work); in tb_cfg_request_cancel()
559 req->result.err = err; in tb_cfg_request_cancel()
568 * tb_cfg_request_sync() - Start control request and wait until it completes
594 tb_cfg_request_cancel(req, -ETIMEDOUT); in tb_cfg_request_sync()
596 flush_work(&req->work); in tb_cfg_request_sync()
598 return req->result; in tb_cfg_request_sync()
604 * tb_ctl_alloc() - allocate a control channel
616 ctl->nhi = nhi; in tb_ctl_alloc()
617 ctl->callback = cb; in tb_ctl_alloc()
618 ctl->callback_data = cb_data; in tb_ctl_alloc()
620 mutex_init(&ctl->request_queue_lock); in tb_ctl_alloc()
621 INIT_LIST_HEAD(&ctl->request_queue); in tb_ctl_alloc()
622 ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev, in tb_ctl_alloc()
624 if (!ctl->frame_pool) in tb_ctl_alloc()
627 ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND); in tb_ctl_alloc()
628 if (!ctl->tx) in tb_ctl_alloc()
631 ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0xffff, in tb_ctl_alloc()
633 if (!ctl->rx) in tb_ctl_alloc()
637 ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl); in tb_ctl_alloc()
638 if (!ctl->rx_packets[i]) in tb_ctl_alloc()
640 ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback; in tb_ctl_alloc()
651 * tb_ctl_free() - free a control channel
655 * Must NOT be called from ctl->callback.
664 if (ctl->rx) in tb_ctl_free()
665 tb_ring_free(ctl->rx); in tb_ctl_free()
666 if (ctl->tx) in tb_ctl_free()
667 tb_ring_free(ctl->tx); in tb_ctl_free()
671 tb_ctl_pkg_free(ctl->rx_packets[i]); in tb_ctl_free()
674 dma_pool_destroy(ctl->frame_pool); in tb_ctl_free()
679 * tb_cfg_start() - start/resume the control channel
685 tb_ring_start(ctl->tx); /* is used to ack hotplug packets, start first */ in tb_ctl_start()
686 tb_ring_start(ctl->rx); in tb_ctl_start()
688 tb_ctl_rx_submit(ctl->rx_packets[i]); in tb_ctl_start()
690 ctl->running = true; in tb_ctl_start()
694 * control() - pause the control channel
696 * All invocations of ctl->callback will have finished after this method
699 * Must NOT be called from ctl->callback.
703 mutex_lock(&ctl->request_queue_lock); in tb_ctl_stop()
704 ctl->running = false; in tb_ctl_stop()
705 mutex_unlock(&ctl->request_queue_lock); in tb_ctl_stop()
707 tb_ring_stop(ctl->rx); in tb_ctl_stop()
708 tb_ring_stop(ctl->tx); in tb_ctl_stop()
710 if (!list_empty(&ctl->request_queue)) in tb_ctl_stop()
712 INIT_LIST_HEAD(&ctl->request_queue); in tb_ctl_stop()
719 * tb_cfg_ack_plug() - Ack hot plug/unplug event
745 u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63); in tb_cfg_match()
747 if (pkg->frame.eof == TB_CFG_PKG_ERROR) in tb_cfg_match()
750 if (pkg->frame.eof != req->response_type) in tb_cfg_match()
752 if (route != tb_cfg_get_route(req->request)) in tb_cfg_match()
754 if (pkg->frame.size != req->response_size) in tb_cfg_match()
757 if (pkg->frame.eof == TB_CFG_PKG_READ || in tb_cfg_match()
758 pkg->frame.eof == TB_CFG_PKG_WRITE) { in tb_cfg_match()
759 const struct cfg_read_pkg *req_hdr = req->request; in tb_cfg_match()
760 const struct cfg_read_pkg *res_hdr = pkg->buffer; in tb_cfg_match()
762 if (req_hdr->addr.seq != res_hdr->addr.seq) in tb_cfg_match()
774 res = parse_header(pkg, req->response_size, req->response_type, in tb_cfg_copy()
775 tb_cfg_get_route(req->request)); in tb_cfg_copy()
777 memcpy(req->response, pkg->buffer, req->response_size); in tb_cfg_copy()
779 req->result = res; in tb_cfg_copy()
786 * tb_cfg_reset() - send a reset packet and wait for a response
790 * -ETIMEDOUT and attempt to reconfigure the switch.
802 res.err = -ENOMEM; in tb_cfg_reset()
806 req->match = tb_cfg_match; in tb_cfg_reset()
807 req->copy = tb_cfg_copy; in tb_cfg_reset()
808 req->request = &request; in tb_cfg_reset()
809 req->request_size = sizeof(request); in tb_cfg_reset()
810 req->request_type = TB_CFG_PKG_RESET; in tb_cfg_reset()
811 req->response = &reply; in tb_cfg_reset()
812 req->response_size = sizeof(reply); in tb_cfg_reset()
813 req->response_type = TB_CFG_PKG_RESET; in tb_cfg_reset()
823 * tb_cfg_read() - read from config space into buffer
827 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer, in tb_cfg_read_raw() argument
849 res.err = -ENOMEM; in tb_cfg_read_raw()
855 req->match = tb_cfg_match; in tb_cfg_read_raw()
856 req->copy = tb_cfg_copy; in tb_cfg_read_raw()
857 req->request = &request; in tb_cfg_read_raw()
858 req->request_size = sizeof(request); in tb_cfg_read_raw()
859 req->request_type = TB_CFG_PKG_READ; in tb_cfg_read_raw()
860 req->response = &reply; in tb_cfg_read_raw()
861 req->response_size = 12 + 4 * length; in tb_cfg_read_raw()
862 req->response_type = TB_CFG_PKG_READ; in tb_cfg_read_raw()
868 if (res.err != -ETIMEDOUT) in tb_cfg_read_raw()
881 memcpy(buffer, &reply.data, 4 * length); in tb_cfg_read_raw()
886 * tb_cfg_write() - write from buffer into config space
890 struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer, in tb_cfg_write_raw() argument
907 memcpy(&request.data, buffer, length * 4); in tb_cfg_write_raw()
914 res.err = -ENOMEM; in tb_cfg_write_raw()
920 req->match = tb_cfg_match; in tb_cfg_write_raw()
921 req->copy = tb_cfg_copy; in tb_cfg_write_raw()
922 req->request = &request; in tb_cfg_write_raw()
923 req->request_size = 12 + 4 * length; in tb_cfg_write_raw()
924 req->request_type = TB_CFG_PKG_WRITE; in tb_cfg_write_raw()
925 req->response = &reply; in tb_cfg_write_raw()
926 req->response_size = sizeof(reply); in tb_cfg_write_raw()
927 req->response_type = TB_CFG_PKG_WRITE; in tb_cfg_write_raw()
933 if (res.err != -ETIMEDOUT) in tb_cfg_write_raw()
954 * set to TB_TYPE_INACTIVE). In the former case return -ENODEV so in tb_cfg_get_error()
958 res->tb_error == TB_CFG_ERROR_INVALID_CONFIG_SPACE) in tb_cfg_get_error()
959 return -ENODEV; in tb_cfg_get_error()
963 if (res->tb_error == TB_CFG_ERROR_LOCK) in tb_cfg_get_error()
964 return -EACCES; in tb_cfg_get_error()
965 return -EIO; in tb_cfg_get_error()
968 int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, in tb_cfg_read() argument
971 struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port, in tb_cfg_read()
982 case -ETIMEDOUT: in tb_cfg_read()
994 int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port, in tb_cfg_write() argument
997 struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port, in tb_cfg_write()
1008 case -ETIMEDOUT: in tb_cfg_write()
1021 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
1036 return -EIO; in tb_cfg_get_upstream_port()