blob: 97cb03b38953112cbe6aa6112ee1b3d60aa3f89e [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Andreas Noeverf25bf6f2014-06-03 22:03:59 +02002/*
Mika Westerberg15c67842018-10-01 12:31:22 +03003 * Thunderbolt driver - control channel and configuration commands
Andreas Noeverf25bf6f2014-06-03 22:03:59 +02004 *
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
Mika Westerberg15c67842018-10-01 12:31:22 +03006 * Copyright (C) 2018, Intel Corporation
Andreas Noeverf25bf6f2014-06-03 22:03:59 +02007 */
8
9#ifndef _TB_CFG
10#define _TB_CFG
11
Mika Westerbergd7f781b2017-06-06 15:25:10 +030012#include <linux/kref.h>
Mika Westerbergeaf8ff32017-10-02 13:38:31 +030013#include <linux/thunderbolt.h>
Mika Westerbergd7f781b2017-06-06 15:25:10 +030014
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020015#include "nhi.h"
Mika Westerberg32af9432017-06-06 15:25:06 +030016#include "tb_msgs.h"
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020017
18/* control channel */
19struct tb_ctl;
20
Mika Westerbergd1ff7022017-10-02 13:38:34 +030021typedef bool (*event_cb)(void *data, enum tb_cfg_pkg_type type,
Mika Westerberg81a54b52017-06-06 15:25:09 +030022 const void *buf, size_t size);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020023
Mika Westerberg81a54b52017-06-06 15:25:09 +030024struct tb_ctl *tb_ctl_alloc(struct tb_nhi *nhi, event_cb cb, void *cb_data);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020025void tb_ctl_start(struct tb_ctl *ctl);
26void tb_ctl_stop(struct tb_ctl *ctl);
27void tb_ctl_free(struct tb_ctl *ctl);
28
29/* configuration commands */
30
31#define TB_CFG_DEFAULT_TIMEOUT 5000 /* msec */
32
Andreas Noeverf25bf6f2014-06-03 22:03:59 +020033struct tb_cfg_result {
34 u64 response_route;
35 u32 response_port; /*
36 * If err = 1 then this is the port that send the
37 * error.
38 * If err = 0 and if this was a cfg_read/write then
39 * this is the the upstream port of the responding
40 * switch.
41 * Otherwise the field is set to zero.
42 */
43 int err; /* negative errors, 0 for success, 1 for tb errors */
44 enum tb_cfg_error tb_error; /* valid if err == 1 */
45};
46
Mika Westerbergd7f781b2017-06-06 15:25:10 +030047struct ctl_pkg {
48 struct tb_ctl *ctl;
49 void *buffer;
50 struct ring_frame frame;
51};
52
53/**
54 * struct tb_cfg_request - Control channel request
55 * @kref: Reference count
56 * @ctl: Pointer to the control channel structure. Only set when the
57 * request is queued.
58 * @request_size: Size of the request packet (in bytes)
59 * @request_type: Type of the request packet
60 * @response: Response is stored here
61 * @response_size: Maximum size of one response packet
62 * @response_type: Expected type of the response packet
63 * @npackets: Number of packets expected to be returned with this request
64 * @match: Function used to match the incoming packet
65 * @copy: Function used to copy the incoming packet to @response
66 * @callback: Callback called when the request is finished successfully
67 * @callback_data: Data to be passed to @callback
68 * @flags: Flags for the request
69 * @work: Work item used to complete the request
70 * @result: Result after the request has been completed
71 * @list: Requests are queued using this field
72 *
73 * An arbitrary request over Thunderbolt control channel. For standard
74 * control channel message, one should use tb_cfg_read/write() and
75 * friends if possible.
76 */
77struct tb_cfg_request {
78 struct kref kref;
79 struct tb_ctl *ctl;
80 const void *request;
81 size_t request_size;
82 enum tb_cfg_pkg_type request_type;
83 void *response;
84 size_t response_size;
85 enum tb_cfg_pkg_type response_type;
86 size_t npackets;
87 bool (*match)(const struct tb_cfg_request *req,
88 const struct ctl_pkg *pkg);
89 bool (*copy)(struct tb_cfg_request *req, const struct ctl_pkg *pkg);
90 void (*callback)(void *callback_data);
91 void *callback_data;
92 unsigned long flags;
93 struct work_struct work;
94 struct tb_cfg_result result;
95 struct list_head list;
96};
97
98#define TB_CFG_REQUEST_ACTIVE 0
99#define TB_CFG_REQUEST_CANCELED 1
100
101struct tb_cfg_request *tb_cfg_request_alloc(void);
102void tb_cfg_request_get(struct tb_cfg_request *req);
103void tb_cfg_request_put(struct tb_cfg_request *req);
104int tb_cfg_request(struct tb_ctl *ctl, struct tb_cfg_request *req,
105 void (*callback)(void *), void *callback_data);
106void tb_cfg_request_cancel(struct tb_cfg_request *req, int err);
107struct tb_cfg_result tb_cfg_request_sync(struct tb_ctl *ctl,
108 struct tb_cfg_request *req, int timeout_msec);
109
Mika Westerbergac6c44d2017-06-06 15:25:07 +0300110static inline u64 tb_cfg_get_route(const struct tb_cfg_header *header)
111{
112 return (u64) header->route_hi << 32 | header->route_lo;
113}
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200114
Mika Westerberg05c242e2017-06-06 15:25:08 +0300115static inline struct tb_cfg_header tb_cfg_make_header(u64 route)
116{
117 struct tb_cfg_header header = {
118 .route_hi = route >> 32,
119 .route_lo = route,
120 };
121 /* check for overflow, route_hi is not 32 bits! */
122 WARN_ON(tb_cfg_get_route(&header) != route);
123 return header;
124}
125
Mika Westerberg210e9f52019-12-17 15:33:39 +0300126int tb_cfg_ack_plug(struct tb_ctl *ctl, u64 route, u32 port, bool unplug);
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200127struct tb_cfg_result tb_cfg_reset(struct tb_ctl *ctl, u64 route,
128 int timeout_msec);
129struct tb_cfg_result tb_cfg_read_raw(struct tb_ctl *ctl, void *buffer,
130 u64 route, u32 port,
131 enum tb_cfg_space space, u32 offset,
132 u32 length, int timeout_msec);
Mika Westerberg16a12582017-06-06 15:24:53 +0300133struct tb_cfg_result tb_cfg_write_raw(struct tb_ctl *ctl, const void *buffer,
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200134 u64 route, u32 port,
135 enum tb_cfg_space space, u32 offset,
136 u32 length, int timeout_msec);
137int tb_cfg_read(struct tb_ctl *ctl, void *buffer, u64 route, u32 port,
138 enum tb_cfg_space space, u32 offset, u32 length);
Mika Westerberg16a12582017-06-06 15:24:53 +0300139int tb_cfg_write(struct tb_ctl *ctl, const void *buffer, u64 route, u32 port,
Andreas Noeverf25bf6f2014-06-03 22:03:59 +0200140 enum tb_cfg_space space, u32 offset, u32 length);
141int tb_cfg_get_upstream_port(struct tb_ctl *ctl, u64 route);
142
143
144#endif