blob: e3799a53a193066ec87e277ad643e8091e41d26d [file] [log] [blame]
Greg Kroah-Hartmaneb50fd32017-11-07 14:58:41 +01001// SPDX-License-Identifier: GPL-2.0
Alex Elderc68adb22014-10-01 21:54:14 -05002/*
3 * Greybus connections
4 *
5 * Copyright 2014 Google Inc.
Alex Eldera46e9672014-12-12 12:08:42 -06006 * Copyright 2014 Linaro Ltd.
Alex Elderc68adb22014-10-01 21:54:14 -05007 */
8
Johan Hovold5a5bc352015-07-23 10:50:02 +02009#include <linux/workqueue.h>
Greg Kroah-Hartmanec0ad862019-08-25 07:54:27 +020010#include <linux/greybus.h>
Johan Hovold5a5bc352015-07-23 10:50:02 +020011
Alex Elder79c8c642016-06-03 15:55:37 -050012#include "greybus_trace.h"
Alex Elderc68adb22014-10-01 21:54:14 -050013
Johan Hovoldaac08392016-08-26 12:55:49 +020014#define GB_CONNECTION_CPORT_QUIESCE_TIMEOUT 1000
15
Johan Hovold0e46fab2016-01-19 12:51:25 +010016static void gb_connection_kref_release(struct kref *kref);
17
Alex Elder748e1232014-10-03 14:14:22 -050018static DEFINE_SPINLOCK(gb_connections_lock);
Johan Hovold210b5082016-01-19 12:51:26 +010019static DEFINE_MUTEX(gb_connection_mutex);
20
Johan Hovoldb53e0c92016-01-19 12:51:27 +010021/* Caller holds gb_connection_mutex. */
Viresh Kumar54131222016-06-10 14:59:09 +053022static bool gb_connection_cport_in_use(struct gb_interface *intf, u16 cport_id)
Alex Elderf5c2be92015-06-09 17:42:58 -050023{
Johan Hovold25376362015-11-03 18:03:23 +010024 struct gb_host_device *hd = intf->hd;
Alex Elderf5c2be92015-06-09 17:42:58 -050025 struct gb_connection *connection;
26
Johan Hovold0daf17b2015-11-25 15:59:12 +010027 list_for_each_entry(connection, &hd->connections, hd_links) {
28 if (connection->intf == intf &&
Cristian Sicilia8478c352018-11-25 17:58:15 +010029 connection->intf_cport_id == cport_id)
Viresh Kumar54131222016-06-10 14:59:09 +053030 return true;
Johan Hovold0daf17b2015-11-25 15:59:12 +010031 }
32
Viresh Kumar54131222016-06-10 14:59:09 +053033 return false;
Alex Elderf5c2be92015-06-09 17:42:58 -050034}
35
Johan Hovold0e46fab2016-01-19 12:51:25 +010036static void gb_connection_get(struct gb_connection *connection)
37{
38 kref_get(&connection->kref);
Alex Elder79c8c642016-06-03 15:55:37 -050039
40 trace_gb_connection_get(connection);
Johan Hovold0e46fab2016-01-19 12:51:25 +010041}
42
43static void gb_connection_put(struct gb_connection *connection)
44{
Alex Elder79c8c642016-06-03 15:55:37 -050045 trace_gb_connection_put(connection);
46
Johan Hovold0e46fab2016-01-19 12:51:25 +010047 kref_put(&connection->kref, gb_connection_kref_release);
48}
49
50/*
51 * Returns a reference-counted pointer to the connection if found.
52 */
Alex Elder8bd0ae62015-06-08 12:05:11 -050053static struct gb_connection *
Johan Hovold25376362015-11-03 18:03:23 +010054gb_connection_hd_find(struct gb_host_device *hd, u16 cport_id)
Alex Elderee9ebe42014-10-06 06:53:08 -050055{
Alex Elderf5c2be92015-06-09 17:42:58 -050056 struct gb_connection *connection;
Johan Hovold8f5eadb2015-03-02 09:55:26 +010057 unsigned long flags;
Alex Elderee9ebe42014-10-06 06:53:08 -050058
Johan Hovold8f5eadb2015-03-02 09:55:26 +010059 spin_lock_irqsave(&gb_connections_lock, flags);
Alex Elder2c43ce42014-11-17 08:08:44 -060060 list_for_each_entry(connection, &hd->connections, hd_links)
Johan Hovold0e46fab2016-01-19 12:51:25 +010061 if (connection->hd_cport_id == cport_id) {
62 gb_connection_get(connection);
Marti Bolivare86905b2014-10-06 13:26:02 -040063 goto found;
Johan Hovold0e46fab2016-01-19 12:51:25 +010064 }
Marti Bolivare86905b2014-10-06 13:26:02 -040065 connection = NULL;
Alex Elderf5c2be92015-06-09 17:42:58 -050066found:
Johan Hovold8f5eadb2015-03-02 09:55:26 +010067 spin_unlock_irqrestore(&gb_connections_lock, flags);
Alex Elderee9ebe42014-10-06 06:53:08 -050068
69 return connection;
70}
71
Alex Elderde3557d2014-11-20 16:09:18 -060072/*
73 * Callback from the host driver to let us know that data has been
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -050074 * received on the bundle.
Alex Elderde3557d2014-11-20 16:09:18 -060075 */
Johan Hovold25376362015-11-03 18:03:23 +010076void greybus_data_rcvd(struct gb_host_device *hd, u16 cport_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +010077 u8 *data, size_t length)
Alex Elder374e6a22014-11-17 18:08:37 -060078{
79 struct gb_connection *connection;
80
Alex Elder495787a2016-06-03 15:55:38 -050081 trace_gb_hd_in(hd);
82
Viresh Kumar12eba9f2015-05-20 16:48:00 +053083 connection = gb_connection_hd_find(hd, cport_id);
Alex Elder374e6a22014-11-17 18:08:37 -060084 if (!connection) {
Johan Hovold2adaefb2015-11-25 15:59:02 +010085 dev_err(&hd->dev,
Alex Elder374e6a22014-11-17 18:08:37 -060086 "nonexistent connection (%zu bytes dropped)\n", length);
87 return;
88 }
Alex Elder61089e82014-11-18 13:26:50 -060089 gb_connection_recv(connection, data, length);
Johan Hovold0e46fab2016-01-19 12:51:25 +010090 gb_connection_put(connection);
Alex Elder374e6a22014-11-17 18:08:37 -060091}
Alex Elderde3557d2014-11-20 16:09:18 -060092EXPORT_SYMBOL_GPL(greybus_data_rcvd);
Alex Elder374e6a22014-11-17 18:08:37 -060093
Greg Kroah-Hartmanb750fa32015-10-16 16:56:38 -070094static void gb_connection_kref_release(struct kref *kref)
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +080095{
Greg Kroah-Hartmanb750fa32015-10-16 16:56:38 -070096 struct gb_connection *connection;
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +080097
Greg Kroah-Hartmanb750fa32015-10-16 16:56:38 -070098 connection = container_of(kref, struct gb_connection, kref);
Johan Hovoldc3681f62016-01-19 12:51:23 +010099
Alex Elder79c8c642016-06-03 15:55:37 -0500100 trace_gb_connection_release(connection);
101
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +0800102 kfree(connection);
103}
104
Johan Hovold729b2602015-11-25 15:59:14 +0100105static void gb_connection_init_name(struct gb_connection *connection)
106{
107 u16 hd_cport_id = connection->hd_cport_id;
108 u16 cport_id = 0;
109 u8 intf_id = 0;
110
111 if (connection->intf) {
112 intf_id = connection->intf->interface_id;
113 cport_id = connection->intf_cport_id;
114 }
115
116 snprintf(connection->name, sizeof(connection->name),
Cristian Sicilia8478c352018-11-25 17:58:15 +0100117 "%u/%u:%u", hd_cport_id, intf_id, cport_id);
Johan Hovold729b2602015-11-25 15:59:14 +0100118}
119
Alex Elder177404b2014-10-03 14:14:24 -0500120/*
Johan Hovold96c2af52016-01-21 17:34:15 +0100121 * _gb_connection_create() - create a Greybus connection
Johan Hovold2566fae62015-11-25 15:59:11 +0100122 * @hd: host device of the connection
123 * @hd_cport_id: host-device cport id, or -1 for dynamic allocation
124 * @intf: remote interface, or NULL for static connections
125 * @bundle: remote-interface bundle (may be NULL)
126 * @cport_id: remote-interface cport id, or 0 for static connections
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100127 * @handler: request handler (may be NULL)
Johan Hovoldcb033182016-03-03 13:34:36 +0100128 * @flags: connection flags
Johan Hovold2566fae62015-11-25 15:59:11 +0100129 *
130 * Create a Greybus connection, representing the bidirectional link
Alex Elderc68adb22014-10-01 21:54:14 -0500131 * between a CPort on a (local) Greybus host device and a CPort on
Johan Hovold2566fae62015-11-25 15:59:11 +0100132 * another Greybus interface.
Alex Elderc68adb22014-10-01 21:54:14 -0500133 *
Alex Eldere88afa52014-10-01 21:54:15 -0500134 * A connection also maintains the state of operations sent over the
135 * connection.
136 *
Johan Hovold210b5082016-01-19 12:51:26 +0100137 * Serialised against concurrent create and destroy using the
138 * gb_connection_mutex.
139 *
Johan Hovold24e094d2016-01-21 17:34:16 +0100140 * Return: A pointer to the new connection if successful, or an ERR_PTR
141 * otherwise.
Alex Elderc68adb22014-10-01 21:54:14 -0500142 */
Johan Hovold2566fae62015-11-25 15:59:11 +0100143static struct gb_connection *
Johan Hovold96c2af52016-01-21 17:34:15 +0100144_gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100145 struct gb_interface *intf,
146 struct gb_bundle *bundle, int cport_id,
147 gb_request_handler_t handler,
148 unsigned long flags)
Alex Elderc68adb22014-10-01 21:54:14 -0500149{
150 struct gb_connection *connection;
Johan Hovold24e094d2016-01-21 17:34:16 +0100151 int ret;
Alex Elderc68adb22014-10-01 21:54:14 -0500152
Johan Hovold210b5082016-01-19 12:51:26 +0100153 mutex_lock(&gb_connection_mutex);
154
Viresh Kumar54131222016-06-10 14:59:09 +0530155 if (intf && gb_connection_cport_in_use(intf, cport_id)) {
Johan Hovoldb53e0c92016-01-19 12:51:27 +0100156 dev_err(&intf->dev, "cport %u already in use\n", cport_id);
Johan Hovold24e094d2016-01-21 17:34:16 +0100157 ret = -EBUSY;
Johan Hovoldb53e0c92016-01-19 12:51:27 +0100158 goto err_unlock;
159 }
160
Johan Hovoldf2aae1c2016-05-11 10:18:02 +0200161 ret = gb_hd_cport_allocate(hd, hd_cport_id, flags);
Johan Hovold74a5d932016-05-11 10:17:59 +0200162 if (ret < 0) {
163 dev_err(&hd->dev, "failed to allocate cport: %d\n", ret);
Johan Hovold210b5082016-01-19 12:51:26 +0100164 goto err_unlock;
Johan Hovold74a5d932016-05-11 10:17:59 +0200165 }
Johan Hovold24e094d2016-01-21 17:34:16 +0100166 hd_cport_id = ret;
Alex Elderc68adb22014-10-01 21:54:14 -0500167
Johan Hovold10f9fa12015-07-23 10:50:01 +0200168 connection = kzalloc(sizeof(*connection), GFP_KERNEL);
Johan Hovold24e094d2016-01-21 17:34:16 +0100169 if (!connection) {
170 ret = -ENOMEM;
Johan Hovold74a5d932016-05-11 10:17:59 +0200171 goto err_hd_cport_release;
Johan Hovold24e094d2016-01-21 17:34:16 +0100172 }
Johan Hovold10f9fa12015-07-23 10:50:01 +0200173
Johan Hovold7c63a822015-07-23 10:50:00 +0200174 connection->hd_cport_id = hd_cport_id;
Alex Elderf9b0366f12015-06-12 10:21:08 -0500175 connection->intf_cport_id = cport_id;
176 connection->hd = hd;
Johan Hovold2566fae62015-11-25 15:59:11 +0100177 connection->intf = intf;
Greg Kroah-Hartman1db0a5f2014-12-12 17:10:17 -0500178 connection->bundle = bundle;
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100179 connection->handler = handler;
Johan Hovoldcb033182016-03-03 13:34:36 +0100180 connection->flags = flags;
Johan Hovold0e9b41a2016-05-11 10:17:55 +0200181 if (intf && (intf->quirks & GB_INTERFACE_QUIRK_NO_CPORT_FEATURES))
182 connection->flags |= GB_CONNECTION_FLAG_NO_FLOWCTRL;
Alex Elder36561f22014-10-22 02:04:30 -0500183 connection->state = GB_CONNECTION_STATE_DISABLED;
Alex Elderad1c4492014-10-02 12:30:06 -0500184
Johan Hovold4e2b1e42015-07-22 17:49:19 +0200185 atomic_set(&connection->op_cycle, 0);
Johan Hovold23268782016-01-19 12:51:06 +0100186 mutex_init(&connection->mutex);
Johan Hovold4e2b1e42015-07-22 17:49:19 +0200187 spin_lock_init(&connection->lock);
188 INIT_LIST_HEAD(&connection->operations);
189
Johan Hovold5a5bc352015-07-23 10:50:02 +0200190 connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
Johan Hovold582b3a12015-11-25 15:59:03 +0100191 dev_name(&hd->dev), hd_cport_id);
Johan Hovold24e094d2016-01-21 17:34:16 +0100192 if (!connection->wq) {
193 ret = -ENOMEM;
Johan Hovold5a5bc352015-07-23 10:50:02 +0200194 goto err_free_connection;
Johan Hovold24e094d2016-01-21 17:34:16 +0100195 }
Johan Hovold5a5bc352015-07-23 10:50:02 +0200196
Greg Kroah-Hartmanb750fa32015-10-16 16:56:38 -0700197 kref_init(&connection->kref);
Greg Kroah-Hartmanf0f61b92014-10-24 17:34:46 +0800198
Johan Hovold729b2602015-11-25 15:59:14 +0100199 gb_connection_init_name(connection);
200
Johan Hovold0b1d2622016-06-27 20:07:10 +0200201 spin_lock_irq(&gb_connections_lock);
Viresh Kumar928f2ab2015-06-04 18:16:45 +0530202 list_add(&connection->hd_links, &hd->connections);
Viresh Kumar75662e52015-07-21 17:44:16 +0530203
204 if (bundle)
205 list_add(&connection->bundle_links, &bundle->connections);
206 else
207 INIT_LIST_HEAD(&connection->bundle_links);
208
Johan Hovold0b1d2622016-06-27 20:07:10 +0200209 spin_unlock_irq(&gb_connections_lock);
Alex Elder748e1232014-10-03 14:14:22 -0500210
Johan Hovold210b5082016-01-19 12:51:26 +0100211 mutex_unlock(&gb_connection_mutex);
212
Alex Elder79c8c642016-06-03 15:55:37 -0500213 trace_gb_connection_create(connection);
214
Alex Elderc68adb22014-10-01 21:54:14 -0500215 return connection;
Johan Hovold10f9fa12015-07-23 10:50:01 +0200216
Johan Hovold5a5bc352015-07-23 10:50:02 +0200217err_free_connection:
218 kfree(connection);
Johan Hovold74a5d932016-05-11 10:17:59 +0200219err_hd_cport_release:
220 gb_hd_cport_release(hd, hd_cport_id);
Johan Hovold210b5082016-01-19 12:51:26 +0100221err_unlock:
222 mutex_unlock(&gb_connection_mutex);
Johan Hovold10f9fa12015-07-23 10:50:01 +0200223
Johan Hovold24e094d2016-01-21 17:34:16 +0100224 return ERR_PTR(ret);
Alex Elderc68adb22014-10-01 21:54:14 -0500225}
226
Johan Hovold2566fae62015-11-25 15:59:11 +0100227struct gb_connection *
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100228gb_connection_create_static(struct gb_host_device *hd, u16 hd_cport_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100229 gb_request_handler_t handler)
Johan Hovold2566fae62015-11-25 15:59:11 +0100230{
Johan Hovoldcb033182016-03-03 13:34:36 +0100231 return _gb_connection_create(hd, hd_cport_id, NULL, NULL, 0, handler,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100232 GB_CONNECTION_FLAG_HIGH_PRIO);
Johan Hovold2566fae62015-11-25 15:59:11 +0100233}
234
235struct gb_connection *
Johan Hovold59507e22016-01-21 17:34:12 +0100236gb_connection_create_control(struct gb_interface *intf)
237{
Johan Hovoldaca7aab2016-05-27 17:26:25 +0200238 return _gb_connection_create(intf->hd, -1, intf, NULL, 0, NULL,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100239 GB_CONNECTION_FLAG_CONTROL |
240 GB_CONNECTION_FLAG_HIGH_PRIO);
Johan Hovold59507e22016-01-21 17:34:12 +0100241}
242
243struct gb_connection *
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100244gb_connection_create(struct gb_bundle *bundle, u16 cport_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100245 gb_request_handler_t handler)
Johan Hovold2566fae62015-11-25 15:59:11 +0100246{
Johan Hovold8cff6c62016-01-21 17:34:14 +0100247 struct gb_interface *intf = bundle->intf;
248
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100249 return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100250 handler, 0);
Johan Hovold2566fae62015-11-25 15:59:11 +0100251}
Johan Hovold96c2af52016-01-21 17:34:15 +0100252EXPORT_SYMBOL_GPL(gb_connection_create);
Johan Hovold2566fae62015-11-25 15:59:11 +0100253
Johan Hovoldcb033182016-03-03 13:34:36 +0100254struct gb_connection *
255gb_connection_create_flags(struct gb_bundle *bundle, u16 cport_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100256 gb_request_handler_t handler,
257 unsigned long flags)
Johan Hovoldcb033182016-03-03 13:34:36 +0100258{
259 struct gb_interface *intf = bundle->intf;
260
Johan Hovold1ba30c32016-06-22 11:42:03 +0200261 if (WARN_ON_ONCE(flags & GB_CONNECTION_FLAG_CORE_MASK))
262 flags &= ~GB_CONNECTION_FLAG_CORE_MASK;
263
Johan Hovoldcb033182016-03-03 13:34:36 +0100264 return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100265 handler, flags);
Johan Hovoldcb033182016-03-03 13:34:36 +0100266}
267EXPORT_SYMBOL_GPL(gb_connection_create_flags);
268
Johan Hovold781ac8662016-05-11 10:17:57 +0200269struct gb_connection *
270gb_connection_create_offloaded(struct gb_bundle *bundle, u16 cport_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100271 unsigned long flags)
Johan Hovold781ac8662016-05-11 10:17:57 +0200272{
Johan Hovold781ac8662016-05-11 10:17:57 +0200273 flags |= GB_CONNECTION_FLAG_OFFLOADED;
274
Johan Hovold1ba30c32016-06-22 11:42:03 +0200275 return gb_connection_create_flags(bundle, cport_id, NULL, flags);
Johan Hovold781ac8662016-05-11 10:17:57 +0200276}
277EXPORT_SYMBOL_GPL(gb_connection_create_offloaded);
278
Johan Hovoldd7ea30a52015-09-17 13:17:26 +0200279static int gb_connection_hd_cport_enable(struct gb_connection *connection)
280{
Johan Hovold25376362015-11-03 18:03:23 +0100281 struct gb_host_device *hd = connection->hd;
Johan Hovoldd7ea30a52015-09-17 13:17:26 +0200282 int ret;
283
284 if (!hd->driver->cport_enable)
285 return 0;
286
Johan Hovold6910fa22016-06-22 11:42:02 +0200287 ret = hd->driver->cport_enable(hd, connection->hd_cport_id,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100288 connection->flags);
Johan Hovoldd7ea30a52015-09-17 13:17:26 +0200289 if (ret) {
Viresh Kumar62e04622016-06-09 16:34:42 +0530290 dev_err(&hd->dev, "%s: failed to enable host cport: %d\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100291 connection->name, ret);
Johan Hovoldd7ea30a52015-09-17 13:17:26 +0200292 return ret;
293 }
294
295 return 0;
296}
297
298static void gb_connection_hd_cport_disable(struct gb_connection *connection)
299{
Johan Hovold25376362015-11-03 18:03:23 +0100300 struct gb_host_device *hd = connection->hd;
Johan Hovold3cbe52c2016-05-27 17:26:28 +0200301 int ret;
Johan Hovoldd7ea30a52015-09-17 13:17:26 +0200302
303 if (!hd->driver->cport_disable)
304 return;
305
Johan Hovold3cbe52c2016-05-27 17:26:28 +0200306 ret = hd->driver->cport_disable(hd, connection->hd_cport_id);
307 if (ret) {
Viresh Kumar62e04622016-06-09 16:34:42 +0530308 dev_err(&hd->dev, "%s: failed to disable host cport: %d\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100309 connection->name, ret);
Johan Hovold3cbe52c2016-05-27 17:26:28 +0200310 }
Johan Hovoldd7ea30a52015-09-17 13:17:26 +0200311}
312
Johan Hovoldaac08392016-08-26 12:55:49 +0200313static int gb_connection_hd_cport_connected(struct gb_connection *connection)
314{
315 struct gb_host_device *hd = connection->hd;
316 int ret;
317
318 if (!hd->driver->cport_connected)
319 return 0;
320
321 ret = hd->driver->cport_connected(hd, connection->hd_cport_id);
322 if (ret) {
323 dev_err(&hd->dev, "%s: failed to set connected state: %d\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100324 connection->name, ret);
Johan Hovoldaac08392016-08-26 12:55:49 +0200325 return ret;
326 }
327
328 return 0;
329}
330
Johan Hovold800d6c82016-05-27 17:26:37 +0200331static int gb_connection_hd_cport_flush(struct gb_connection *connection)
332{
333 struct gb_host_device *hd = connection->hd;
334 int ret;
335
336 if (!hd->driver->cport_flush)
337 return 0;
338
339 ret = hd->driver->cport_flush(hd, connection->hd_cport_id);
340 if (ret) {
341 dev_err(&hd->dev, "%s: failed to flush host cport: %d\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100342 connection->name, ret);
Johan Hovold800d6c82016-05-27 17:26:37 +0200343 return ret;
344 }
345
346 return 0;
347}
348
Johan Hovoldaac08392016-08-26 12:55:49 +0200349static int gb_connection_hd_cport_quiesce(struct gb_connection *connection)
Fabien Parent9ed5e1b2016-02-23 18:46:08 +0100350{
351 struct gb_host_device *hd = connection->hd;
Johan Hovoldaac08392016-08-26 12:55:49 +0200352 size_t peer_space;
Fabien Parent9ed5e1b2016-02-23 18:46:08 +0100353 int ret;
354
Jason Hrycayf05a88a2016-12-20 14:49:27 -0600355 if (!hd->driver->cport_quiesce)
356 return 0;
357
Johan Hovoldaac08392016-08-26 12:55:49 +0200358 peer_space = sizeof(struct gb_operation_msg_hdr) +
359 sizeof(struct gb_cport_shutdown_request);
Fabien Parent9ed5e1b2016-02-23 18:46:08 +0100360
Johan Hovoldaac08392016-08-26 12:55:49 +0200361 if (connection->mode_switch)
362 peer_space += sizeof(struct gb_operation_msg_hdr);
363
364 ret = hd->driver->cport_quiesce(hd, connection->hd_cport_id,
365 peer_space,
366 GB_CONNECTION_CPORT_QUIESCE_TIMEOUT);
Fabien Parent9ed5e1b2016-02-23 18:46:08 +0100367 if (ret) {
Johan Hovoldaac08392016-08-26 12:55:49 +0200368 dev_err(&hd->dev, "%s: failed to quiesce host cport: %d\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100369 connection->name, ret);
Fabien Parent9ed5e1b2016-02-23 18:46:08 +0100370 return ret;
371 }
372
373 return 0;
374}
375
Johan Hovoldaac08392016-08-26 12:55:49 +0200376static int gb_connection_hd_cport_clear(struct gb_connection *connection)
Fabien Parent9ed5e1b2016-02-23 18:46:08 +0100377{
378 struct gb_host_device *hd = connection->hd;
Johan Hovoldaac08392016-08-26 12:55:49 +0200379 int ret;
Fabien Parent9ed5e1b2016-02-23 18:46:08 +0100380
Jason Hrycayf05a88a2016-12-20 14:49:27 -0600381 if (!hd->driver->cport_clear)
382 return 0;
383
Johan Hovoldaac08392016-08-26 12:55:49 +0200384 ret = hd->driver->cport_clear(hd, connection->hd_cport_id);
385 if (ret) {
386 dev_err(&hd->dev, "%s: failed to clear host cport: %d\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100387 connection->name, ret);
Johan Hovoldaac08392016-08-26 12:55:49 +0200388 return ret;
389 }
Fabien Parent9ed5e1b2016-02-23 18:46:08 +0100390
Johan Hovoldaac08392016-08-26 12:55:49 +0200391 return 0;
Fabien Parent9ed5e1b2016-02-23 18:46:08 +0100392}
393
Alex Elderc68adb22014-10-01 21:54:14 -0500394/*
Johan Hovolda95c2582015-09-17 13:17:21 +0200395 * Request the SVC to create a connection from AP's cport to interface's
396 * cport.
397 */
398static int
399gb_connection_svc_connection_create(struct gb_connection *connection)
400{
Johan Hovold25376362015-11-03 18:03:23 +0100401 struct gb_host_device *hd = connection->hd;
Viresh Kumar1575ef12015-10-07 15:40:24 -0400402 struct gb_interface *intf;
Johan Hovold27f25c12016-03-03 13:34:38 +0100403 u8 cport_flags;
Johan Hovolda95c2582015-09-17 13:17:21 +0200404 int ret;
405
Johan Hovold4ec15742015-11-25 15:59:13 +0100406 if (gb_connection_is_static(connection))
Johan Hovold00ad6972016-05-27 17:26:31 +0200407 return 0;
Johan Hovolda95c2582015-09-17 13:17:21 +0200408
Johan Hovold35822c02015-11-25 15:59:22 +0100409 intf = connection->intf;
Johan Hovold27f25c12016-03-03 13:34:38 +0100410
Johan Hovoldec199cc2016-03-29 18:56:03 -0400411 /*
Johan Hovold0e9b41a2016-05-11 10:17:55 +0200412 * Enable either E2EFC or CSD, unless no flow control is requested.
Johan Hovoldec199cc2016-03-29 18:56:03 -0400413 */
Johan Hovold27f25c12016-03-03 13:34:38 +0100414 cport_flags = GB_SVC_CPORT_FLAG_CSV_N;
Johan Hovold0e9b41a2016-05-11 10:17:55 +0200415 if (gb_connection_flow_control_disabled(connection)) {
Johan Hovold27f25c12016-03-03 13:34:38 +0100416 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N;
Johan Hovold64a6d132016-03-03 13:34:39 +0100417 } else if (gb_connection_e2efc_enabled(connection)) {
Johan Hovold27f25c12016-03-03 13:34:38 +0100418 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N |
419 GB_SVC_CPORT_FLAG_E2EFC;
420 }
421
Johan Hovolda95c2582015-09-17 13:17:21 +0200422 ret = gb_svc_connection_create(hd->svc,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100423 hd->svc->ap_intf_id,
424 connection->hd_cport_id,
425 intf->interface_id,
426 connection->intf_cport_id,
427 cport_flags);
Johan Hovolda95c2582015-09-17 13:17:21 +0200428 if (ret) {
Johan Hovold4c4b5022015-11-25 15:59:15 +0100429 dev_err(&connection->hd->dev,
430 "%s: failed to create svc connection: %d\n",
431 connection->name, ret);
Johan Hovolda95c2582015-09-17 13:17:21 +0200432 return ret;
433 }
434
Johan Hovolda95c2582015-09-17 13:17:21 +0200435 return 0;
436}
437
Viresh Kumar1b7a9cd2015-09-07 16:01:22 +0530438static void
439gb_connection_svc_connection_destroy(struct gb_connection *connection)
440{
Johan Hovold4ec15742015-11-25 15:59:13 +0100441 if (gb_connection_is_static(connection))
Viresh Kumar1b7a9cd2015-09-07 16:01:22 +0530442 return;
443
444 gb_svc_connection_destroy(connection->hd->svc,
Johan Hovold66069fb2015-11-25 15:59:09 +0100445 connection->hd->svc->ap_intf_id,
Viresh Kumar1b7a9cd2015-09-07 16:01:22 +0530446 connection->hd_cport_id,
Johan Hovold35822c02015-11-25 15:59:22 +0100447 connection->intf->interface_id,
Viresh Kumar1b7a9cd2015-09-07 16:01:22 +0530448 connection->intf_cport_id);
449}
450
Johan Hovold9d7fc252015-09-17 13:17:24 +0200451/* Inform Interface about active CPorts */
452static int gb_connection_control_connected(struct gb_connection *connection)
453{
Johan Hovold9d7fc252015-09-17 13:17:24 +0200454 struct gb_control *control;
455 u16 cport_id = connection->intf_cport_id;
456 int ret;
457
Johan Hovoldbc3be172016-01-19 12:51:12 +0100458 if (gb_connection_is_static(connection))
Johan Hovold9d7fc252015-09-17 13:17:24 +0200459 return 0;
460
Johan Hovoldaca7aab2016-05-27 17:26:25 +0200461 if (gb_connection_is_control(connection))
Johan Hovoldbc3be172016-01-19 12:51:12 +0100462 return 0;
Johan Hovold9d7fc252015-09-17 13:17:24 +0200463
Johan Hovoldaca7aab2016-05-27 17:26:25 +0200464 control = connection->intf->control;
465
Johan Hovold9d7fc252015-09-17 13:17:24 +0200466 ret = gb_control_connected_operation(control, cport_id);
467 if (ret) {
Greg Kroah-Hartman30482c12015-10-16 16:56:23 -0700468 dev_err(&connection->bundle->dev,
469 "failed to connect cport: %d\n", ret);
Johan Hovold9d7fc252015-09-17 13:17:24 +0200470 return ret;
471 }
472
473 return 0;
474}
475
Johan Hovold3de5acf2016-05-27 17:26:36 +0200476static void
477gb_connection_control_disconnecting(struct gb_connection *connection)
478{
479 struct gb_control *control;
480 u16 cport_id = connection->intf_cport_id;
481 int ret;
482
483 if (gb_connection_is_static(connection))
484 return;
485
486 control = connection->intf->control;
487
488 ret = gb_control_disconnecting_operation(control, cport_id);
489 if (ret) {
490 dev_err(&connection->hd->dev,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100491 "%s: failed to send disconnecting: %d\n",
492 connection->name, ret);
Johan Hovold3de5acf2016-05-27 17:26:36 +0200493 }
494}
495
Johan Hovold72d74822015-09-17 13:17:23 +0200496static void
497gb_connection_control_disconnected(struct gb_connection *connection)
Viresh Kumar18690652015-08-11 07:35:56 +0530498{
499 struct gb_control *control;
Johan Hovold72d74822015-09-17 13:17:23 +0200500 u16 cport_id = connection->intf_cport_id;
Viresh Kumar18690652015-08-11 07:35:56 +0530501 int ret;
502
Johan Hovoldbc3be172016-01-19 12:51:12 +0100503 if (gb_connection_is_static(connection))
Viresh Kumar18690652015-08-11 07:35:56 +0530504 return;
505
Johan Hovoldaca7aab2016-05-27 17:26:25 +0200506 control = connection->intf->control;
507
Johan Hovold55742d22016-05-27 17:26:40 +0200508 if (gb_connection_is_control(connection)) {
509 if (connection->mode_switch) {
510 ret = gb_control_mode_switch_operation(control);
511 if (ret) {
512 /*
513 * Allow mode switch to time out waiting for
514 * mailbox event.
515 */
516 return;
517 }
518 }
519
520 return;
521 }
522
Viresh Kumar18690652015-08-11 07:35:56 +0530523 ret = gb_control_disconnected_operation(control, cport_id);
Johan Hovold72d74822015-09-17 13:17:23 +0200524 if (ret) {
Greg Kroah-Hartman30482c12015-10-16 16:56:23 -0700525 dev_warn(&connection->bundle->dev,
526 "failed to disconnect cport: %d\n", ret);
Johan Hovold72d74822015-09-17 13:17:23 +0200527 }
Viresh Kumar18690652015-08-11 07:35:56 +0530528}
529
Johan Hovoldaac08392016-08-26 12:55:49 +0200530static int gb_connection_shutdown_operation(struct gb_connection *connection,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100531 u8 phase)
Johan Hovold3de5acf2016-05-27 17:26:36 +0200532{
Johan Hovoldaac08392016-08-26 12:55:49 +0200533 struct gb_cport_shutdown_request *req;
Johan Hovold3de5acf2016-05-27 17:26:36 +0200534 struct gb_operation *operation;
535 int ret;
536
537 operation = gb_operation_create_core(connection,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100538 GB_REQUEST_TYPE_CPORT_SHUTDOWN,
539 sizeof(*req), 0, 0,
540 GFP_KERNEL);
Johan Hovold3de5acf2016-05-27 17:26:36 +0200541 if (!operation)
542 return -ENOMEM;
543
Johan Hovoldaac08392016-08-26 12:55:49 +0200544 req = operation->request->payload;
545 req->phase = phase;
546
Johan Hovold3de5acf2016-05-27 17:26:36 +0200547 ret = gb_operation_request_send_sync(operation);
548
549 gb_operation_put(operation);
550
551 return ret;
552}
553
Johan Hovoldaac08392016-08-26 12:55:49 +0200554static int gb_connection_cport_shutdown(struct gb_connection *connection,
555 u8 phase)
Johan Hovold3de5acf2016-05-27 17:26:36 +0200556{
557 struct gb_host_device *hd = connection->hd;
Johan Hovoldaac08392016-08-26 12:55:49 +0200558 const struct gb_hd_driver *drv = hd->driver;
Johan Hovold3de5acf2016-05-27 17:26:36 +0200559 int ret;
560
561 if (gb_connection_is_static(connection))
562 return 0;
563
564 if (gb_connection_is_offloaded(connection)) {
Johan Hovoldaac08392016-08-26 12:55:49 +0200565 if (!drv->cport_shutdown)
Johan Hovold3de5acf2016-05-27 17:26:36 +0200566 return 0;
567
Johan Hovoldaac08392016-08-26 12:55:49 +0200568 ret = drv->cport_shutdown(hd, connection->hd_cport_id, phase,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100569 GB_OPERATION_TIMEOUT_DEFAULT);
Johan Hovold3de5acf2016-05-27 17:26:36 +0200570 } else {
Johan Hovoldaac08392016-08-26 12:55:49 +0200571 ret = gb_connection_shutdown_operation(connection, phase);
Johan Hovold3de5acf2016-05-27 17:26:36 +0200572 }
573
574 if (ret) {
Johan Hovoldaac08392016-08-26 12:55:49 +0200575 dev_err(&hd->dev, "%s: failed to send cport shutdown (phase %d): %d\n",
Cristian Sicilia8478c352018-11-25 17:58:15 +0100576 connection->name, phase, ret);
Johan Hovold3de5acf2016-05-27 17:26:36 +0200577 return ret;
578 }
579
580 return 0;
581}
582
Johan Hovoldaac08392016-08-26 12:55:49 +0200583static int
584gb_connection_cport_shutdown_phase_1(struct gb_connection *connection)
585{
586 return gb_connection_cport_shutdown(connection, 1);
587}
588
589static int
590gb_connection_cport_shutdown_phase_2(struct gb_connection *connection)
591{
592 return gb_connection_cport_shutdown(connection, 2);
593}
594
Johan Hovold2846d3e2015-09-17 13:17:25 +0200595/*
Johan Hovold520c6ea2016-01-19 12:51:04 +0100596 * Cancel all active operations on a connection.
597 *
Johan Hovold3de5acf2016-05-27 17:26:36 +0200598 * Locking: Called with connection lock held and state set to DISABLED or
599 * DISCONNECTING.
Johan Hovold520c6ea2016-01-19 12:51:04 +0100600 */
601static void gb_connection_cancel_operations(struct gb_connection *connection,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100602 int errno)
Viresh Kumar127c1fb2016-01-28 15:50:48 +0530603 __must_hold(&connection->lock)
Johan Hovold520c6ea2016-01-19 12:51:04 +0100604{
605 struct gb_operation *operation;
606
607 while (!list_empty(&connection->operations)) {
608 operation = list_last_entry(&connection->operations,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100609 struct gb_operation, links);
Johan Hovold520c6ea2016-01-19 12:51:04 +0100610 gb_operation_get(operation);
Johan Hovolda29bac62016-06-27 20:07:09 +0200611 spin_unlock_irq(&connection->lock);
Johan Hovold520c6ea2016-01-19 12:51:04 +0100612
613 if (gb_operation_is_incoming(operation))
614 gb_operation_cancel_incoming(operation, errno);
615 else
616 gb_operation_cancel(operation, errno);
617
618 gb_operation_put(operation);
619
Johan Hovolda29bac62016-06-27 20:07:09 +0200620 spin_lock_irq(&connection->lock);
Johan Hovold520c6ea2016-01-19 12:51:04 +0100621 }
622}
623
Johan Hovoldbeb6b7f2016-01-19 12:51:08 +0100624/*
625 * Cancel all active incoming operations on a connection.
626 *
627 * Locking: Called with connection lock held and state set to ENABLED_TX.
628 */
629static void
630gb_connection_flush_incoming_operations(struct gb_connection *connection,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100631 int errno)
Viresh Kumar127c1fb2016-01-28 15:50:48 +0530632 __must_hold(&connection->lock)
Johan Hovoldbeb6b7f2016-01-19 12:51:08 +0100633{
634 struct gb_operation *operation;
635 bool incoming;
636
637 while (!list_empty(&connection->operations)) {
638 incoming = false;
639 list_for_each_entry(operation, &connection->operations,
Cristian Sicilia8478c352018-11-25 17:58:15 +0100640 links) {
Johan Hovoldbeb6b7f2016-01-19 12:51:08 +0100641 if (gb_operation_is_incoming(operation)) {
642 gb_operation_get(operation);
643 incoming = true;
644 break;
645 }
646 }
647
648 if (!incoming)
649 break;
650
Johan Hovolda29bac62016-06-27 20:07:09 +0200651 spin_unlock_irq(&connection->lock);
Johan Hovoldbeb6b7f2016-01-19 12:51:08 +0100652
653 /* FIXME: flush, not cancel? */
654 gb_operation_cancel_incoming(operation, errno);
655 gb_operation_put(operation);
656
Johan Hovolda29bac62016-06-27 20:07:09 +0200657 spin_lock_irq(&connection->lock);
Johan Hovoldbeb6b7f2016-01-19 12:51:08 +0100658 }
659}
660
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100661/*
662 * _gb_connection_enable() - enable a connection
663 * @connection: connection to enable
664 * @rx: whether to enable incoming requests
665 *
666 * Connection-enable helper for DISABLED->ENABLED, DISABLED->ENABLED_TX, and
667 * ENABLED_TX->ENABLED state transitions.
668 *
669 * Locking: Caller holds connection->mutex.
670 */
671static int _gb_connection_enable(struct gb_connection *connection, bool rx)
Alex Elder574341c2014-10-16 06:35:35 -0500672{
Alex Elder36561f22014-10-22 02:04:30 -0500673 int ret;
674
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100675 /* Handle ENABLED_TX -> ENABLED transitions. */
Johan Hovold570dfa72016-01-19 12:51:07 +0100676 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100677 if (!(connection->handler && rx))
678 return 0;
Johan Hovold570dfa72016-01-19 12:51:07 +0100679
Johan Hovolda29bac62016-06-27 20:07:09 +0200680 spin_lock_irq(&connection->lock);
Johan Hovold570dfa72016-01-19 12:51:07 +0100681 connection->state = GB_CONNECTION_STATE_ENABLED;
Johan Hovolda29bac62016-06-27 20:07:09 +0200682 spin_unlock_irq(&connection->lock);
Johan Hovold570dfa72016-01-19 12:51:07 +0100683
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100684 return 0;
Johan Hovold570dfa72016-01-19 12:51:07 +0100685 }
686
Johan Hovoldd7ea30a52015-09-17 13:17:26 +0200687 ret = gb_connection_hd_cport_enable(connection);
Johan Hovolda95c2582015-09-17 13:17:21 +0200688 if (ret)
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100689 return ret;
Viresh Kumara1163fa2015-09-07 16:01:21 +0530690
Johan Hovoldd7ea30a52015-09-17 13:17:26 +0200691 ret = gb_connection_svc_connection_create(connection);
692 if (ret)
Johan Hovoldaac08392016-08-26 12:55:49 +0200693 goto err_hd_cport_clear;
Johan Hovoldd7ea30a52015-09-17 13:17:26 +0200694
Johan Hovoldaac08392016-08-26 12:55:49 +0200695 ret = gb_connection_hd_cport_connected(connection);
Johan Hovold00ad6972016-05-27 17:26:31 +0200696 if (ret)
697 goto err_svc_connection_destroy;
698
Johan Hovolda29bac62016-06-27 20:07:09 +0200699 spin_lock_irq(&connection->lock);
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100700 if (connection->handler && rx)
Johan Hovold570dfa72016-01-19 12:51:07 +0100701 connection->state = GB_CONNECTION_STATE_ENABLED;
702 else
703 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
Johan Hovolda29bac62016-06-27 20:07:09 +0200704 spin_unlock_irq(&connection->lock);
Johan Hovoldcad09a82015-07-14 15:43:30 +0200705
Johan Hovold4d0bee12016-01-08 20:13:45 +0100706 ret = gb_connection_control_connected(connection);
707 if (ret)
Johan Hovold3de5acf2016-05-27 17:26:36 +0200708 goto err_control_disconnecting;
Johan Hovold4d0bee12016-01-08 20:13:45 +0100709
Johan Hovold8d7a7122015-09-17 13:17:22 +0200710 return 0;
711
Johan Hovold3de5acf2016-05-27 17:26:36 +0200712err_control_disconnecting:
Johan Hovolda29bac62016-06-27 20:07:09 +0200713 spin_lock_irq(&connection->lock);
Johan Hovold3de5acf2016-05-27 17:26:36 +0200714 connection->state = GB_CONNECTION_STATE_DISCONNECTING;
Johan Hovolda29bac62016-06-27 20:07:09 +0200715 gb_connection_cancel_operations(connection, -ESHUTDOWN);
716 spin_unlock_irq(&connection->lock);
Johan Hovold3ea6a812016-01-08 20:13:46 +0100717
Johan Hovold800d6c82016-05-27 17:26:37 +0200718 /* Transmit queue should already be empty. */
719 gb_connection_hd_cport_flush(connection);
720
Johan Hovoldaac08392016-08-26 12:55:49 +0200721 gb_connection_control_disconnecting(connection);
722 gb_connection_cport_shutdown_phase_1(connection);
723 gb_connection_hd_cport_quiesce(connection);
724 gb_connection_cport_shutdown_phase_2(connection);
Johan Hovold3de5acf2016-05-27 17:26:36 +0200725 gb_connection_control_disconnected(connection);
726 connection->state = GB_CONNECTION_STATE_DISABLED;
Johan Hovold00ad6972016-05-27 17:26:31 +0200727err_svc_connection_destroy:
Johan Hovold3ea6a812016-01-08 20:13:46 +0100728 gb_connection_svc_connection_destroy(connection);
Johan Hovoldaac08392016-08-26 12:55:49 +0200729err_hd_cport_clear:
730 gb_connection_hd_cport_clear(connection);
731
Johan Hovold3ea6a812016-01-08 20:13:46 +0100732 gb_connection_hd_cport_disable(connection);
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100733
734 return ret;
735}
736
737int gb_connection_enable(struct gb_connection *connection)
738{
739 int ret = 0;
740
741 mutex_lock(&connection->mutex);
742
743 if (connection->state == GB_CONNECTION_STATE_ENABLED)
744 goto out_unlock;
745
746 ret = _gb_connection_enable(connection, true);
Alex Elder79c8c642016-06-03 15:55:37 -0500747 if (!ret)
748 trace_gb_connection_enable(connection);
749
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100750out_unlock:
Johan Hovold23268782016-01-19 12:51:06 +0100751 mutex_unlock(&connection->mutex);
Johan Hovold3ea6a812016-01-08 20:13:46 +0100752
753 return ret;
754}
755EXPORT_SYMBOL_GPL(gb_connection_enable);
756
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100757int gb_connection_enable_tx(struct gb_connection *connection)
758{
759 int ret = 0;
760
761 mutex_lock(&connection->mutex);
762
763 if (connection->state == GB_CONNECTION_STATE_ENABLED) {
764 ret = -EINVAL;
765 goto out_unlock;
766 }
767
768 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX)
769 goto out_unlock;
770
771 ret = _gb_connection_enable(connection, false);
Alex Elder79c8c642016-06-03 15:55:37 -0500772 if (!ret)
773 trace_gb_connection_enable(connection);
774
Johan Hovoldf7ee0812016-01-21 17:34:21 +0100775out_unlock:
776 mutex_unlock(&connection->mutex);
777
778 return ret;
779}
780EXPORT_SYMBOL_GPL(gb_connection_enable_tx);
781
Johan Hovoldbeb6b7f2016-01-19 12:51:08 +0100782void gb_connection_disable_rx(struct gb_connection *connection)
783{
784 mutex_lock(&connection->mutex);
785
Johan Hovolda29bac62016-06-27 20:07:09 +0200786 spin_lock_irq(&connection->lock);
Johan Hovoldbeb6b7f2016-01-19 12:51:08 +0100787 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
Johan Hovolda29bac62016-06-27 20:07:09 +0200788 spin_unlock_irq(&connection->lock);
Johan Hovoldbeb6b7f2016-01-19 12:51:08 +0100789 goto out_unlock;
790 }
791 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
Johan Hovolda29bac62016-06-27 20:07:09 +0200792 gb_connection_flush_incoming_operations(connection, -ESHUTDOWN);
793 spin_unlock_irq(&connection->lock);
Johan Hovoldbeb6b7f2016-01-19 12:51:08 +0100794
Alex Elder79c8c642016-06-03 15:55:37 -0500795 trace_gb_connection_disable(connection);
796
Johan Hovoldbeb6b7f2016-01-19 12:51:08 +0100797out_unlock:
798 mutex_unlock(&connection->mutex);
799}
Vaibhav Hiremath6d58e712016-05-05 14:32:29 +0530800EXPORT_SYMBOL_GPL(gb_connection_disable_rx);
Johan Hovoldbeb6b7f2016-01-19 12:51:08 +0100801
Johan Hovold55742d22016-05-27 17:26:40 +0200802void gb_connection_mode_switch_prepare(struct gb_connection *connection)
803{
804 connection->mode_switch = true;
805}
806
807void gb_connection_mode_switch_complete(struct gb_connection *connection)
808{
809 gb_connection_svc_connection_destroy(connection);
Johan Hovoldaac08392016-08-26 12:55:49 +0200810 gb_connection_hd_cport_clear(connection);
811
Johan Hovold55742d22016-05-27 17:26:40 +0200812 gb_connection_hd_cport_disable(connection);
Johan Hovoldaac08392016-08-26 12:55:49 +0200813
Johan Hovold55742d22016-05-27 17:26:40 +0200814 connection->mode_switch = false;
815}
816
Johan Hovold3ea6a812016-01-08 20:13:46 +0100817void gb_connection_disable(struct gb_connection *connection)
818{
Johan Hovold23268782016-01-19 12:51:06 +0100819 mutex_lock(&connection->mutex);
820
Johan Hovold81fba242016-01-19 12:51:03 +0100821 if (connection->state == GB_CONNECTION_STATE_DISABLED)
Johan Hovold23268782016-01-19 12:51:06 +0100822 goto out_unlock;
Johan Hovold81fba242016-01-19 12:51:03 +0100823
Viresh Kumar0698be02016-06-15 08:25:56 +0530824 trace_gb_connection_disable(connection);
825
Johan Hovolda29bac62016-06-27 20:07:09 +0200826 spin_lock_irq(&connection->lock);
Johan Hovold3de5acf2016-05-27 17:26:36 +0200827 connection->state = GB_CONNECTION_STATE_DISCONNECTING;
Johan Hovolda29bac62016-06-27 20:07:09 +0200828 gb_connection_cancel_operations(connection, -ESHUTDOWN);
829 spin_unlock_irq(&connection->lock);
Johan Hovold81fba242016-01-19 12:51:03 +0100830
Johan Hovold800d6c82016-05-27 17:26:37 +0200831 gb_connection_hd_cport_flush(connection);
832
Johan Hovoldaac08392016-08-26 12:55:49 +0200833 gb_connection_control_disconnecting(connection);
834 gb_connection_cport_shutdown_phase_1(connection);
835 gb_connection_hd_cport_quiesce(connection);
836 gb_connection_cport_shutdown_phase_2(connection);
Johan Hovold3de5acf2016-05-27 17:26:36 +0200837 gb_connection_control_disconnected(connection);
838
839 connection->state = GB_CONNECTION_STATE_DISABLED;
840
Johan Hovold55742d22016-05-27 17:26:40 +0200841 /* control-connection tear down is deferred when mode switching */
842 if (!connection->mode_switch) {
843 gb_connection_svc_connection_destroy(connection);
Johan Hovoldaac08392016-08-26 12:55:49 +0200844 gb_connection_hd_cport_clear(connection);
845
Johan Hovold55742d22016-05-27 17:26:40 +0200846 gb_connection_hd_cport_disable(connection);
847 }
Johan Hovold23268782016-01-19 12:51:06 +0100848
849out_unlock:
850 mutex_unlock(&connection->mutex);
Johan Hovold3ea6a812016-01-08 20:13:46 +0100851}
852EXPORT_SYMBOL_GPL(gb_connection_disable);
853
Johan Hovold7aefe792016-05-27 17:26:22 +0200854/* Disable a connection without communicating with the remote end. */
855void gb_connection_disable_forced(struct gb_connection *connection)
856{
857 mutex_lock(&connection->mutex);
858
859 if (connection->state == GB_CONNECTION_STATE_DISABLED)
860 goto out_unlock;
861
Viresh Kumar0698be02016-06-15 08:25:56 +0530862 trace_gb_connection_disable(connection);
863
Johan Hovolda29bac62016-06-27 20:07:09 +0200864 spin_lock_irq(&connection->lock);
Johan Hovold7aefe792016-05-27 17:26:22 +0200865 connection->state = GB_CONNECTION_STATE_DISABLED;
Johan Hovolda29bac62016-06-27 20:07:09 +0200866 gb_connection_cancel_operations(connection, -ESHUTDOWN);
867 spin_unlock_irq(&connection->lock);
Johan Hovold7aefe792016-05-27 17:26:22 +0200868
Johan Hovold800d6c82016-05-27 17:26:37 +0200869 gb_connection_hd_cport_flush(connection);
Johan Hovold7aefe792016-05-27 17:26:22 +0200870
Johan Hovoldaac08392016-08-26 12:55:49 +0200871 gb_connection_svc_connection_destroy(connection);
872 gb_connection_hd_cport_clear(connection);
873
874 gb_connection_hd_cport_disable(connection);
Johan Hovold7aefe792016-05-27 17:26:22 +0200875out_unlock:
876 mutex_unlock(&connection->mutex);
877}
878EXPORT_SYMBOL_GPL(gb_connection_disable_forced);
879
Johan Hovoldc3681f62016-01-19 12:51:23 +0100880/* Caller must have disabled the connection before destroying it. */
Viresh Kumarfda23812015-08-31 17:21:13 +0530881void gb_connection_destroy(struct gb_connection *connection)
882{
Johan Hovold2edbf5f2016-01-19 12:51:22 +0100883 if (!connection)
Viresh Kumarfda23812015-08-31 17:21:13 +0530884 return;
885
Alex Elder814ae5312016-05-17 09:13:16 -0500886 if (WARN_ON(connection->state != GB_CONNECTION_STATE_DISABLED))
887 gb_connection_disable(connection);
888
Johan Hovold210b5082016-01-19 12:51:26 +0100889 mutex_lock(&gb_connection_mutex);
890
Johan Hovold0b1d2622016-06-27 20:07:10 +0200891 spin_lock_irq(&gb_connections_lock);
Viresh Kumarfda23812015-08-31 17:21:13 +0530892 list_del(&connection->bundle_links);
893 list_del(&connection->hd_links);
Johan Hovold0b1d2622016-06-27 20:07:10 +0200894 spin_unlock_irq(&gb_connections_lock);
Viresh Kumarfda23812015-08-31 17:21:13 +0530895
Johan Hovoldc3681f62016-01-19 12:51:23 +0100896 destroy_workqueue(connection->wq);
897
Johan Hovold74a5d932016-05-11 10:17:59 +0200898 gb_hd_cport_release(connection->hd, connection->hd_cport_id);
Viresh Kumarfda23812015-08-31 17:21:13 +0530899 connection->hd_cport_id = CPORT_ID_BAD;
900
Johan Hovold210b5082016-01-19 12:51:26 +0100901 mutex_unlock(&gb_connection_mutex);
902
Johan Hovold0e46fab2016-01-19 12:51:25 +0100903 gb_connection_put(connection);
Viresh Kumarfda23812015-08-31 17:21:13 +0530904}
Johan Hovold98fdf5a2016-01-21 17:34:09 +0100905EXPORT_SYMBOL_GPL(gb_connection_destroy);
Viresh Kumarfda23812015-08-31 17:21:13 +0530906
Bryan O'Donoghuee7e2efc2015-10-15 16:10:42 +0100907void gb_connection_latency_tag_enable(struct gb_connection *connection)
908{
Johan Hovold25376362015-11-03 18:03:23 +0100909 struct gb_host_device *hd = connection->hd;
Bryan O'Donoghuee7e2efc2015-10-15 16:10:42 +0100910 int ret;
911
912 if (!hd->driver->latency_tag_enable)
913 return;
914
915 ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
916 if (ret) {
Johan Hovold4c4b5022015-11-25 15:59:15 +0100917 dev_err(&connection->hd->dev,
918 "%s: failed to enable latency tag: %d\n",
919 connection->name, ret);
Bryan O'Donoghuee7e2efc2015-10-15 16:10:42 +0100920 }
921}
922EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
923
924void gb_connection_latency_tag_disable(struct gb_connection *connection)
925{
Johan Hovold25376362015-11-03 18:03:23 +0100926 struct gb_host_device *hd = connection->hd;
Bryan O'Donoghuee7e2efc2015-10-15 16:10:42 +0100927 int ret;
928
929 if (!hd->driver->latency_tag_disable)
930 return;
931
932 ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
933 if (ret) {
Johan Hovold4c4b5022015-11-25 15:59:15 +0100934 dev_err(&connection->hd->dev,
935 "%s: failed to disable latency tag: %d\n",
936 connection->name, ret);
Bryan O'Donoghuee7e2efc2015-10-15 16:10:42 +0100937 }
938}
939EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);