Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Thunderbolt Cactus Ridge driver - bus logic (NHI independent) |
| 3 | * |
| 4 | * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com> |
| 5 | */ |
| 6 | |
| 7 | #ifndef TB_H_ |
| 8 | #define TB_H_ |
| 9 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 10 | #include <linux/pci.h> |
| 11 | |
| 12 | #include "tb_regs.h" |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 13 | #include "ctl.h" |
| 14 | |
| 15 | /** |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 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; |
Andreas Noever | ca389f7 | 2014-06-03 22:04:04 +0200 | [diff] [blame] | 22 | int cap_plug_events; /* offset, zero if not found */ |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | /** |
| 26 | * struct tb_port - a thunderbolt port, part of a tb_switch |
| 27 | */ |
| 28 | struct tb_port { |
| 29 | struct tb_regs_port_header config; |
| 30 | struct tb_switch *sw; |
| 31 | struct tb_port *remote; /* remote port, NULL if not connected */ |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame^] | 32 | int cap_phy; /* offset, zero if not found */ |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 33 | u8 port; /* port number on switch */ |
| 34 | }; |
| 35 | |
| 36 | /** |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 37 | * struct tb - main thunderbolt bus structure |
| 38 | */ |
| 39 | struct tb { |
| 40 | struct mutex lock; /* |
| 41 | * Big lock. Must be held when accessing cfg or |
| 42 | * any struct tb_switch / struct tb_port. |
| 43 | */ |
| 44 | struct tb_nhi *nhi; |
| 45 | struct tb_ctl *ctl; |
| 46 | struct workqueue_struct *wq; /* ordered workqueue for plug events */ |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 47 | struct tb_switch *root_switch; |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 48 | bool hotplug_active; /* |
| 49 | * tb_handle_hotplug will stop progressing plug |
| 50 | * events and exit if this is not set (it needs to |
| 51 | * acquire the lock one more time). Used to drain |
| 52 | * wq after cfg has been paused. |
| 53 | */ |
| 54 | |
| 55 | }; |
| 56 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 57 | /* helper functions & macros */ |
| 58 | |
| 59 | /** |
| 60 | * tb_upstream_port() - return the upstream port of a switch |
| 61 | * |
| 62 | * Every switch has an upstream port (for the root switch it is the NHI). |
| 63 | * |
| 64 | * During switch alloc/init tb_upstream_port()->remote may be NULL, even for |
| 65 | * non root switches (on the NHI port remote is always NULL). |
| 66 | * |
| 67 | * Return: Returns the upstream port of the switch. |
| 68 | */ |
| 69 | static inline struct tb_port *tb_upstream_port(struct tb_switch *sw) |
| 70 | { |
| 71 | return &sw->ports[sw->config.upstream_port_number]; |
| 72 | } |
| 73 | |
| 74 | static inline u64 tb_route(struct tb_switch *sw) |
| 75 | { |
| 76 | return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo; |
| 77 | } |
| 78 | |
| 79 | static inline int tb_sw_read(struct tb_switch *sw, void *buffer, |
| 80 | enum tb_cfg_space space, u32 offset, u32 length) |
| 81 | { |
| 82 | return tb_cfg_read(sw->tb->ctl, |
| 83 | buffer, |
| 84 | tb_route(sw), |
| 85 | 0, |
| 86 | space, |
| 87 | offset, |
| 88 | length); |
| 89 | } |
| 90 | |
| 91 | static inline int tb_sw_write(struct tb_switch *sw, void *buffer, |
| 92 | enum tb_cfg_space space, u32 offset, u32 length) |
| 93 | { |
| 94 | return tb_cfg_write(sw->tb->ctl, |
| 95 | buffer, |
| 96 | tb_route(sw), |
| 97 | 0, |
| 98 | space, |
| 99 | offset, |
| 100 | length); |
| 101 | } |
| 102 | |
| 103 | static inline int tb_port_read(struct tb_port *port, void *buffer, |
| 104 | enum tb_cfg_space space, u32 offset, u32 length) |
| 105 | { |
| 106 | return tb_cfg_read(port->sw->tb->ctl, |
| 107 | buffer, |
| 108 | tb_route(port->sw), |
| 109 | port->port, |
| 110 | space, |
| 111 | offset, |
| 112 | length); |
| 113 | } |
| 114 | |
| 115 | static inline int tb_port_write(struct tb_port *port, void *buffer, |
| 116 | enum tb_cfg_space space, u32 offset, u32 length) |
| 117 | { |
| 118 | return tb_cfg_write(port->sw->tb->ctl, |
| 119 | buffer, |
| 120 | tb_route(port->sw), |
| 121 | port->port, |
| 122 | space, |
| 123 | offset, |
| 124 | length); |
| 125 | } |
| 126 | |
| 127 | #define tb_err(tb, fmt, arg...) dev_err(&(tb)->nhi->pdev->dev, fmt, ## arg) |
| 128 | #define tb_WARN(tb, fmt, arg...) dev_WARN(&(tb)->nhi->pdev->dev, fmt, ## arg) |
| 129 | #define tb_warn(tb, fmt, arg...) dev_warn(&(tb)->nhi->pdev->dev, fmt, ## arg) |
| 130 | #define tb_info(tb, fmt, arg...) dev_info(&(tb)->nhi->pdev->dev, fmt, ## arg) |
| 131 | |
| 132 | |
| 133 | #define __TB_SW_PRINT(level, sw, fmt, arg...) \ |
| 134 | do { \ |
| 135 | struct tb_switch *__sw = (sw); \ |
| 136 | level(__sw->tb, "%llx: " fmt, \ |
| 137 | tb_route(__sw), ## arg); \ |
| 138 | } while (0) |
| 139 | #define tb_sw_WARN(sw, fmt, arg...) __TB_SW_PRINT(tb_WARN, sw, fmt, ##arg) |
| 140 | #define tb_sw_warn(sw, fmt, arg...) __TB_SW_PRINT(tb_warn, sw, fmt, ##arg) |
| 141 | #define tb_sw_info(sw, fmt, arg...) __TB_SW_PRINT(tb_info, sw, fmt, ##arg) |
| 142 | |
| 143 | |
| 144 | #define __TB_PORT_PRINT(level, _port, fmt, arg...) \ |
| 145 | do { \ |
| 146 | struct tb_port *__port = (_port); \ |
| 147 | level(__port->sw->tb, "%llx:%x: " fmt, \ |
| 148 | tb_route(__port->sw), __port->port, ## arg); \ |
| 149 | } while (0) |
| 150 | #define tb_port_WARN(port, fmt, arg...) \ |
| 151 | __TB_PORT_PRINT(tb_WARN, port, fmt, ##arg) |
| 152 | #define tb_port_warn(port, fmt, arg...) \ |
| 153 | __TB_PORT_PRINT(tb_warn, port, fmt, ##arg) |
| 154 | #define tb_port_info(port, fmt, arg...) \ |
| 155 | __TB_PORT_PRINT(tb_info, port, fmt, ##arg) |
| 156 | |
| 157 | |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 158 | struct tb *thunderbolt_alloc_and_start(struct tb_nhi *nhi); |
| 159 | void thunderbolt_shutdown_and_free(struct tb *tb); |
| 160 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 161 | struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route); |
| 162 | void tb_switch_free(struct tb_switch *sw); |
| 163 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame^] | 164 | int tb_wait_for_port(struct tb_port *port, bool wait_if_unplugged); |
| 165 | |
Andreas Noever | e2b8785 | 2014-06-03 22:04:03 +0200 | [diff] [blame] | 166 | int tb_find_cap(struct tb_port *port, enum tb_cfg_space space, u32 value); |
| 167 | |
Andreas Noever | a25c8b2 | 2014-06-03 22:04:02 +0200 | [diff] [blame] | 168 | |
| 169 | static inline int tb_route_length(u64 route) |
| 170 | { |
| 171 | return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT; |
| 172 | } |
| 173 | |
| 174 | static inline bool tb_is_upstream_port(struct tb_port *port) |
| 175 | { |
| 176 | return port == tb_upstream_port(port->sw); |
| 177 | } |
| 178 | |
Andreas Noever | 9da672a | 2014-06-03 22:04:05 +0200 | [diff] [blame^] | 179 | /** |
| 180 | * tb_downstream_route() - get route to downstream switch |
| 181 | * |
| 182 | * Port must not be the upstream port (otherwise a loop is created). |
| 183 | * |
| 184 | * Return: Returns a route to the switch behind @port. |
| 185 | */ |
| 186 | static inline u64 tb_downstream_route(struct tb_port *port) |
| 187 | { |
| 188 | return tb_route(port->sw) |
| 189 | | ((u64) port->port << (port->sw->config.depth * 8)); |
| 190 | } |
| 191 | |
Andreas Noever | d6cc51c | 2014-06-03 22:04:00 +0200 | [diff] [blame] | 192 | #endif |