blob: 8fcf8a7b6c2264d75f98d919b4bc88e7b1a3e4ae [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--) {
153 struct tb_regs_hop hop;
154
155 /* dword 0 */
156 hop.next_hop = path->hops[i].next_hop_index;
157 hop.out_port = path->hops[i].out_port->port;
158 /* TODO: figure out why these are good values */
159 hop.initial_credits = (i == path->path_length - 1) ? 16 : 7;
160 hop.unknown1 = 0;
161 hop.enable = 1;
162
163 /* dword 1 */
164 out_mask = (i == path->path_length - 1) ?
165 TB_PATH_DESTINATION : TB_PATH_INTERNAL;
166 in_mask = (i == 0) ? TB_PATH_SOURCE : TB_PATH_INTERNAL;
167 hop.weight = path->weight;
168 hop.unknown2 = 0;
169 hop.priority = path->priority;
170 hop.drop_packages = path->drop_packages;
171 hop.counter = path->hops[i].in_counter_index;
172 hop.counter_enable = path->hops[i].in_counter_index != -1;
173 hop.ingress_fc = path->ingress_fc_enable & in_mask;
174 hop.egress_fc = path->egress_fc_enable & out_mask;
175 hop.ingress_shared_buffer = path->ingress_shared_buffer
176 & in_mask;
177 hop.egress_shared_buffer = path->egress_shared_buffer
178 & out_mask;
179 hop.unknown3 = 0;
180
181 tb_port_info(path->hops[i].in_port, "Writing hop %d, index %d",
182 i, path->hops[i].in_hop_index);
183 tb_dump_hop(path->hops[i].in_port, &hop);
184 res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
185 2 * path->hops[i].in_hop_index, 2);
186 if (res) {
187 __tb_path_deactivate_hops(path, i);
188 __tb_path_deallocate_nfc(path, 0);
189 goto err;
190 }
191 }
192 path->activated = true;
193 tb_info(path->tb, "path activation complete\n");
194 return 0;
195err:
196 tb_WARN(path->tb, "path activation failed\n");
197 return res;
198}
199
200/**
201 * tb_path_is_invalid() - check whether any ports on the path are invalid
202 *
203 * Return: Returns true if the path is invalid, false otherwise.
204 */
205bool tb_path_is_invalid(struct tb_path *path)
206{
207 int i = 0;
208 for (i = 0; i < path->path_length; i++) {
209 if (path->hops[i].in_port->sw->is_unplugged)
210 return true;
211 if (path->hops[i].out_port->sw->is_unplugged)
212 return true;
213 }
214 return false;
215}