diff options
author | Andreas Noever <andreas.noever@gmail.com> | 2014-06-03 16:04:02 -0400 |
---|---|---|
committer | Greg Kroah-Hartman <gregkh@linuxfoundation.org> | 2014-06-19 17:07:08 -0400 |
commit | a25c8b2fc9636aaf29d9d9d89f92cdfd27a2a23d (patch) | |
tree | db952ae5e1d2897f54751e9d2d4c258dce853a00 | |
parent | 7adf60972c692b0b3d0958cd7322e22a67187111 (diff) |
thunderbolt: Initialize root switch and ports
This patch adds the structures tb_switch and tb_port as well as code to
initialize the root switch.
Signed-off-by: Andreas Noever <andreas.noever@gmail.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
-rw-r--r-- | drivers/thunderbolt/Makefile | 2 | ||||
-rw-r--r-- | drivers/thunderbolt/switch.c | 186 | ||||
-rw-r--r-- | drivers/thunderbolt/tb.c | 8 | ||||
-rw-r--r-- | drivers/thunderbolt/tb.h | 138 |
4 files changed, 333 insertions, 1 deletions
diff --git a/drivers/thunderbolt/Makefile b/drivers/thunderbolt/Makefile index 1f996bb96ac9..4ac18d969306 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 | 2 | thunderbolt-objs := nhi.o ctl.o tb.o switch.o |
3 | 3 | ||
diff --git a/drivers/thunderbolt/switch.c b/drivers/thunderbolt/switch.c new file mode 100644 index 000000000000..e89121ffe44e --- /dev/null +++ b/drivers/thunderbolt/switch.c | |||
@@ -0,0 +1,186 @@ | |||
1 | /* | ||
2 | * Thunderbolt Cactus Ridge driver - switch/port utility functions | ||
3 | * | ||
4 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> | ||
5 | */ | ||
6 | |||
7 | #include <linux/delay.h> | ||
8 | |||
9 | #include "tb.h" | ||
10 | |||
11 | /* port utility functions */ | ||
12 | |||
13 | static const char *tb_port_type(struct tb_regs_port_header *port) | ||
14 | { | ||
15 | switch (port->type >> 16) { | ||
16 | case 0: | ||
17 | switch ((u8) port->type) { | ||
18 | case 0: | ||
19 | return "Inactive"; | ||
20 | case 1: | ||
21 | return "Port"; | ||
22 | case 2: | ||
23 | return "NHI"; | ||
24 | default: | ||
25 | return "unknown"; | ||
26 | } | ||
27 | case 0x2: | ||
28 | return "Ethernet"; | ||
29 | case 0x8: | ||
30 | return "SATA"; | ||
31 | case 0xe: | ||
32 | return "DP/HDMI"; | ||
33 | case 0x10: | ||
34 | return "PCIe"; | ||
35 | case 0x20: | ||
36 | return "USB"; | ||
37 | default: | ||
38 | return "unknown"; | ||
39 | } | ||
40 | } | ||
41 | |||
42 | static void tb_dump_port(struct tb *tb, struct tb_regs_port_header *port) | ||
43 | { | ||
44 | tb_info(tb, | ||
45 | " Port %d: %x:%x (Revision: %d, TB Version: %d, Type: %s (%#x))\n", | ||
46 | port->port_number, port->vendor_id, port->device_id, | ||
47 | port->revision, port->thunderbolt_version, tb_port_type(port), | ||
48 | port->type); | ||
49 | tb_info(tb, " Max hop id (in/out): %d/%d\n", | ||
50 | port->max_in_hop_id, port->max_out_hop_id); | ||
51 | tb_info(tb, " Max counters: %d\n", port->max_counters); | ||
52 | tb_info(tb, " NFC Credits: %#x\n", port->nfc_credits); | ||
53 | } | ||
54 | |||
55 | /** | ||
56 | * tb_init_port() - initialize a port | ||
57 | * | ||
58 | * This is a helper method for tb_switch_alloc. Does not check or initialize | ||
59 | * any downstream switches. | ||
60 | * | ||
61 | * Return: Returns 0 on success or an error code on failure. | ||
62 | */ | ||
63 | static int tb_init_port(struct tb_switch *sw, u8 port_nr) | ||
64 | { | ||
65 | int res; | ||
66 | struct tb_port *port = &sw->ports[port_nr]; | ||
67 | port->sw = sw; | ||
68 | port->port = port_nr; | ||
69 | port->remote = NULL; | ||
70 | res = tb_port_read(port, &port->config, TB_CFG_PORT, 0, 8); | ||
71 | if (res) | ||
72 | return res; | ||
73 | |||
74 | tb_dump_port(sw->tb, &port->config); | ||
75 | |||
76 | /* TODO: Read dual link port, DP port and more from EEPROM. */ | ||
77 | return 0; | ||
78 | |||
79 | } | ||
80 | |||
81 | /* switch utility functions */ | ||
82 | |||
83 | static void tb_dump_switch(struct tb *tb, struct tb_regs_switch_header *sw) | ||
84 | { | ||
85 | tb_info(tb, | ||
86 | " Switch: %x:%x (Revision: %d, TB Version: %d)\n", | ||
87 | sw->vendor_id, sw->device_id, sw->revision, | ||
88 | sw->thunderbolt_version); | ||
89 | tb_info(tb, " Max Port Number: %d\n", sw->max_port_number); | ||
90 | tb_info(tb, " Config:\n"); | ||
91 | tb_info(tb, | ||
92 | " Upstream Port Number: %d Depth: %d Route String: %#llx Enabled: %d, PlugEventsDelay: %dms\n", | ||
93 | sw->upstream_port_number, sw->depth, | ||
94 | (((u64) sw->route_hi) << 32) | sw->route_lo, | ||
95 | sw->enabled, sw->plug_events_delay); | ||
96 | tb_info(tb, | ||
97 | " unknown1: %#x unknown4: %#x\n", | ||
98 | sw->__unknown1, sw->__unknown4); | ||
99 | } | ||
100 | |||
101 | /** | ||
102 | * tb_switch_free() - free a tb_switch and all downstream switches | ||
103 | */ | ||
104 | void tb_switch_free(struct tb_switch *sw) | ||
105 | { | ||
106 | int i; | ||
107 | /* port 0 is the switch itself and never has a remote */ | ||
108 | for (i = 1; i <= sw->config.max_port_number; i++) { | ||
109 | if (tb_is_upstream_port(&sw->ports[i])) | ||
110 | continue; | ||
111 | if (sw->ports[i].remote) | ||
112 | tb_switch_free(sw->ports[i].remote->sw); | ||
113 | sw->ports[i].remote = NULL; | ||
114 | } | ||
115 | |||
116 | kfree(sw->ports); | ||
117 | kfree(sw); | ||
118 | } | ||
119 | |||
120 | /** | ||
121 | * tb_switch_alloc() - allocate and initialize a switch | ||
122 | * | ||
123 | * Return: Returns a NULL on failure. | ||
124 | */ | ||
125 | struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route) | ||
126 | { | ||
127 | int i; | ||
128 | struct tb_switch *sw; | ||
129 | int upstream_port = tb_cfg_get_upstream_port(tb->ctl, route); | ||
130 | if (upstream_port < 0) | ||
131 | return NULL; | ||
132 | |||
133 | sw = kzalloc(sizeof(*sw), GFP_KERNEL); | ||
134 | if (!sw) | ||
135 | return NULL; | ||
136 | |||
137 | sw->tb = tb; | ||
138 | if (tb_cfg_read(tb->ctl, &sw->config, route, 0, 2, 0, 5)) | ||
139 | goto err; | ||
140 | tb_info(tb, | ||
141 | "initializing Switch at %#llx (depth: %d, up port: %d)\n", | ||
142 | route, tb_route_length(route), upstream_port); | ||
143 | tb_info(tb, "old switch config:\n"); | ||
144 | tb_dump_switch(tb, &sw->config); | ||
145 | |||
146 | /* configure switch */ | ||
147 | sw->config.upstream_port_number = upstream_port; | ||
148 | sw->config.depth = tb_route_length(route); | ||
149 | sw->config.route_lo = route; | ||
150 | sw->config.route_hi = route >> 32; | ||
151 | sw->config.enabled = 1; | ||
152 | /* from here on we may use the tb_sw_* functions & macros */ | ||
153 | |||
154 | if (sw->config.vendor_id != 0x8086) | ||
155 | tb_sw_warn(sw, "unknown switch vendor id %#x\n", | ||
156 | sw->config.vendor_id); | ||
157 | |||
158 | if (sw->config.device_id != 0x1547 && sw->config.device_id != 0x1549) | ||
159 | tb_sw_warn(sw, "unsupported switch device id %#x\n", | ||
160 | sw->config.device_id); | ||
161 | |||
162 | /* upload configuration */ | ||
163 | if (tb_sw_write(sw, 1 + (u32 *) &sw->config, TB_CFG_SWITCH, 1, 3)) | ||
164 | goto err; | ||
165 | |||
166 | /* initialize ports */ | ||
167 | sw->ports = kcalloc(sw->config.max_port_number + 1, sizeof(*sw->ports), | ||
168 | GFP_KERNEL); | ||
169 | if (!sw->ports) | ||
170 | goto err; | ||
171 | |||
172 | for (i = 0; i <= sw->config.max_port_number; i++) { | ||
173 | if (tb_init_port(sw, i)) | ||
174 | goto err; | ||
175 | /* TODO: check if port is disabled (EEPROM) */ | ||
176 | } | ||
177 | |||
178 | /* TODO: I2C, IECS, EEPROM, link controller */ | ||
179 | |||
180 | return sw; | ||
181 | err: | ||
182 | kfree(sw->ports); | ||
183 | kfree(sw); | ||
184 | return NULL; | ||
185 | } | ||
186 | |||
diff --git a/drivers/thunderbolt/tb.c b/drivers/thunderbolt/tb.c index 164dea083e9e..f1b6100b6cf0 100644 --- a/drivers/thunderbolt/tb.c +++ b/drivers/thunderbolt/tb.c | |||
@@ -71,6 +71,10 @@ void thunderbolt_shutdown_and_free(struct tb *tb) | |||
71 | { | 71 | { |
72 | mutex_lock(&tb->lock); | 72 | mutex_lock(&tb->lock); |
73 | 73 | ||
74 | if (tb->root_switch) | ||
75 | tb_switch_free(tb->root_switch); | ||
76 | tb->root_switch = NULL; | ||
77 | |||
74 | if (tb->ctl) { | 78 | if (tb->ctl) { |
75 | tb_ctl_stop(tb->ctl); | 79 | tb_ctl_stop(tb->ctl); |
76 | tb_ctl_free(tb->ctl); | 80 | tb_ctl_free(tb->ctl); |
@@ -126,6 +130,10 @@ struct tb *thunderbolt_alloc_and_start(struct tb_nhi *nhi) | |||
126 | */ | 130 | */ |
127 | tb_ctl_start(tb->ctl); | 131 | tb_ctl_start(tb->ctl); |
128 | 132 | ||
133 | tb->root_switch = tb_switch_alloc(tb, 0); | ||
134 | if (!tb->root_switch) | ||
135 | goto err_locked; | ||
136 | |||
129 | /* Allow tb_handle_hotplug to progress events */ | 137 | /* Allow tb_handle_hotplug to progress events */ |
130 | tb->hotplug_active = true; | 138 | tb->hotplug_active = true; |
131 | mutex_unlock(&tb->lock); | 139 | mutex_unlock(&tb->lock); |
diff --git a/drivers/thunderbolt/tb.h b/drivers/thunderbolt/tb.h index 6055dfd713fc..389dbb405c5f 100644 --- a/drivers/thunderbolt/tb.h +++ b/drivers/thunderbolt/tb.h | |||
@@ -7,9 +7,31 @@ | |||
7 | #ifndef TB_H_ | 7 | #ifndef TB_H_ |
8 | #define TB_H_ | 8 | #define TB_H_ |
9 | 9 | ||
10 | #include <linux/pci.h> | ||
11 | |||
12 | #include "tb_regs.h" | ||
10 | #include "ctl.h" | 13 | #include "ctl.h" |
11 | 14 | ||
12 | /** | 15 | /** |
16 | * struct tb_switch - a thunderbolt switch | ||
17 | */ | ||
18 | struct tb_switch { | ||
19 | struct tb_regs_switch_header config; | ||
20 | struct tb_port *ports; | ||
21 | struct tb *tb; | ||
22 | }; | ||
23 | |||
24 | /** | ||
25 | * struct tb_port - a thunderbolt port, part of a tb_switch | ||
26 | */ | ||
27 | struct tb_port { | ||
28 | struct tb_regs_port_header config; | ||
29 | struct tb_switch *sw; | ||
30 | struct tb_port *remote; /* remote port, NULL if not connected */ | ||
31 | u8 port; /* port number on switch */ | ||
32 | }; | ||
33 | |||
34 | /** | ||
13 | * struct tb - main thunderbolt bus structure | 35 | * struct tb - main thunderbolt bus structure |
14 | */ | 36 | */ |
15 | struct tb { | 37 | struct tb { |
@@ -20,6 +42,7 @@ struct tb { | |||
20 | struct tb_nhi *nhi; | 42 | struct tb_nhi *nhi; |
21 | struct tb_ctl *ctl; | 43 | struct tb_ctl *ctl; |
22 | struct workqueue_struct *wq; /* ordered workqueue for plug events */ | 44 | struct workqueue_struct *wq; /* ordered workqueue for plug events */ |
45 | struct tb_switch *root_switch; | ||
23 | bool hotplug_active; /* | 46 | bool hotplug_active; /* |
24 | * tb_handle_hotplug will stop progressing plug | 47 | * tb_handle_hotplug will stop progressing plug |
25 | * events and exit if this is not set (it needs to | 48 | * events and exit if this is not set (it needs to |
@@ -29,7 +52,122 @@ struct tb { | |||
29 | 52 | ||
30 | }; | 53 | }; |
31 | 54 | ||
55 | /* helper functions & macros */ | ||
56 | |||
57 | /** | ||
58 | * tb_upstream_port() - return the upstream port of a switch | ||
59 | * | ||
60 | * Every switch has an upstream port (for the root switch it is the NHI). | ||
61 | * | ||
62 | * During switch alloc/init tb_upstream_port()->remote may be NULL, even for | ||
63 | * non root switches (on the NHI port remote is always NULL). | ||
64 | * | ||
65 | * Return: Returns the upstream port of the switch. | ||
66 | */ | ||
67 | static inline struct tb_port *tb_upstream_port(struct tb_switch *sw) | ||
68 | { | ||
69 | return &sw->ports[sw->config.upstream_port_number]; | ||
70 | } | ||
71 | |||
72 | static inline u64 tb_route(struct tb_switch *sw) | ||
73 | { | ||
74 | return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo; | ||
75 | } | ||
76 | |||
77 | static inline int tb_sw_read(struct tb_switch *sw, void *buffer, | ||
78 | enum tb_cfg_space space, u32 offset, u32 length) | ||
79 | { | ||
80 | return tb_cfg_read(sw->tb->ctl, | ||
81 | buffer, | ||
82 | tb_route(sw), | ||
83 | 0, | ||
84 | space, | ||
85 | offset, | ||
86 | length); | ||
87 | } | ||
88 | |||
89 | static inline int tb_sw_write(struct tb_switch *sw, void *buffer, | ||
90 | enum tb_cfg_space space, u32 offset, u32 length) | ||
91 | { | ||
92 | return tb_cfg_write(sw->tb->ctl, | ||
93 | buffer, | ||
94 | tb_route(sw), | ||
95 | 0, | ||
96 | space, | ||
97 | offset, | ||
98 | length); | ||
99 | } | ||
100 | |||
101 | static inline int tb_port_read(struct tb_port *port, void *buffer, | ||
102 | enum tb_cfg_space space, u32 offset, u32 length) | ||
103 | { | ||
104 | return tb_cfg_read(port->sw->tb->ctl, | ||
105 | buffer, | ||
106 | tb_route(port->sw), | ||
107 | port->port, | ||
108 | space, | ||
109 | offset, | ||
110 | length); | ||
111 | } | ||
112 | |||
113 | static inline int tb_port_write(struct tb_port *port, void *buffer, | ||
114 | enum tb_cfg_space space, u32 offset, u32 length) | ||
115 | { | ||
116 | return tb_cfg_write(port->sw->tb->ctl, | ||
117 | buffer, | ||
118 | tb_route(port->sw), | ||
119 | port->port, | ||
120 | space, | ||
121 | offset, | ||
122 | length); | ||
123 | } | ||
124 | |||
125 | #define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg) | ||
126 | #define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg) | ||
127 | #define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg) | ||
128 | #define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg) | ||
129 | |||
130 | |||
131 | #define __TB_SW_PRINT(level, sw, fmt, arg...) \ | ||
132 | do { \ | ||
133 | struct tb_switch *__sw = (sw); \ | ||
134 | level(__sw->tb, "%llx: " fmt, \ | ||
135 | tb_route(__sw), ## arg); \ | ||
136 | } while (0) | ||
137 | #define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg) | ||
138 | #define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg) | ||
139 | #define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg) | ||
140 | |||
141 | |||
142 | #define __TB_PORT_PRINT(level, _port, fmt, arg...) \ | ||
143 | do { \ | ||
144 | struct tb_port *__port = (_port); \ | ||
145 | level(__port->sw->tb, "%llx:%x: " fmt, \ | ||
146 | tb_route(__port->sw), __port->port, ## arg); \ | ||
147 | } while (0) | ||
148 | #define tb_port_WARN(port, fmt, arg...) \ | ||
149 | __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg) | ||
150 | #define tb_port_warn(port, fmt, arg...) \ | ||
151 | __TB_PORT_PRINT(tb_warn, port, fmt, ##arg) | ||
152 | #define tb_port_info(port, fmt, arg...) \ | ||
153 | __TB_PORT_PRINT(tb_info, port, fmt, ##arg) | ||
154 | |||
155 | |||
32 | struct tb *thunderbolt_alloc_and_start(struct tb_nhi *nhi); | 156 | struct tb *thunderbolt_alloc_and_start(struct tb_nhi *nhi); |
33 | void thunderbolt_shutdown_and_free(struct tb *tb); | 157 | void thunderbolt_shutdown_and_free(struct tb *tb); |
34 | 158 | ||
159 | struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route); | ||
160 | void tb_switch_free(struct tb_switch *sw); | ||
161 | |||
162 | |||
163 | static inline int tb_route_length(u64 route) | ||
164 | { | ||
165 | return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT; | ||
166 | } | ||
167 | |||
168 | static inline bool tb_is_upstream_port(struct tb_port *port) | ||
169 | { | ||
170 | return port == tb_upstream_port(port->sw); | ||
171 | } | ||
172 | |||
35 | #endif | 173 | #endif |