162306a36Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0 */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Thunderbolt driver - control channel and configuration commands 462306a36Sopenharmony_ci * 562306a36Sopenharmony_ci * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> 662306a36Sopenharmony_ci * Copyright (C) 2018, Intel Corporation 762306a36Sopenharmony_ci */ 862306a36Sopenharmony_ci 962306a36Sopenharmony_ci#ifndef _TB_CFG 1062306a36Sopenharmony_ci#define _TB_CFG 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ci#include <linux/kref.h> 1362306a36Sopenharmony_ci#include <linux/thunderbolt.h> 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci#include "nhi.h" 1662306a36Sopenharmony_ci#include "tb_msgs.h" 1762306a36Sopenharmony_ci 1862306a36Sopenharmony_ci/* control channel */ 1962306a36Sopenharmony_cistruct tb_ctl; 2062306a36Sopenharmony_ci 2162306a36Sopenharmony_citypedef bool (*event_cb)(void *data, enum tb_cfg_pkg_type type, 2262306a36Sopenharmony_ci const void *buf, size_t size); 2362306a36Sopenharmony_ci 2462306a36Sopenharmony_cistruct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, int timeout_msec, event_cb cb, 2562306a36Sopenharmony_ci void *cb_data); 2662306a36Sopenharmony_civoid tb_ctl_start(struct tb_ctl *ctl); 2762306a36Sopenharmony_civoid tb_ctl_stop(struct tb_ctl *ctl); 2862306a36Sopenharmony_civoid tb_ctl_free(struct tb_ctl *ctl); 2962306a36Sopenharmony_ci 3062306a36Sopenharmony_ci/* configuration commands */ 3162306a36Sopenharmony_ci 3262306a36Sopenharmony_cistruct tb_cfg_result { 3362306a36Sopenharmony_ci u64 response_route; 3462306a36Sopenharmony_ci u32 response_port; /* 3562306a36Sopenharmony_ci * If err = 1 then this is the port that send the 3662306a36Sopenharmony_ci * error. 3762306a36Sopenharmony_ci * If err = 0 and if this was a cfg_read/write then 3862306a36Sopenharmony_ci * this is the upstream port of the responding 3962306a36Sopenharmony_ci * switch. 4062306a36Sopenharmony_ci * Otherwise the field is set to zero. 4162306a36Sopenharmony_ci */ 4262306a36Sopenharmony_ci int err; /* negative errors, 0 for success, 1 for tb errors */ 4362306a36Sopenharmony_ci enum tb_cfg_error tb_error; /* valid if err == 1 */ 4462306a36Sopenharmony_ci}; 4562306a36Sopenharmony_ci 4662306a36Sopenharmony_cistruct ctl_pkg { 4762306a36Sopenharmony_ci struct tb_ctl *ctl; 4862306a36Sopenharmony_ci void *buffer; 4962306a36Sopenharmony_ci struct ring_frame frame; 5062306a36Sopenharmony_ci}; 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci/** 5362306a36Sopenharmony_ci * struct tb_cfg_request - Control channel request 5462306a36Sopenharmony_ci * @kref: Reference count 5562306a36Sopenharmony_ci * @ctl: Pointer to the control channel structure. Only set when the 5662306a36Sopenharmony_ci * request is queued. 5762306a36Sopenharmony_ci * @request_size: Size of the request packet (in bytes) 5862306a36Sopenharmony_ci * @request_type: Type of the request packet 5962306a36Sopenharmony_ci * @response: Response is stored here 6062306a36Sopenharmony_ci * @response_size: Maximum size of one response packet 6162306a36Sopenharmony_ci * @response_type: Expected type of the response packet 6262306a36Sopenharmony_ci * @npackets: Number of packets expected to be returned with this request 6362306a36Sopenharmony_ci * @match: Function used to match the incoming packet 6462306a36Sopenharmony_ci * @copy: Function used to copy the incoming packet to @response 6562306a36Sopenharmony_ci * @callback: Callback called when the request is finished successfully 6662306a36Sopenharmony_ci * @callback_data: Data to be passed to @callback 6762306a36Sopenharmony_ci * @flags: Flags for the request 6862306a36Sopenharmony_ci * @work: Work item used to complete the request 6962306a36Sopenharmony_ci * @result: Result after the request has been completed 7062306a36Sopenharmony_ci * @list: Requests are queued using this field 7162306a36Sopenharmony_ci * 7262306a36Sopenharmony_ci * An arbitrary request over Thunderbolt control channel. For standard 7362306a36Sopenharmony_ci * control channel message, one should use tb_cfg_read/write() and 7462306a36Sopenharmony_ci * friends if possible. 7562306a36Sopenharmony_ci */ 7662306a36Sopenharmony_cistruct tb_cfg_request { 7762306a36Sopenharmony_ci struct kref kref; 7862306a36Sopenharmony_ci struct tb_ctl *ctl; 7962306a36Sopenharmony_ci const void *request; 8062306a36Sopenharmony_ci size_t request_size; 8162306a36Sopenharmony_ci enum tb_cfg_pkg_type request_type; 8262306a36Sopenharmony_ci void *response; 8362306a36Sopenharmony_ci size_t response_size; 8462306a36Sopenharmony_ci enum tb_cfg_pkg_type response_type; 8562306a36Sopenharmony_ci size_t npackets; 8662306a36Sopenharmony_ci bool (*match)(const struct tb_cfg_request *req, 8762306a36Sopenharmony_ci const struct ctl_pkg *pkg); 8862306a36Sopenharmony_ci bool (*copy)(struct tb_cfg_request *req, const struct ctl_pkg *pkg); 8962306a36Sopenharmony_ci void (*callback)(void *callback_data); 9062306a36Sopenharmony_ci void *callback_data; 9162306a36Sopenharmony_ci unsigned long flags; 9262306a36Sopenharmony_ci struct work_struct work; 9362306a36Sopenharmony_ci struct tb_cfg_result result; 9462306a36Sopenharmony_ci struct list_head list; 9562306a36Sopenharmony_ci}; 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ci#define TB_CFG_REQUEST_ACTIVE 0 9862306a36Sopenharmony_ci#define TB_CFG_REQUEST_CANCELED 1 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_cistruct tb_cfg_request *tb_cfg_request_alloc(void); 10162306a36Sopenharmony_civoid tb_cfg_request_get(struct tb_cfg_request *req); 10262306a36Sopenharmony_civoid tb_cfg_request_put(struct tb_cfg_request *req); 10362306a36Sopenharmony_ciint tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req, 10462306a36Sopenharmony_ci void (*callback)(void *), void *callback_data); 10562306a36Sopenharmony_civoid tb_cfg_request_cancel(struct tb_cfg_request *req, int err); 10662306a36Sopenharmony_cistruct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl, 10762306a36Sopenharmony_ci struct tb_cfg_request *req, int timeout_msec); 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_cistatic inline u64 tb_cfg_get_route(const struct tb_cfg_header *header) 11062306a36Sopenharmony_ci{ 11162306a36Sopenharmony_ci return (u64) header->route_hi << 32 | header->route_lo; 11262306a36Sopenharmony_ci} 11362306a36Sopenharmony_ci 11462306a36Sopenharmony_cistatic inline struct tb_cfg_header tb_cfg_make_header(u64 route) 11562306a36Sopenharmony_ci{ 11662306a36Sopenharmony_ci struct tb_cfg_header header = { 11762306a36Sopenharmony_ci .route_hi = route >> 32, 11862306a36Sopenharmony_ci .route_lo = route, 11962306a36Sopenharmony_ci }; 12062306a36Sopenharmony_ci /* check for overflow, route_hi is not 32 bits! */ 12162306a36Sopenharmony_ci WARN_ON(tb_cfg_get_route(&header) != route); 12262306a36Sopenharmony_ci return header; 12362306a36Sopenharmony_ci} 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ciint tb_cfg_ack_notification(struct tb_ctl *ctl, u64 route, 12662306a36Sopenharmony_ci const struct cfg_error_pkg *error); 12762306a36Sopenharmony_ciint tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug); 12862306a36Sopenharmony_cistruct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route); 12962306a36Sopenharmony_cistruct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer, 13062306a36Sopenharmony_ci u64 route, u32 port, 13162306a36Sopenharmony_ci enum tb_cfg_space space, u32 offset, 13262306a36Sopenharmony_ci u32 length, int timeout_msec); 13362306a36Sopenharmony_cistruct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer, 13462306a36Sopenharmony_ci u64 route, u32 port, 13562306a36Sopenharmony_ci enum tb_cfg_space space, u32 offset, 13662306a36Sopenharmony_ci u32 length, int timeout_msec); 13762306a36Sopenharmony_ciint tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port, 13862306a36Sopenharmony_ci enum tb_cfg_space space, u32 offset, u32 length); 13962306a36Sopenharmony_ciint tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port, 14062306a36Sopenharmony_ci enum tb_cfg_space space, u32 offset, u32 length); 14162306a36Sopenharmony_ciint tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route); 14262306a36Sopenharmony_ci 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_ci#endif 145