Skip to content

Commit ae76578

Browse files
committed
thunderbolt: Rework how tunnel->[init|deinit] hooks are called
The way these are called is not exactly symmetric as it is supposed to be: the former is called when tunnel is being activated and the latter is called when it is being released (not when it is being de-activated). Furthermore host-to-host (DMA) tunnels are abusing the ->deinit hook to clear out the credits. This makes it quite hard to follow what is being called and when. For these reasons rework the two "init" hooks to be called symmetrically and rename them accordingly. For the DMA one, add a new hook that is specifically used to run clean up for the tunnel when its memory is being released. Signed-off-by: Mika Westerberg <[email protected]>
1 parent 693b5bb commit ae76578

File tree

2 files changed

+29
-21
lines changed

2 files changed

+29
-21
lines changed

drivers/thunderbolt/tunnel.c

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ static int tb_dp_bandwidth_alloc_mode_enable(struct tb_tunnel *tunnel)
851851
return 0;
852852
}
853853

854-
static int tb_dp_init(struct tb_tunnel *tunnel)
854+
static int tb_dp_pre_activate(struct tb_tunnel *tunnel)
855855
{
856856
struct tb_port *in = tunnel->src_port;
857857
struct tb_switch *sw = in->sw;
@@ -877,7 +877,7 @@ static int tb_dp_init(struct tb_tunnel *tunnel)
877877
return tb_dp_bandwidth_alloc_mode_enable(tunnel);
878878
}
879879

880-
static void tb_dp_deinit(struct tb_tunnel *tunnel)
880+
static void tb_dp_post_deactivate(struct tb_tunnel *tunnel)
881881
{
882882
struct tb_port *in = tunnel->src_port;
883883

@@ -1368,9 +1368,9 @@ struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in,
13681368
if (!tunnel)
13691369
return NULL;
13701370

1371-
tunnel->init = tb_dp_init;
1372-
tunnel->deinit = tb_dp_deinit;
1371+
tunnel->pre_activate = tb_dp_pre_activate;
13731372
tunnel->activate = tb_dp_activate;
1373+
tunnel->post_deactivate = tb_dp_post_deactivate;
13741374
tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth;
13751375
tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth;
13761376
tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth;
@@ -1464,9 +1464,9 @@ struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
14641464
if (!tunnel)
14651465
return NULL;
14661466

1467-
tunnel->init = tb_dp_init;
1468-
tunnel->deinit = tb_dp_deinit;
1467+
tunnel->pre_activate = tb_dp_pre_activate;
14691468
tunnel->activate = tb_dp_activate;
1469+
tunnel->post_deactivate = tb_dp_post_deactivate;
14701470
tunnel->maximum_bandwidth = tb_dp_maximum_bandwidth;
14711471
tunnel->allocated_bandwidth = tb_dp_allocated_bandwidth;
14721472
tunnel->alloc_bandwidth = tb_dp_alloc_bandwidth;
@@ -1623,22 +1623,22 @@ static void tb_dma_release_credits(struct tb_path_hop *hop)
16231623
}
16241624
}
16251625

1626-
static void tb_dma_deinit_path(struct tb_path *path)
1626+
static void tb_dma_destroy_path(struct tb_path *path)
16271627
{
16281628
struct tb_path_hop *hop;
16291629

16301630
tb_path_for_each_hop(path, hop)
16311631
tb_dma_release_credits(hop);
16321632
}
16331633

1634-
static void tb_dma_deinit(struct tb_tunnel *tunnel)
1634+
static void tb_dma_destroy(struct tb_tunnel *tunnel)
16351635
{
16361636
int i;
16371637

16381638
for (i = 0; i < tunnel->npaths; i++) {
16391639
if (!tunnel->paths[i])
16401640
continue;
1641-
tb_dma_deinit_path(tunnel->paths[i]);
1641+
tb_dma_destroy_path(tunnel->paths[i]);
16421642
}
16431643
}
16441644

@@ -1684,7 +1684,7 @@ struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
16841684

16851685
tunnel->src_port = nhi;
16861686
tunnel->dst_port = dst;
1687-
tunnel->deinit = tb_dma_deinit;
1687+
tunnel->destroy = tb_dma_destroy;
16881688

16891689
credits = min_not_zero(dma_credits, nhi->sw->max_dma_credits);
16901690

@@ -1796,7 +1796,7 @@ static int tb_usb3_max_link_rate(struct tb_port *up, struct tb_port *down)
17961796
return min(up_max_rate, down_max_rate);
17971797
}
17981798

1799-
static int tb_usb3_init(struct tb_tunnel *tunnel)
1799+
static int tb_usb3_pre_activate(struct tb_tunnel *tunnel)
18001800
{
18011801
tb_tunnel_dbg(tunnel, "allocating initial bandwidth %d/%d Mb/s\n",
18021802
tunnel->allocated_up, tunnel->allocated_down);
@@ -2027,7 +2027,7 @@ struct tb_tunnel *tb_tunnel_discover_usb3(struct tb *tb, struct tb_port *down,
20272027
tb_tunnel_dbg(tunnel, "currently allocated bandwidth %d/%d Mb/s\n",
20282028
tunnel->allocated_up, tunnel->allocated_down);
20292029

2030-
tunnel->init = tb_usb3_init;
2030+
tunnel->pre_activate = tb_usb3_pre_activate;
20312031
tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
20322032
tunnel->release_unused_bandwidth =
20332033
tb_usb3_release_unused_bandwidth;
@@ -2116,7 +2116,7 @@ struct tb_tunnel *tb_tunnel_alloc_usb3(struct tb *tb, struct tb_port *up,
21162116
tunnel->allocated_up = min(max_rate, max_up);
21172117
tunnel->allocated_down = min(max_rate, max_down);
21182118

2119-
tunnel->init = tb_usb3_init;
2119+
tunnel->pre_activate = tb_usb3_pre_activate;
21202120
tunnel->consumed_bandwidth = tb_usb3_consumed_bandwidth;
21212121
tunnel->release_unused_bandwidth =
21222122
tb_usb3_release_unused_bandwidth;
@@ -2140,8 +2140,8 @@ void tb_tunnel_free(struct tb_tunnel *tunnel)
21402140
if (!tunnel)
21412141
return;
21422142

2143-
if (tunnel->deinit)
2144-
tunnel->deinit(tunnel);
2143+
if (tunnel->destroy)
2144+
tunnel->destroy(tunnel);
21452145

21462146
for (i = 0; i < tunnel->npaths; i++) {
21472147
if (tunnel->paths[i])
@@ -2192,8 +2192,8 @@ int tb_tunnel_restart(struct tb_tunnel *tunnel)
21922192
}
21932193
}
21942194

2195-
if (tunnel->init) {
2196-
res = tunnel->init(tunnel);
2195+
if (tunnel->pre_activate) {
2196+
res = tunnel->pre_activate(tunnel);
21972197
if (res)
21982198
return res;
21992199
}
@@ -2256,6 +2256,9 @@ void tb_tunnel_deactivate(struct tb_tunnel *tunnel)
22562256
if (tunnel->paths[i] && tunnel->paths[i]->activated)
22572257
tb_path_deactivate(tunnel->paths[i]);
22582258
}
2259+
2260+
if (tunnel->post_deactivate)
2261+
tunnel->post_deactivate(tunnel);
22592262
}
22602263

22612264
/**

drivers/thunderbolt/tunnel.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@ enum tb_tunnel_type {
2626
* tunnels may be %NULL or null adapter port instead.
2727
* @paths: All paths required by the tunnel
2828
* @npaths: Number of paths in @paths
29-
* @init: Optional tunnel specific initialization
30-
* @deinit: Optional tunnel specific de-initialization
29+
* @pre_activate: Optional tunnel specific initialization called before
30+
* activation. Can touch hardware.
3131
* @activate: Optional tunnel specific activation/deactivation
32+
* @post_deactivate: Optional tunnel specific de-initialization called
33+
* after deactivation. Can touch hardware.
34+
* @destroy: Optional tunnel specific callback called when the tunnel
35+
* memory is being released. Should not touch hardware.
3236
* @maximum_bandwidth: Returns maximum possible bandwidth for this tunnel
3337
* @allocated_bandwidth: Return how much bandwidth is allocated for the tunnel
3438
* @alloc_bandwidth: Change tunnel bandwidth allocation
@@ -52,9 +56,10 @@ struct tb_tunnel {
5256
struct tb_port *dst_port;
5357
struct tb_path **paths;
5458
size_t npaths;
55-
int (*init)(struct tb_tunnel *tunnel);
56-
void (*deinit)(struct tb_tunnel *tunnel);
59+
int (*pre_activate)(struct tb_tunnel *tunnel);
5760
int (*activate)(struct tb_tunnel *tunnel, bool activate);
61+
void (*post_deactivate)(struct tb_tunnel *tunnel);
62+
void (*destroy)(struct tb_tunnel *tunnel);
5863
int (*maximum_bandwidth)(struct tb_tunnel *tunnel, int *max_up,
5964
int *max_down);
6065
int (*allocated_bandwidth)(struct tb_tunnel *tunnel, int *allocated_up,

0 commit comments

Comments
 (0)