net: smc91x: ACPI Enable lan91x adapters
[linux/fpc-iii.git] / drivers / thunderbolt / tunnel_pci.c
blobbaf1cd370446d6e12e9b69efba7b214b17092c8b
1 /*
2 * Thunderbolt Cactus Ridge driver - PCIe tunnel
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
5 */
7 #include <linux/slab.h>
8 #include <linux/list.h>
10 #include "tunnel_pci.h"
11 #include "tb.h"
13 #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...) \
14 do { \
15 struct tb_pci_tunnel *__tunnel = (tunnel); \
16 level(__tunnel->tb, "%llx:%x <-> %llx:%x (PCI): " fmt, \
17 tb_route(__tunnel->down_port->sw), \
18 __tunnel->down_port->port, \
19 tb_route(__tunnel->up_port->sw), \
20 __tunnel->up_port->port, \
21 ## arg); \
22 } while (0)
24 #define tb_tunnel_WARN(tunnel, fmt, arg...) \
25 __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
26 #define tb_tunnel_warn(tunnel, fmt, arg...) \
27 __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
28 #define tb_tunnel_info(tunnel, fmt, arg...) \
29 __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)
31 static void tb_pci_init_path(struct tb_path *path)
33 path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
34 path->egress_shared_buffer = TB_PATH_NONE;
35 path->ingress_fc_enable = TB_PATH_ALL;
36 path->ingress_shared_buffer = TB_PATH_NONE;
37 path->priority = 3;
38 path->weight = 1;
39 path->drop_packages = 0;
40 path->nfc_credits = 0;
43 /**
44 * tb_pci_alloc() - allocate a pci tunnel
46 * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
47 * TB_TYPE_PCIE_DOWN.
49 * Currently only paths consisting of two hops are supported (that is the
50 * ports must be on "adjacent" switches).
52 * The paths are hard-coded to use hop 8 (the only working hop id available on
53 * my thunderbolt devices). Therefore at most ONE path per device may be
54 * activated.
56 * Return: Returns a tb_pci_tunnel on success or NULL on failure.
58 struct tb_pci_tunnel *tb_pci_alloc(struct tb *tb, struct tb_port *up,
59 struct tb_port *down)
61 struct tb_pci_tunnel *tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
62 if (!tunnel)
63 goto err;
64 tunnel->tb = tb;
65 tunnel->down_port = down;
66 tunnel->up_port = up;
67 INIT_LIST_HEAD(&tunnel->list);
68 tunnel->path_to_up = tb_path_alloc(up->sw->tb, 2);
69 if (!tunnel->path_to_up)
70 goto err;
71 tunnel->path_to_down = tb_path_alloc(up->sw->tb, 2);
72 if (!tunnel->path_to_down)
73 goto err;
74 tb_pci_init_path(tunnel->path_to_up);
75 tb_pci_init_path(tunnel->path_to_down);
77 tunnel->path_to_up->hops[0].in_port = down;
78 tunnel->path_to_up->hops[0].in_hop_index = 8;
79 tunnel->path_to_up->hops[0].in_counter_index = -1;
80 tunnel->path_to_up->hops[0].out_port = tb_upstream_port(up->sw)->remote;
81 tunnel->path_to_up->hops[0].next_hop_index = 8;
83 tunnel->path_to_up->hops[1].in_port = tb_upstream_port(up->sw);
84 tunnel->path_to_up->hops[1].in_hop_index = 8;
85 tunnel->path_to_up->hops[1].in_counter_index = -1;
86 tunnel->path_to_up->hops[1].out_port = up;
87 tunnel->path_to_up->hops[1].next_hop_index = 8;
89 tunnel->path_to_down->hops[0].in_port = up;
90 tunnel->path_to_down->hops[0].in_hop_index = 8;
91 tunnel->path_to_down->hops[0].in_counter_index = -1;
92 tunnel->path_to_down->hops[0].out_port = tb_upstream_port(up->sw);
93 tunnel->path_to_down->hops[0].next_hop_index = 8;
95 tunnel->path_to_down->hops[1].in_port =
96 tb_upstream_port(up->sw)->remote;
97 tunnel->path_to_down->hops[1].in_hop_index = 8;
98 tunnel->path_to_down->hops[1].in_counter_index = -1;
99 tunnel->path_to_down->hops[1].out_port = down;
100 tunnel->path_to_down->hops[1].next_hop_index = 8;
101 return tunnel;
103 err:
104 if (tunnel) {
105 if (tunnel->path_to_down)
106 tb_path_free(tunnel->path_to_down);
107 if (tunnel->path_to_up)
108 tb_path_free(tunnel->path_to_up);
109 kfree(tunnel);
111 return NULL;
115 * tb_pci_free() - free a tunnel
117 * The tunnel must have been deactivated.
119 void tb_pci_free(struct tb_pci_tunnel *tunnel)
121 if (tunnel->path_to_up->activated || tunnel->path_to_down->activated) {
122 tb_tunnel_WARN(tunnel, "trying to free an activated tunnel\n");
123 return;
125 tb_path_free(tunnel->path_to_up);
126 tb_path_free(tunnel->path_to_down);
127 kfree(tunnel);
131 * tb_pci_is_invalid - check whether an activated path is still valid
133 bool tb_pci_is_invalid(struct tb_pci_tunnel *tunnel)
135 WARN_ON(!tunnel->path_to_up->activated);
136 WARN_ON(!tunnel->path_to_down->activated);
138 return tb_path_is_invalid(tunnel->path_to_up)
139 || tb_path_is_invalid(tunnel->path_to_down);
143 * tb_pci_port_active() - activate/deactivate PCI capability
145 * Return: Returns 0 on success or an error code on failure.
147 static int tb_pci_port_active(struct tb_port *port, bool active)
149 u32 word = active ? 0x80000000 : 0x0;
150 int cap = tb_find_cap(port, TB_CFG_PORT, TB_CAP_PCIE);
151 if (cap <= 0) {
152 tb_port_warn(port, "TB_CAP_PCIE not found: %d\n", cap);
153 return cap ? cap : -ENXIO;
155 return tb_port_write(port, &word, TB_CFG_PORT, cap, 1);
159 * tb_pci_restart() - activate a tunnel after a hardware reset
161 int tb_pci_restart(struct tb_pci_tunnel *tunnel)
163 int res;
164 tunnel->path_to_up->activated = false;
165 tunnel->path_to_down->activated = false;
167 tb_tunnel_info(tunnel, "activating\n");
169 res = tb_path_activate(tunnel->path_to_up);
170 if (res)
171 goto err;
172 res = tb_path_activate(tunnel->path_to_down);
173 if (res)
174 goto err;
176 res = tb_pci_port_active(tunnel->down_port, true);
177 if (res)
178 goto err;
180 res = tb_pci_port_active(tunnel->up_port, true);
181 if (res)
182 goto err;
183 return 0;
184 err:
185 tb_tunnel_warn(tunnel, "activation failed\n");
186 tb_pci_deactivate(tunnel);
187 return res;
191 * tb_pci_activate() - activate a tunnel
193 * Return: Returns 0 on success or an error code on failure.
195 int tb_pci_activate(struct tb_pci_tunnel *tunnel)
197 int res;
198 if (tunnel->path_to_up->activated || tunnel->path_to_down->activated) {
199 tb_tunnel_WARN(tunnel,
200 "trying to activate an already activated tunnel\n");
201 return -EINVAL;
204 res = tb_pci_restart(tunnel);
205 if (res)
206 return res;
208 list_add(&tunnel->list, &tunnel->tb->tunnel_list);
209 return 0;
215 * tb_pci_deactivate() - deactivate a tunnel
217 void tb_pci_deactivate(struct tb_pci_tunnel *tunnel)
219 tb_tunnel_info(tunnel, "deactivating\n");
221 * TODO: enable reset by writing 0x04000000 to TB_CAP_PCIE + 1 on up
222 * port. Seems to have no effect?
224 tb_pci_port_active(tunnel->up_port, false);
225 tb_pci_port_active(tunnel->down_port, false);
226 if (tunnel->path_to_down->activated)
227 tb_path_deactivate(tunnel->path_to_down);
228 if (tunnel->path_to_up->activated)
229 tb_path_deactivate(tunnel->path_to_up);
230 list_del_init(&tunnel->list);