• Home
  • Raw
  • Download

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

1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt Cactus Ridge driver - control channel and configuration commands
22 * struct tb_cfg - thunderbolt control channel
41 dev_WARN(&(ctl)->nhi->pdev->dev, format, ## arg)
44 dev_err(&(ctl)->nhi->pdev->dev, format, ## arg)
47 dev_warn(&(ctl)->nhi->pdev->dev, format, ## arg)
50 dev_info(&(ctl)->nhi->pdev->dev, format, ## arg)
53 dev_dbg(&(ctl)->nhi->pdev->dev, format, ## arg)
60 * tb_cfg_request_alloc() - Allocates a new config request
73 kref_init(&req->kref); in tb_cfg_request_alloc()
79 * tb_cfg_request_get() - Increase refcount of a request
85 kref_get(&req->kref); in tb_cfg_request_get()
97 * tb_cfg_request_put() - Decrease refcount and possibly release the request
106 kref_put(&req->kref, tb_cfg_request_destroy); in tb_cfg_request_put()
113 WARN_ON(test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags)); in tb_cfg_request_enqueue()
114 WARN_ON(req->ctl); in tb_cfg_request_enqueue()
116 mutex_lock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
117 if (!ctl->running) { in tb_cfg_request_enqueue()
118 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
119 return -ENOTCONN; in tb_cfg_request_enqueue()
121 req->ctl = ctl; in tb_cfg_request_enqueue()
122 list_add_tail(&req->list, &ctl->request_queue); in tb_cfg_request_enqueue()
123 set_bit(TB_CFG_REQUEST_ACTIVE, &req->flags); in tb_cfg_request_enqueue()
124 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_enqueue()
130 struct tb_ctl *ctl = req->ctl; in tb_cfg_request_dequeue()
132 mutex_lock(&ctl->request_queue_lock); in tb_cfg_request_dequeue()
133 list_del(&req->list); in tb_cfg_request_dequeue()
134 clear_bit(TB_CFG_REQUEST_ACTIVE, &req->flags); in tb_cfg_request_dequeue()
135 if (test_bit(TB_CFG_REQUEST_CANCELED, &req->flags)) in tb_cfg_request_dequeue()
137 mutex_unlock(&ctl->request_queue_lock); in tb_cfg_request_dequeue()
142 return test_bit(TB_CFG_REQUEST_ACTIVE, &req->flags); in tb_cfg_request_is_active()
151 mutex_lock(&pkg->ctl->request_queue_lock); in tb_cfg_request_find()
152 list_for_each_entry(req, &pkg->ctl->request_queue, list) { in tb_cfg_request_find()
154 if (req->match(req, pkg)) { in tb_cfg_request_find()
160 mutex_unlock(&pkg->ctl->request_queue_lock); in tb_cfg_request_find()
171 struct tb_cfg_header *header = pkg->buffer; in check_header()
173 /* check frame, TODO: frame flags */ in check_header()
174 if (WARN(len != pkg->frame.size, in check_header()
176 len, pkg->frame.size)) in check_header()
177 return -EIO; in check_header()
178 if (WARN(type != pkg->frame.eof, "wrong eof (expected %#x, got %#x)\n", in check_header()
179 type, pkg->frame.eof)) in check_header()
180 return -EIO; in check_header()
181 if (WARN(pkg->frame.sof, "wrong sof (expected 0x0, got %#x)\n", in check_header()
182 pkg->frame.sof)) in check_header()
183 return -EIO; in check_header()
186 if (WARN(header->unknown != 1 << 9, in check_header()
187 "header->unknown is %#x\n", header->unknown)) in check_header()
188 return -EIO; in check_header()
192 return -EIO; in check_header()
201 return -EIO; in check_config_address()
204 return -EIO; in check_config_address()
207 return -EIO; in check_config_address()
210 return -EIO; in check_config_address()
212 * We cannot check addr->port as it is set to the upstream port of the in check_config_address()
220 struct cfg_error_pkg *pkg = response->buffer; in decode_error()
222 res.response_route = tb_cfg_get_route(&pkg->header); in decode_error()
225 tb_cfg_get_route(&pkg->header)); in decode_error()
229 WARN(pkg->zero1, "pkg->zero1 is %#x\n", pkg->zero1); in decode_error()
230 WARN(pkg->zero2, "pkg->zero1 is %#x\n", pkg->zero1); in decode_error()
231 WARN(pkg->zero3, "pkg->zero1 is %#x\n", pkg->zero1); in decode_error()
233 res.tb_error = pkg->error; in decode_error()
234 res.response_port = pkg->port; in decode_error()
242 struct tb_cfg_header *header = pkg->buffer; in parse_header()
245 if (pkg->frame.eof == TB_CFG_PKG_ERROR) in parse_header()
257 WARN_ON(res->err != 1); in tb_cfg_print_error()
258 switch (res->tb_error) { in tb_cfg_print_error()
270 res->response_route, res->response_port); in tb_cfg_print_error()
274 * - The route contains a non-existent port. in tb_cfg_print_error()
275 * - The route contains a non-PHY port (e.g. PCIe). in tb_cfg_print_error()
276 * - The port in cfg_read/cfg_write does not exist. in tb_cfg_print_error()
279 res->response_route, res->response_port); in tb_cfg_print_error()
283 res->response_route, res->response_port); in tb_cfg_print_error()
288 res->response_route, res->response_port); in tb_cfg_print_error()
301 dma_pool_free(pkg->ctl->frame_pool, in tb_ctl_pkg_free()
302 pkg->buffer, pkg->frame.buffer_phy); in tb_ctl_pkg_free()
312 pkg->ctl = ctl; in tb_ctl_pkg_alloc()
313 pkg->buffer = dma_pool_alloc(ctl->frame_pool, GFP_KERNEL, in tb_ctl_pkg_alloc()
314 &pkg->frame.buffer_phy); in tb_ctl_pkg_alloc()
315 if (!pkg->buffer) { in tb_ctl_pkg_alloc()
325 static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame, in tb_ctl_tx_callback() argument
328 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame); in tb_ctl_tx_callback()
333 * tb_cfg_tx() - transmit a packet on the control channel
344 if (len % 4 != 0) { /* required for le->be conversion */ in tb_ctl_tx()
346 return -EINVAL; in tb_ctl_tx()
348 if (len > TB_FRAME_SIZE - 4) { /* checksum is 4 bytes */ in tb_ctl_tx()
350 len, TB_FRAME_SIZE - 4); in tb_ctl_tx()
351 return -EINVAL; in tb_ctl_tx()
355 return -ENOMEM; in tb_ctl_tx()
356 pkg->frame.callback = tb_ctl_tx_callback; in tb_ctl_tx()
357 pkg->frame.size = len + 4; in tb_ctl_tx()
358 pkg->frame.sof = type; in tb_ctl_tx()
359 pkg->frame.eof = type; in tb_ctl_tx()
360 cpu_to_be32_array(pkg->buffer, data, len / 4); in tb_ctl_tx()
361 *(__be32 *) (pkg->buffer + len) = tb_crc(pkg->buffer, len); in tb_ctl_tx()
363 res = tb_ring_tx(ctl->tx, &pkg->frame); in tb_ctl_tx()
370 * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback
375 return ctl->callback(ctl->callback_data, type, pkg->buffer, size); in tb_ctl_handle_event()
380 tb_ring_rx(pkg->ctl->rx, &pkg->frame); /* in tb_ctl_rx_submit()
383 * from ctl->rx_packets, so we do in tb_ctl_rx_submit()
392 if (pkg->frame.eof != TB_CFG_PKG_ERROR) in tb_async_error()
395 switch (error->error) { in tb_async_error()
406 static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame, in tb_ctl_rx_callback() argument
409 struct ctl_pkg *pkg = container_of(frame, typeof(*pkg), frame); in tb_ctl_rx_callback()
416 * ctl->rx_packets. in tb_ctl_rx_callback()
419 if (frame->size < 4 || frame->size % 4 != 0) { in tb_ctl_rx_callback()
420 tb_ctl_err(pkg->ctl, "RX: invalid size %#x, dropping packet\n", in tb_ctl_rx_callback()
421 frame->size); in tb_ctl_rx_callback()
425 frame->size -= 4; /* remove checksum */ in tb_ctl_rx_callback()
426 crc32 = tb_crc(pkg->buffer, frame->size); in tb_ctl_rx_callback()
427 be32_to_cpu_array(pkg->buffer, pkg->buffer, frame->size / 4); in tb_ctl_rx_callback()
429 switch (frame->eof) { in tb_ctl_rx_callback()
435 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) { in tb_ctl_rx_callback()
436 tb_ctl_err(pkg->ctl, in tb_ctl_rx_callback()
441 tb_ctl_handle_event(pkg->ctl, frame->eof, in tb_ctl_rx_callback()
442 pkg, frame->size); in tb_ctl_rx_callback()
450 if (*(__be32 *)(pkg->buffer + frame->size) != crc32) { in tb_ctl_rx_callback()
451 tb_ctl_err(pkg->ctl, in tb_ctl_rx_callback()
457 if (tb_ctl_handle_event(pkg->ctl, frame->eof, pkg, frame->size)) in tb_ctl_rx_callback()
471 req = tb_cfg_request_find(pkg->ctl, pkg); in tb_ctl_rx_callback()
473 if (req->copy(req, pkg)) in tb_ctl_rx_callback()
474 schedule_work(&req->work); in tb_ctl_rx_callback()
486 if (!test_bit(TB_CFG_REQUEST_CANCELED, &req->flags)) in tb_cfg_request_work()
487 req->callback(req->callback_data); in tb_cfg_request_work()
494 * tb_cfg_request() - Start control request not waiting for it to complete
508 req->flags = 0; in tb_cfg_request()
509 req->callback = callback; in tb_cfg_request()
510 req->callback_data = callback_data; in tb_cfg_request()
511 INIT_WORK(&req->work, tb_cfg_request_work); in tb_cfg_request()
512 INIT_LIST_HEAD(&req->list); in tb_cfg_request()
519 ret = tb_ctl_tx(ctl, req->request, req->request_size, in tb_cfg_request()
520 req->request_type); in tb_cfg_request()
524 if (!req->response) in tb_cfg_request()
525 schedule_work(&req->work); in tb_cfg_request()
538 * tb_cfg_request_cancel() - Cancel a control request
547 set_bit(TB_CFG_REQUEST_CANCELED, &req->flags); in tb_cfg_request_cancel()
548 schedule_work(&req->work); in tb_cfg_request_cancel()
550 req->result.err = err; in tb_cfg_request_cancel()
559 * tb_cfg_request_sync() - Start control request and wait until it completes
585 tb_cfg_request_cancel(req, -ETIMEDOUT); in tb_cfg_request_sync()
587 flush_work(&req->work); in tb_cfg_request_sync()
589 return req->result; in tb_cfg_request_sync()
595 * tb_ctl_alloc() - allocate a control channel
607 ctl->nhi = nhi; in tb_ctl_alloc()
608 ctl->callback = cb; in tb_ctl_alloc()
609 ctl->callback_data = cb_data; in tb_ctl_alloc()
611 mutex_init(&ctl->request_queue_lock); in tb_ctl_alloc()
612 INIT_LIST_HEAD(&ctl->request_queue); in tb_ctl_alloc()
613 ctl->frame_pool = dma_pool_create("thunderbolt_ctl", &nhi->pdev->dev, in tb_ctl_alloc()
615 if (!ctl->frame_pool) in tb_ctl_alloc()
618 ctl->tx = tb_ring_alloc_tx(nhi, 0, 10, RING_FLAG_NO_SUSPEND); in tb_ctl_alloc()
619 if (!ctl->tx) in tb_ctl_alloc()
622 ctl->rx = tb_ring_alloc_rx(nhi, 0, 10, RING_FLAG_NO_SUSPEND, 0xffff, in tb_ctl_alloc()
624 if (!ctl->rx) in tb_ctl_alloc()
628 ctl->rx_packets[i] = tb_ctl_pkg_alloc(ctl); in tb_ctl_alloc()
629 if (!ctl->rx_packets[i]) in tb_ctl_alloc()
631 ctl->rx_packets[i]->frame.callback = tb_ctl_rx_callback; in tb_ctl_alloc()
642 * tb_ctl_free() - free a control channel
646 * Must NOT be called from ctl->callback.
655 if (ctl->rx) in tb_ctl_free()
656 tb_ring_free(ctl->rx); in tb_ctl_free()
657 if (ctl->tx) in tb_ctl_free()
658 tb_ring_free(ctl->tx); in tb_ctl_free()
662 tb_ctl_pkg_free(ctl->rx_packets[i]); in tb_ctl_free()
665 if (ctl->frame_pool) in tb_ctl_free()
666 dma_pool_destroy(ctl->frame_pool); in tb_ctl_free()
671 * tb_cfg_start() - start/resume the control channel
677 tb_ring_start(ctl->tx); /* is used to ack hotplug packets, start first */ in tb_ctl_start()
678 tb_ring_start(ctl->rx); in tb_ctl_start()
680 tb_ctl_rx_submit(ctl->rx_packets[i]); in tb_ctl_start()
682 ctl->running = true; in tb_ctl_start()
686 * control() - pause the control channel
688 * All invocations of ctl->callback will have finished after this method
691 * Must NOT be called from ctl->callback.
695 mutex_lock(&ctl->request_queue_lock); in tb_ctl_stop()
696 ctl->running = false; in tb_ctl_stop()
697 mutex_unlock(&ctl->request_queue_lock); in tb_ctl_stop()
699 tb_ring_stop(ctl->rx); in tb_ctl_stop()
700 tb_ring_stop(ctl->tx); in tb_ctl_stop()
702 if (!list_empty(&ctl->request_queue)) in tb_ctl_stop()
704 INIT_LIST_HEAD(&ctl->request_queue); in tb_ctl_stop()
711 * tb_cfg_error() - send error packet
730 u64 route = tb_cfg_get_route(pkg->buffer) & ~BIT_ULL(63); in tb_cfg_match()
732 if (pkg->frame.eof == TB_CFG_PKG_ERROR) in tb_cfg_match()
735 if (pkg->frame.eof != req->response_type) in tb_cfg_match()
737 if (route != tb_cfg_get_route(req->request)) in tb_cfg_match()
739 if (pkg->frame.size != req->response_size) in tb_cfg_match()
742 if (pkg->frame.eof == TB_CFG_PKG_READ || in tb_cfg_match()
743 pkg->frame.eof == TB_CFG_PKG_WRITE) { in tb_cfg_match()
744 const struct cfg_read_pkg *req_hdr = req->request; in tb_cfg_match()
745 const struct cfg_read_pkg *res_hdr = pkg->buffer; in tb_cfg_match()
747 if (req_hdr->addr.seq != res_hdr->addr.seq) in tb_cfg_match()
759 res = parse_header(pkg, req->response_size, req->response_type, in tb_cfg_copy()
760 tb_cfg_get_route(req->request)); in tb_cfg_copy()
762 memcpy(req->response, pkg->buffer, req->response_size); in tb_cfg_copy()
764 req->result = res; in tb_cfg_copy()
771 * tb_cfg_reset() - send a reset packet and wait for a response
775 * -ETIMEDOUT and attempt to reconfigure the switch.
787 res.err = -ENOMEM; in tb_cfg_reset()
791 req->match = tb_cfg_match; in tb_cfg_reset()
792 req->copy = tb_cfg_copy; in tb_cfg_reset()
793 req->request = &request; in tb_cfg_reset()
794 req->request_size = sizeof(request); in tb_cfg_reset()
795 req->request_type = TB_CFG_PKG_RESET; in tb_cfg_reset()
796 req->response = &reply; in tb_cfg_reset()
797 req->response_size = sizeof(reply); in tb_cfg_reset()
798 req->response_type = TB_CFG_PKG_RESET; in tb_cfg_reset()
808 * tb_cfg_read() - read from config space into buffer
812 struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer, in tb_cfg_read_raw() argument
834 res.err = -ENOMEM; in tb_cfg_read_raw()
840 req->match = tb_cfg_match; in tb_cfg_read_raw()
841 req->copy = tb_cfg_copy; in tb_cfg_read_raw()
842 req->request = &request; in tb_cfg_read_raw()
843 req->request_size = sizeof(request); in tb_cfg_read_raw()
844 req->request_type = TB_CFG_PKG_READ; in tb_cfg_read_raw()
845 req->response = &reply; in tb_cfg_read_raw()
846 req->response_size = 12 + 4 * length; in tb_cfg_read_raw()
847 req->response_type = TB_CFG_PKG_READ; in tb_cfg_read_raw()
853 if (res.err != -ETIMEDOUT) in tb_cfg_read_raw()
866 memcpy(buffer, &reply.data, 4 * length); in tb_cfg_read_raw()
871 * tb_cfg_write() - write from buffer into config space
875 struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer, in tb_cfg_write_raw() argument
892 memcpy(&request.data, buffer, length * 4); in tb_cfg_write_raw()
899 res.err = -ENOMEM; in tb_cfg_write_raw()
905 req->match = tb_cfg_match; in tb_cfg_write_raw()
906 req->copy = tb_cfg_copy; in tb_cfg_write_raw()
907 req->request = &request; in tb_cfg_write_raw()
908 req->request_size = 12 + 4 * length; in tb_cfg_write_raw()
909 req->request_type = TB_CFG_PKG_WRITE; in tb_cfg_write_raw()
910 req->response = &reply; in tb_cfg_write_raw()
911 req->response_size = sizeof(reply); in tb_cfg_write_raw()
912 req->response_type = TB_CFG_PKG_WRITE; in tb_cfg_write_raw()
918 if (res.err != -ETIMEDOUT) in tb_cfg_write_raw()
933 int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, in tb_cfg_read() argument
936 struct tb_cfg_result res = tb_cfg_read_raw(ctl, buffer, route, port, in tb_cfg_read()
946 return -EIO; in tb_cfg_read()
948 case -ETIMEDOUT: in tb_cfg_read()
960 int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port, in tb_cfg_write() argument
963 struct tb_cfg_result res = tb_cfg_write_raw(ctl, buffer, route, port, in tb_cfg_write()
973 return -EIO; in tb_cfg_write()
975 case -ETIMEDOUT: in tb_cfg_write()
988 * tb_cfg_get_upstream_port() - get upstream port number of switch at route
1003 return -EIO; in tb_cfg_get_upstream_port()