blob: 9562cd026dc01754144edab74f981313f837b507 [file] [log] [blame]
Andreas Noever520b6702014-06-03 22:04:07 +02001/*
2 * Thunderbolt Cactus Ridge driver - path/tunnel functionality
3 *
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
5 */
6
7#include <linux/slab.h>
8#include <linux/errno.h>
9
10#include "tb.h"
11
12
13static void tb_dump_hop(struct tb_port *port, struct tb_regs_hop *hop)
14{
15 tb_port_info(port, " Hop through port %d to hop %d (%s)\n",
16 hop->out_port, hop->next_hop,
17 hop->enable ? "enabled" : "disabled");
18 tb_port_info(port, " Weight: %d Priority: %d Credits: %d Drop: %d\n",
19 hop->weight, hop->priority,
20 hop->initial_credits, hop->drop_packages);
21 tb_port_info(port, " Counter enabled: %d Counter index: %d\n",
22 hop->counter_enable, hop->counter);
23 tb_port_info(port, " Flow Control (In/Eg): %d/%d Shared Buffer (In/Eg): %d/%d\n",
24 hop->ingress_fc, hop->egress_fc,
25 hop->ingress_shared_buffer, hop->egress_shared_buffer);
26 tb_port_info(port, " Unknown1: %#x Unknown2: %#x Unknown3: %#x\n",
27 hop->unknown1, hop->unknown2, hop->unknown3);
28}
29
30/**
31 * tb_path_alloc() - allocate a thunderbolt path
32 *
33 * Return: Returns a tb_path on success or NULL on failure.
34 */
35struct tb_path *tb_path_alloc(struct tb *tb, int num_hops)
36{
37 struct tb_path *path = kzalloc(sizeof(*path), GFP_KERNEL);
38 if (!path)
39 return NULL;
40 path->hops = kcalloc(num_hops, sizeof(*path->hops), GFP_KERNEL);
41 if (!path->hops) {
42 kfree(path);
43 return NULL;
44 }
45 path->tb = tb;
46 path->path_length = num_hops;
47 return path;
48}
49
50/**
51 * tb_path_free() - free a deactivated path
52 */
53void tb_path_free(struct tb_path *path)
54{
55 if (path->activated) {
56 tb_WARN(path->tb, "trying to free an activated path\n")
57 return;
58 }
59 kfree(path->hops);
60 kfree(path);
61}
62
63static void __tb_path_deallocate_nfc(struct tb_path *path, int first_hop)
64{
65 int i, res;
66 for (i = first_hop; i < path->path_length; i++) {
67 res = tb_port_add_nfc_credits(path->hops[i].in_port,
68 -path->nfc_credits);
69 if (res)
70 tb_port_warn(path->hops[i].in_port,
71 "nfc credits deallocation failed for hop %d\n",
72 i);
73 }
74}
75
76static void __tb_path_deactivate_hops(struct tb_path *path, int first_hop)
77{
78 int i, res;
79 struct tb_regs_hop hop = { };
80 for (i = first_hop; i < path->path_length; i++) {
81 res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
82 2 * path->hops[i].in_hop_index, 2);
83 if (res)
84 tb_port_warn(path->hops[i].in_port,
85 "hop deactivation failed for hop %d, index %d\n",
86 i, path->hops[i].in_hop_index);
87 }
88}
89
90void tb_path_deactivate(struct tb_path *path)
91{
92 if (!path->activated) {
93 tb_WARN(path->tb, "trying to deactivate an inactive path\n");
94 return;
95 }
96 tb_info(path->tb,
97 "deactivating path from %llx:%x to %llx:%x\n",
98 tb_route(path->hops[0].in_port->sw),
99 path->hops[0].in_port->port,
100 tb_route(path->hops[path->path_length - 1].out_port->sw),
101 path->hops[path->path_length - 1].out_port->port);
102 __tb_path_deactivate_hops(path, 0);
103 __tb_path_deallocate_nfc(path, 0);
104 path->activated = false;
105}
106
107/**
108 * tb_path_activate() - activate a path
109 *
110 * Activate a path starting with the last hop and iterating backwards. The
111 * caller must fill path->hops before calling tb_path_activate().
112 *
113 * Return: Returns 0 on success or an error code on failure.
114 */
115int tb_path_activate(struct tb_path *path)
116{
117 int i, res;
118 enum tb_path_port out_mask, in_mask;
119 if (path->activated) {
120 tb_WARN(path->tb, "trying to activate already activated path\n");
121 return -EINVAL;
122 }
123
124 tb_info(path->tb,
125 "activating path from %llx:%x to %llx:%x\n",
126 tb_route(path->hops[0].in_port->sw),
127 path->hops[0].in_port->port,
128 tb_route(path->hops[path->path_length - 1].out_port->sw),
129 path->hops[path->path_length - 1].out_port->port);
130
131 /* Clear counters. */
132 for (i = path->path_length - 1; i >= 0; i--) {
133 if (path->hops[i].in_counter_index == -1)
134 continue;
135 res = tb_port_clear_counter(path->hops[i].in_port,
136 path->hops[i].in_counter_index);
137 if (res)
138 goto err;
139 }
140
141 /* Add non flow controlled credits. */
142 for (i = path->path_length - 1; i >= 0; i--) {
143 res = tb_port_add_nfc_credits(path->hops[i].in_port,
144 path->nfc_credits);
145 if (res) {
146 __tb_path_deallocate_nfc(path, i);
147 goto err;
148 }
149 }
150
151 /* Activate hops. */
152 for (i = path->path_length - 1; i >= 0; i--) {
Andreas Noever72ad3662014-08-26 17:42:21 +0200153 struct tb_regs_hop hop = { 0 };
154
155 /*
156 * We do (currently) not tear down paths setup by the firmeware.
157 * If a firmware device is unplugged and plugged in again then
158 * it can happen that we reuse some of the hops from the (now
159 * defunct) firmeware path. This causes the hotplug operation to
160 * fail (the pci device does not show up). Clearing the hop
161 * before overwriting it fixes the problem.
162 *
163 * Should be removed once we discover and tear down firmeware
164 * paths.
165 */
166 res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
167 2 * path->hops[i].in_hop_index, 2);
168 if (res) {
169 __tb_path_deactivate_hops(path, i);
170 __tb_path_deallocate_nfc(path, 0);
171 goto err;
172 }
Andreas Noever520b6702014-06-03 22:04:07 +0200173
174 /* dword 0 */
175 hop.next_hop = path->hops[i].next_hop_index;
176 hop.out_port = path->hops[i].out_port->port;
177 /* TODO: figure out why these are good values */
178 hop.initial_credits = (i == path->path_length - 1) ? 16 : 7;
179 hop.unknown1 = 0;
180 hop.enable = 1;
181
182 /* dword 1 */
183 out_mask = (i == path->path_length - 1) ?
184 TB_PATH_DESTINATION : TB_PATH_INTERNAL;
185 in_mask = (i == 0) ? TB_PATH_SOURCE : TB_PATH_INTERNAL;
186 hop.weight = path->weight;
187 hop.unknown2 = 0;
188 hop.priority = path->priority;
189 hop.drop_packages = path->drop_packages;
190 hop.counter = path->hops[i].in_counter_index;
191 hop.counter_enable = path->hops[i].in_counter_index != -1;
192 hop.ingress_fc = path->ingress_fc_enable & in_mask;
193 hop.egress_fc = path->egress_fc_enable & out_mask;
194 hop.ingress_shared_buffer = path->ingress_shared_buffer
195 & in_mask;
196 hop.egress_shared_buffer = path->egress_shared_buffer
197 & out_mask;
198 hop.unknown3 = 0;
199
200 tb_port_info(path->hops[i].in_port, "Writing hop %d, index %d",
201 i, path->hops[i].in_hop_index);
202 tb_dump_hop(path->hops[i].in_port, &hop);
203 res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
204 2 * path->hops[i].in_hop_index, 2);
205 if (res) {
206 __tb_path_deactivate_hops(path, i);
207 __tb_path_deallocate_nfc(path, 0);
208 goto err;
209 }
210 }
211 path->activated = true;
212 tb_info(path->tb, "path activation complete\n");
213 return 0;
214err:
215 tb_WARN(path->tb, "path activation failed\n");
216 return res;
217}
218
219/**
220 * tb_path_is_invalid() - check whether any ports on the path are invalid
221 *
222 * Return: Returns true if the path is invalid, false otherwise.
223 */
224bool tb_path_is_invalid(struct tb_path *path)
225{
226 int i = 0;
227 for (i = 0; i < path->path_length; i++) {
228 if (path->hops[i].in_port->sw->is_unplugged)
229 return true;
230 if (path->hops[i].out_port->sw->is_unplugged)
231 return true;
232 }
233 return false;
234}