diff options
| author | Andreas Noever <andreas.noever@gmail.com> | 2014-06-03 16:04:07 -0400 |
|---|---|---|
| committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2014-06-19 17:07:47 -0400 |
| commit | 520b670216a15fb949e6ec6a1af9b5dd55d219c7 (patch) | |
| tree | 905d7b0702895422156bb1c076072d95756d22db /drivers/thunderbolt | |
| parent | 053596d9e26c86352c4b2b372f43f2746b97de45 (diff) | |
thunderbolt: Add path setup code.
A thunderbolt path is a unidirectional channel between two thunderbolt
ports. Two such paths are needed to establish a pci tunnel.
This patch introduces struct tb_path as well as a set of tb_path_*
methods which are used to activate & deactivate paths.
Signed-off-by: Andreas Noever <andreas.noever@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Diffstat (limited to 'drivers/thunderbolt')
| -rw-r--r-- | drivers/thunderbolt/Makefile | 2 | ||||
| -rw-r--r-- | drivers/thunderbolt/path.c | 215 | ||||
| -rw-r--r-- | drivers/thunderbolt/switch.c | 34 | ||||
| -rw-r--r-- | drivers/thunderbolt/tb.h | 62 |
4 files changed, 312 insertions, 1 deletions
diff --git a/drivers/thunderbolt/Makefile b/drivers/thunderbolt/Makefile index 617b31480b2e..3532f3684efc 100644 --- a/drivers/thunderbolt/Makefile +++ b/drivers/thunderbolt/Makefile | |||
| @@ -1,3 +1,3 @@ | |||
| 1 | obj-${CONFIG_THUNDERBOLT} := thunderbolt.o | 1 | obj-${CONFIG_THUNDERBOLT} := thunderbolt.o |
| 2 | thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o | 2 | thunderbolt-objs := nhi.o ctl.o tb.o switch.o cap.o path.o |
| 3 | 3 | ||
diff --git a/drivers/thunderbolt/path.c b/drivers/thunderbolt/path.c new file mode 100644 index 000000000000..8fcf8a7b6c22 --- /dev/null +++ b/drivers/thunderbolt/path.c | |||
| @@ -0,0 +1,215 @@ | |||
| 1 | /* | ||
| 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 | |||
| 13 | static 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 | */ | ||
| 35 | struct 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 | */ | ||
| 53 | void 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 | |||
| 63 | static 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 | |||
| 76 | static 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 | |||
| 90 | void 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 | */ | ||
| 115 | int 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; | ||
| 195 | err: | ||
| 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 | */ | ||
| 205 | bool 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 | } | ||
diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c index d6c32e1e2b42..667413f3ad7a 100644 --- a/drivers/thunderbolt/switch.c +++ b/drivers/thunderbolt/switch.c | |||
| @@ -139,6 +139,40 @@ int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged) | |||
| 139 | } | 139 | } |
| 140 | 140 | ||
| 141 | /** | 141 | /** |
| 142 | * tb_port_add_nfc_credits() - add/remove non flow controlled credits to port | ||
| 143 | * | ||
| 144 | * Change the number of NFC credits allocated to @port by @credits. To remove | ||
| 145 | * NFC credits pass a negative amount of credits. | ||
| 146 | * | ||
| 147 | * Return: Returns 0 on success or an error code on failure. | ||
| 148 | */ | ||
| 149 | int tb_port_add_nfc_credits(struct tb_port *port, int credits) | ||
| 150 | { | ||
| 151 | if (credits == 0) | ||
| 152 | return 0; | ||
| 153 | tb_port_info(port, | ||
| 154 | "adding %#x NFC credits (%#x -> %#x)", | ||
| 155 | credits, | ||
| 156 | port->config.nfc_credits, | ||
| 157 | port->config.nfc_credits + credits); | ||
| 158 | port->config.nfc_credits += credits; | ||
| 159 | return tb_port_write(port, &port->config.nfc_credits, | ||
| 160 | TB_CFG_PORT, 4, 1); | ||
| 161 | } | ||
| 162 | |||
| 163 | /** | ||
| 164 | * tb_port_clear_counter() - clear a counter in TB_CFG_COUNTER | ||
| 165 | * | ||
| 166 | * Return: Returns 0 on success or an error code on failure. | ||
| 167 | */ | ||
| 168 | int tb_port_clear_counter(struct tb_port *port, int counter) | ||
| 169 | { | ||
| 170 | u32 zero[3] = { 0, 0, 0 }; | ||
| 171 | tb_port_info(port, "clearing counter %d\n", counter); | ||
| 172 | return tb_port_write(port, zero, TB_CFG_COUNTERS, 3 * counter, 3); | ||
| 173 | } | ||
| 174 | |||
| 175 | /** | ||
| 142 | * tb_init_port() - initialize a port | 176 | * tb_init_port() - initialize a port |
| 143 | * | 177 | * |
| 144 | * This is a helper method for tb_switch_alloc. Does not check or initialize | 178 | * This is a helper method for tb_switch_alloc. Does not check or initialize |
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h index 661f1828527a..8bbdc2bc4d09 100644 --- a/drivers/thunderbolt/tb.h +++ b/drivers/thunderbolt/tb.h | |||
| @@ -35,6 +35,60 @@ struct tb_port { | |||
| 35 | }; | 35 | }; |
| 36 | 36 | ||
| 37 | /** | 37 | /** |
| 38 | * struct tb_path_hop - routing information for a tb_path | ||
| 39 | * | ||
| 40 | * Hop configuration is always done on the IN port of a switch. | ||
| 41 | * in_port and out_port have to be on the same switch. Packets arriving on | ||
| 42 | * in_port with "hop" = in_hop_index will get routed to through out_port. The | ||
| 43 | * next hop to take (on out_port->remote) is determined by next_hop_index. | ||
| 44 | * | ||
| 45 | * in_counter_index is the index of a counter (in TB_CFG_COUNTERS) on the in | ||
| 46 | * port. | ||
| 47 | */ | ||
| 48 | struct tb_path_hop { | ||
| 49 | struct tb_port *in_port; | ||
| 50 | struct tb_port *out_port; | ||
| 51 | int in_hop_index; | ||
| 52 | int in_counter_index; /* write -1 to disable counters for this hop. */ | ||
| 53 | int next_hop_index; | ||
| 54 | }; | ||
| 55 | |||
| 56 | /** | ||
| 57 | * enum tb_path_port - path options mask | ||
| 58 | */ | ||
| 59 | enum tb_path_port { | ||
| 60 | TB_PATH_NONE = 0, | ||
| 61 | TB_PATH_SOURCE = 1, /* activate on the first hop (out of src) */ | ||
| 62 | TB_PATH_INTERNAL = 2, /* activate on other hops (not the first/last) */ | ||
| 63 | TB_PATH_DESTINATION = 4, /* activate on the last hop (into dst) */ | ||
| 64 | TB_PATH_ALL = 7, | ||
| 65 | }; | ||
| 66 | |||
| 67 | /** | ||
| 68 | * struct tb_path - a unidirectional path between two ports | ||
| 69 | * | ||
| 70 | * A path consists of a number of hops (see tb_path_hop). To establish a PCIe | ||
| 71 | * tunnel two paths have to be created between the two PCIe ports. | ||
| 72 | * | ||
| 73 | */ | ||
| 74 | struct tb_path { | ||
| 75 | struct tb *tb; | ||
| 76 | int nfc_credits; /* non flow controlled credits */ | ||
| 77 | enum tb_path_port ingress_shared_buffer; | ||
| 78 | enum tb_path_port egress_shared_buffer; | ||
| 79 | enum tb_path_port ingress_fc_enable; | ||
| 80 | enum tb_path_port egress_fc_enable; | ||
| 81 | |||
| 82 | int priority:3; | ||
| 83 | int weight:4; | ||
| 84 | bool drop_packages; | ||
| 85 | bool activated; | ||
| 86 | struct tb_path_hop *hops; | ||
| 87 | int path_length; /* number of hops */ | ||
| 88 | }; | ||
| 89 | |||
| 90 | |||
| 91 | /** | ||
| 38 | * struct tb - main thunderbolt bus structure | 92 | * struct tb - main thunderbolt bus structure |
| 39 | */ | 93 | */ |
| 40 | struct tb { | 94 | struct tb { |
| @@ -165,9 +219,17 @@ void tb_sw_set_unpplugged(struct tb_switch *sw); | |||
| 165 | struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route); | 219 | struct tb_switch *get_switch_at_route(struct tb_switch *sw, u64 route); |
| 166 | 220 | ||
| 167 | int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged); | 221 | int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged); |
| 222 | int tb_port_add_nfc_credits(struct tb_port *port, int credits); | ||
| 223 | int tb_port_clear_counter(struct tb_port *port, int counter); | ||
| 168 | 224 | ||
| 169 | int tb_find_cap(struct tb_port *port, enum tb_cfg_space space, u32 value); | 225 | int tb_find_cap(struct tb_port *port, enum tb_cfg_space space, u32 value); |
| 170 | 226 | ||
| 227 | struct tb_path *tb_path_alloc(struct tb *tb, int num_hops); | ||
| 228 | void tb_path_free(struct tb_path *path); | ||
| 229 | int tb_path_activate(struct tb_path *path); | ||
| 230 | void tb_path_deactivate(struct tb_path *path); | ||
| 231 | bool tb_path_is_invalid(struct tb_path *path); | ||
| 232 | |||
| 171 | 233 | ||
| 172 | static inline int tb_route_length(u64 route) | 234 | static inline int tb_route_length(u64 route) |
| 173 | { | 235 | { |
