// SPDX-License-Identifier: GPL-2.0 /* * Thunderbolt driver - control channel and configuration commands * * Copyright (c) 2014 Andreas Noever <[email protected]> * Copyright (C) 2018, Intel Corporation */ #include <linux/crc32.h> #include <linux/delay.h> #include <linux/slab.h> #include <linux/pci.h> #include <linux/dmapool.h> #include <linux/workqueue.h> #include "ctl.h" #define CREATE_TRACE_POINTS #include "trace.h" #define TB_CTL_RX_PKG_COUNT … #define TB_CTL_RETRIES … /** * struct tb_ctl - Thunderbolt control channel * @nhi: Pointer to the NHI structure * @tx: Transmit ring * @rx: Receive ring * @frame_pool: DMA pool for control messages * @rx_packets: Received control messages * @request_queue_lock: Lock protecting @request_queue * @request_queue: List of outstanding requests * @running: Is the control channel running at the moment * @timeout_msec: Default timeout for non-raw control messages * @callback: Callback called when hotplug message is received * @callback_data: Data passed to @callback * @index: Domain number. This will be output with the trace record. */ struct tb_ctl { … }; #define tb_ctl_WARN(ctl, format, arg...) … #define tb_ctl_err(ctl, format, arg...) … #define tb_ctl_warn(ctl, format, arg...) … #define tb_ctl_info(ctl, format, arg...) … #define tb_ctl_dbg(ctl, format, arg...) … static DECLARE_WAIT_QUEUE_HEAD(tb_cfg_request_cancel_queue); /* Serializes access to request kref_get/put */ static DEFINE_MUTEX(tb_cfg_request_lock); /** * tb_cfg_request_alloc() - Allocates a new config request * * This is refcounted object so when you are done with this, call * tb_cfg_request_put() to it. */ struct tb_cfg_request *tb_cfg_request_alloc(void) { … } /** * tb_cfg_request_get() - Increase refcount of a request * @req: Request whose refcount is increased */ void tb_cfg_request_get(struct tb_cfg_request *req) { … } static void tb_cfg_request_destroy(struct kref *kref) { … } /** * tb_cfg_request_put() - Decrease refcount and possibly release the request * @req: Request whose refcount is decreased * * Call this function when you are done with the request. When refcount * goes to %0 the object is released. */ void tb_cfg_request_put(struct tb_cfg_request *req) { … } static int tb_cfg_request_enqueue(struct tb_ctl *ctl, struct tb_cfg_request *req) { … } static void tb_cfg_request_dequeue(struct tb_cfg_request *req) { … } static bool tb_cfg_request_is_active(struct tb_cfg_request *req) { … } static struct tb_cfg_request * tb_cfg_request_find(struct tb_ctl *ctl, struct ctl_pkg *pkg) { … } /* utility functions */ static int check_header(const struct ctl_pkg *pkg, u32 len, enum tb_cfg_pkg_type type, u64 route) { … } static int check_config_address(struct tb_cfg_address addr, enum tb_cfg_space space, u32 offset, u32 length) { … } static struct tb_cfg_result decode_error(const struct ctl_pkg *response) { … } static struct tb_cfg_result parse_header(const struct ctl_pkg *pkg, u32 len, enum tb_cfg_pkg_type type, u64 route) { … } static void tb_cfg_print_error(struct tb_ctl *ctl, const struct tb_cfg_result *res) { … } static __be32 tb_crc(const void *data, size_t len) { … } static void tb_ctl_pkg_free(struct ctl_pkg *pkg) { … } static struct ctl_pkg *tb_ctl_pkg_alloc(struct tb_ctl *ctl) { … } /* RX/TX handling */ static void tb_ctl_tx_callback(struct tb_ring *ring, struct ring_frame *frame, bool canceled) { … } /* * tb_cfg_tx() - transmit a packet on the control channel * * len must be a multiple of four. * * Return: Returns 0 on success or an error code on failure. */ static int tb_ctl_tx(struct tb_ctl *ctl, const void *data, size_t len, enum tb_cfg_pkg_type type) { … } /* * tb_ctl_handle_event() - acknowledge a plug event, invoke ctl->callback */ static bool tb_ctl_handle_event(struct tb_ctl *ctl, enum tb_cfg_pkg_type type, struct ctl_pkg *pkg, size_t size) { … } static void tb_ctl_rx_submit(struct ctl_pkg *pkg) { … } static int tb_async_error(const struct ctl_pkg *pkg) { … } static void tb_ctl_rx_callback(struct tb_ring *ring, struct ring_frame *frame, bool canceled) { … } static void tb_cfg_request_work(struct work_struct *work) { … } /** * tb_cfg_request() - Start control request not waiting for it to complete * @ctl: Control channel to use * @req: Request to start * @callback: Callback called when the request is completed * @callback_data: Data to be passed to @callback * * This queues @req on the given control channel without waiting for it * to complete. When the request completes @callback is called. */ int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req, void (*callback)(void *), void *callback_data) { … } /** * tb_cfg_request_cancel() - Cancel a control request * @req: Request to cancel * @err: Error to assign to the request * * This function can be used to cancel ongoing request. It will wait * until the request is not active anymore. */ void tb_cfg_request_cancel(struct tb_cfg_request *req, int err) { … } static void tb_cfg_request_complete(void *data) { … } /** * tb_cfg_request_sync() - Start control request and wait until it completes * @ctl: Control channel to use * @req: Request to start * @timeout_msec: Timeout how long to wait @req to complete * * Starts a control request and waits until it completes. If timeout * triggers the request is canceled before function returns. Note the * caller needs to make sure only one message for given switch is active * at a time. */ struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl, struct tb_cfg_request *req, int timeout_msec) { … } /* public interface, alloc/start/stop/free */ /** * tb_ctl_alloc() - allocate a control channel * @nhi: Pointer to NHI * @index: Domain number * @timeout_msec: Default timeout used with non-raw control messages * @cb: Callback called for plug events * @cb_data: Data passed to @cb * * cb will be invoked once for every hot plug event. * * Return: Returns a pointer on success or NULL on failure. */ struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, int index, int timeout_msec, event_cb cb, void *cb_data) { … } /** * tb_ctl_free() - free a control channel * @ctl: Control channel to free * * Must be called after tb_ctl_stop. * * Must NOT be called from ctl->callback. */ void tb_ctl_free(struct tb_ctl *ctl) { … } /** * tb_ctl_start() - start/resume the control channel * @ctl: Control channel to start */ void tb_ctl_start(struct tb_ctl *ctl) { … } /** * tb_ctl_stop() - pause the control channel * @ctl: Control channel to stop * * All invocations of ctl->callback will have finished after this method * returns. * * Must NOT be called from ctl->callback. */ void tb_ctl_stop(struct tb_ctl *ctl) { … } /* public interface, commands */ /** * tb_cfg_ack_notification() - Ack notification * @ctl: Control channel to use * @route: Router that originated the event * @error: Pointer to the notification package * * Call this as response for non-plug notification to ack it. Returns * %0 on success or an error code on failure. */ int tb_cfg_ack_notification(struct tb_ctl *ctl, u64 route, const struct cfg_error_pkg *error) { … } /** * tb_cfg_ack_plug() - Ack hot plug/unplug event * @ctl: Control channel to use * @route: Router that originated the event * @port: Port where the hot plug/unplug happened * @unplug: Ack hot plug or unplug * * Call this as response for hot plug/unplug event to ack it. * Returns %0 on success or an error code on failure. */ int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug) { … } static bool tb_cfg_match(const struct tb_cfg_request *req, const struct ctl_pkg *pkg) { … } static bool tb_cfg_copy(struct tb_cfg_request *req, const struct ctl_pkg *pkg) { … } /** * tb_cfg_reset() - send a reset packet and wait for a response * @ctl: Control channel pointer * @route: Router string for the router to send reset * * If the switch at route is incorrectly configured then we will not receive a * reply (even though the switch will reset). The caller should check for * -ETIMEDOUT and attempt to reconfigure the switch. */ struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route) { … } /** * tb_cfg_read_raw() - read from config space into buffer * @ctl: Pointer to the control channel * @buffer: Buffer where the data is read * @route: Route string of the router * @port: Port number when reading from %TB_CFG_PORT, %0 otherwise * @space: Config space selector * @offset: Dword word offset of the register to start reading * @length: Number of dwords to read * @timeout_msec: Timeout in ms how long to wait for the response * * Reads from router config space without translating the possible error. */ struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, enum tb_cfg_space space, u32 offset, u32 length, int timeout_msec) { … } /** * tb_cfg_write_raw() - write from buffer into config space * @ctl: Pointer to the control channel * @buffer: Data to write * @route: Route string of the router * @port: Port number when writing to %TB_CFG_PORT, %0 otherwise * @space: Config space selector * @offset: Dword word offset of the register to start writing * @length: Number of dwords to write * @timeout_msec: Timeout in ms how long to wait for the response * * Writes to router config space without translating the possible error. */ struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port, enum tb_cfg_space space, u32 offset, u32 length, int timeout_msec) { … } static int tb_cfg_get_error(struct tb_ctl *ctl, enum tb_cfg_space space, const struct tb_cfg_result *res) { … } int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, enum tb_cfg_space space, u32 offset, u32 length) { … } int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port, enum tb_cfg_space space, u32 offset, u32 length) { … } /** * tb_cfg_get_upstream_port() - get upstream port number of switch at route * @ctl: Pointer to the control channel * @route: Route string of the router * * Reads the first dword from the switches TB_CFG_SWITCH config area and * returns the port number from which the reply originated. * * Return: Returns the upstream port number on success or an error code on * failure. */ int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route) { … }