2 * Thunderbolt Cactus Ridge driver - bus logic (NHI independent)
4 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
10 #include <linux/pci.h>
16 * struct tb_switch - a thunderbolt switch
19 struct tb_regs_switch_header config;
20 struct tb_port *ports;
25 * struct tb_port - a thunderbolt port, part of a tb_switch
28 struct tb_regs_port_header config;
30 struct tb_port *remote; /* remote port, NULL if not connected */
31 u8 port; /* port number on switch */
35 * struct tb - main thunderbolt bus structure
39 * Big lock. Must be held when accessing cfg or
40 * any struct tb_switch / struct tb_port.
44 struct workqueue_struct *wq; /* ordered workqueue for plug events */
45 struct tb_switch *root_switch;
46 bool hotplug_active; /*
47 * tb_handle_hotplug will stop progressing plug
48 * events and exit if this is not set (it needs to
49 * acquire the lock one more time). Used to drain
50 * wq after cfg has been paused.
55 /* helper functions & macros */
58 * tb_upstream_port() - return the upstream port of a switch
60 * Every switch has an upstream port (for the root switch it is the NHI).
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).
65 * Return: Returns the upstream port of the switch.
67 static inline struct tb_port *tb_upstream_port(struct tb_switch *sw)
69 return &sw->ports[sw->config.upstream_port_number];
72 static inline u64 tb_route(struct tb_switch *sw)
74 return ((u64) sw->config.route_hi) << 32 | sw->config.route_lo;
77 static inline int tb_sw_read(struct tb_switch *sw, void *buffer,
78 enum tb_cfg_space space, u32 offset, u32 length)
80 return tb_cfg_read(sw->tb->ctl,
89 static inline int tb_sw_write(struct tb_switch *sw, void *buffer,
90 enum tb_cfg_space space, u32 offset, u32 length)
92 return tb_cfg_write(sw->tb->ctl,
101 static inline int tb_port_read(struct tb_port *port, void *buffer,
102 enum tb_cfg_space space, u32 offset, u32 length)
104 return tb_cfg_read(port->sw->tb->ctl,
113 static inline int tb_port_write(struct tb_port *port, void *buffer,
114 enum tb_cfg_space space, u32 offset, u32 length)
116 return tb_cfg_write(port->sw->tb->ctl,
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)
131 #define __TB_SW_PRINT(level, sw, fmt, arg...) \
133 struct tb_switch *__sw = (sw); \
134 level(__sw->tb, "%llx: " fmt, \
135 tb_route(__sw), ## arg); \
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)
142 #define __TB_PORT_PRINT(level, _port, fmt, arg...) \
144 struct tb_port *__port = (_port); \
145 level(__port->sw->tb, "%llx:%x: " fmt, \
146 tb_route(__port->sw), __port->port, ## arg); \
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)
156 struct tb *thunderbolt_alloc_and_start(struct tb_nhi *nhi);
157 void thunderbolt_shutdown_and_free(struct tb *tb);
159 struct tb_switch *tb_switch_alloc(struct tb *tb, u64 route);
160 void tb_switch_free(struct tb_switch *sw);
163 static inline int tb_route_length(u64 route)
165 return (fls64(route) + TB_ROUTE_SHIFT - 1) / TB_ROUTE_SHIFT;
168 static inline bool tb_is_upstream_port(struct tb_port *port)
170 return port == tb_upstream_port(port->sw);