Greg Kroah-Hartman | eb50fd3 | 2017-11-07 14:58:41 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 2 | /* |
| 3 | * Greybus connections |
| 4 | * |
| 5 | * Copyright 2014 Google Inc. |
Alex Elder | a46e967 | 2014-12-12 12:08:42 -0600 | [diff] [blame] | 6 | * Copyright 2014 Linaro Ltd. |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 7 | */ |
| 8 | |
Johan Hovold | 5a5bc35 | 2015-07-23 10:50:02 +0200 | [diff] [blame] | 9 | #include <linux/workqueue.h> |
Greg Kroah-Hartman | ec0ad86 | 2019-08-25 07:54:27 +0200 | [diff] [blame] | 10 | #include <linux/greybus.h> |
Johan Hovold | 5a5bc35 | 2015-07-23 10:50:02 +0200 | [diff] [blame] | 11 | |
Alex Elder | 79c8c64 | 2016-06-03 15:55:37 -0500 | [diff] [blame] | 12 | #include "greybus_trace.h" |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 13 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 14 | #define GB_CONNECTION_CPORT_QUIESCE_TIMEOUT 1000 |
| 15 | |
Johan Hovold | 0e46fab | 2016-01-19 12:51:25 +0100 | [diff] [blame] | 16 | static void gb_connection_kref_release(struct kref *kref); |
| 17 | |
Alex Elder | 748e123 | 2014-10-03 14:14:22 -0500 | [diff] [blame] | 18 | static DEFINE_SPINLOCK(gb_connections_lock); |
Johan Hovold | 210b508 | 2016-01-19 12:51:26 +0100 | [diff] [blame] | 19 | static DEFINE_MUTEX(gb_connection_mutex); |
| 20 | |
Johan Hovold | b53e0c9 | 2016-01-19 12:51:27 +0100 | [diff] [blame] | 21 | /* Caller holds gb_connection_mutex. */ |
Viresh Kumar | 5413122 | 2016-06-10 14:59:09 +0530 | [diff] [blame] | 22 | static bool gb_connection_cport_in_use(struct gb_interface *intf, u16 cport_id) |
Alex Elder | f5c2be9 | 2015-06-09 17:42:58 -0500 | [diff] [blame] | 23 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 24 | struct gb_host_device *hd = intf->hd; |
Alex Elder | f5c2be9 | 2015-06-09 17:42:58 -0500 | [diff] [blame] | 25 | struct gb_connection *connection; |
| 26 | |
Johan Hovold | 0daf17b | 2015-11-25 15:59:12 +0100 | [diff] [blame] | 27 | list_for_each_entry(connection, &hd->connections, hd_links) { |
| 28 | if (connection->intf == intf && |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 29 | connection->intf_cport_id == cport_id) |
Viresh Kumar | 5413122 | 2016-06-10 14:59:09 +0530 | [diff] [blame] | 30 | return true; |
Johan Hovold | 0daf17b | 2015-11-25 15:59:12 +0100 | [diff] [blame] | 31 | } |
| 32 | |
Viresh Kumar | 5413122 | 2016-06-10 14:59:09 +0530 | [diff] [blame] | 33 | return false; |
Alex Elder | f5c2be9 | 2015-06-09 17:42:58 -0500 | [diff] [blame] | 34 | } |
| 35 | |
Johan Hovold | 0e46fab | 2016-01-19 12:51:25 +0100 | [diff] [blame] | 36 | static void gb_connection_get(struct gb_connection *connection) |
| 37 | { |
| 38 | kref_get(&connection->kref); |
Alex Elder | 79c8c64 | 2016-06-03 15:55:37 -0500 | [diff] [blame] | 39 | |
| 40 | trace_gb_connection_get(connection); |
Johan Hovold | 0e46fab | 2016-01-19 12:51:25 +0100 | [diff] [blame] | 41 | } |
| 42 | |
| 43 | static void gb_connection_put(struct gb_connection *connection) |
| 44 | { |
Alex Elder | 79c8c64 | 2016-06-03 15:55:37 -0500 | [diff] [blame] | 45 | trace_gb_connection_put(connection); |
| 46 | |
Johan Hovold | 0e46fab | 2016-01-19 12:51:25 +0100 | [diff] [blame] | 47 | kref_put(&connection->kref, gb_connection_kref_release); |
| 48 | } |
| 49 | |
| 50 | /* |
| 51 | * Returns a reference-counted pointer to the connection if found. |
| 52 | */ |
Alex Elder | 8bd0ae6 | 2015-06-08 12:05:11 -0500 | [diff] [blame] | 53 | static struct gb_connection * |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 54 | gb_connection_hd_find(struct gb_host_device *hd, u16 cport_id) |
Alex Elder | ee9ebe4 | 2014-10-06 06:53:08 -0500 | [diff] [blame] | 55 | { |
Alex Elder | f5c2be9 | 2015-06-09 17:42:58 -0500 | [diff] [blame] | 56 | struct gb_connection *connection; |
Johan Hovold | 8f5eadb | 2015-03-02 09:55:26 +0100 | [diff] [blame] | 57 | unsigned long flags; |
Alex Elder | ee9ebe4 | 2014-10-06 06:53:08 -0500 | [diff] [blame] | 58 | |
Johan Hovold | 8f5eadb | 2015-03-02 09:55:26 +0100 | [diff] [blame] | 59 | spin_lock_irqsave(&gb_connections_lock, flags); |
Alex Elder | 2c43ce4 | 2014-11-17 08:08:44 -0600 | [diff] [blame] | 60 | list_for_each_entry(connection, &hd->connections, hd_links) |
Johan Hovold | 0e46fab | 2016-01-19 12:51:25 +0100 | [diff] [blame] | 61 | if (connection->hd_cport_id == cport_id) { |
| 62 | gb_connection_get(connection); |
Marti Bolivar | e86905b | 2014-10-06 13:26:02 -0400 | [diff] [blame] | 63 | goto found; |
Johan Hovold | 0e46fab | 2016-01-19 12:51:25 +0100 | [diff] [blame] | 64 | } |
Marti Bolivar | e86905b | 2014-10-06 13:26:02 -0400 | [diff] [blame] | 65 | connection = NULL; |
Alex Elder | f5c2be9 | 2015-06-09 17:42:58 -0500 | [diff] [blame] | 66 | found: |
Johan Hovold | 8f5eadb | 2015-03-02 09:55:26 +0100 | [diff] [blame] | 67 | spin_unlock_irqrestore(&gb_connections_lock, flags); |
Alex Elder | ee9ebe4 | 2014-10-06 06:53:08 -0500 | [diff] [blame] | 68 | |
| 69 | return connection; |
| 70 | } |
| 71 | |
Alex Elder | de3557d | 2014-11-20 16:09:18 -0600 | [diff] [blame] | 72 | /* |
| 73 | * Callback from the host driver to let us know that data has been |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 74 | * received on the bundle. |
Alex Elder | de3557d | 2014-11-20 16:09:18 -0600 | [diff] [blame] | 75 | */ |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 76 | void greybus_data_rcvd(struct gb_host_device *hd, u16 cport_id, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 77 | u8 *data, size_t length) |
Alex Elder | 374e6a2 | 2014-11-17 18:08:37 -0600 | [diff] [blame] | 78 | { |
| 79 | struct gb_connection *connection; |
| 80 | |
Alex Elder | 495787a | 2016-06-03 15:55:38 -0500 | [diff] [blame] | 81 | trace_gb_hd_in(hd); |
| 82 | |
Viresh Kumar | 12eba9f | 2015-05-20 16:48:00 +0530 | [diff] [blame] | 83 | connection = gb_connection_hd_find(hd, cport_id); |
Alex Elder | 374e6a2 | 2014-11-17 18:08:37 -0600 | [diff] [blame] | 84 | if (!connection) { |
Johan Hovold | 2adaefb | 2015-11-25 15:59:02 +0100 | [diff] [blame] | 85 | dev_err(&hd->dev, |
Alex Elder | 374e6a2 | 2014-11-17 18:08:37 -0600 | [diff] [blame] | 86 | "nonexistent connection (%zu bytes dropped)\n", length); |
| 87 | return; |
| 88 | } |
Alex Elder | 61089e8 | 2014-11-18 13:26:50 -0600 | [diff] [blame] | 89 | gb_connection_recv(connection, data, length); |
Johan Hovold | 0e46fab | 2016-01-19 12:51:25 +0100 | [diff] [blame] | 90 | gb_connection_put(connection); |
Alex Elder | 374e6a2 | 2014-11-17 18:08:37 -0600 | [diff] [blame] | 91 | } |
Alex Elder | de3557d | 2014-11-20 16:09:18 -0600 | [diff] [blame] | 92 | EXPORT_SYMBOL_GPL(greybus_data_rcvd); |
Alex Elder | 374e6a2 | 2014-11-17 18:08:37 -0600 | [diff] [blame] | 93 | |
Greg Kroah-Hartman | b750fa3 | 2015-10-16 16:56:38 -0700 | [diff] [blame] | 94 | static void gb_connection_kref_release(struct kref *kref) |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 95 | { |
Greg Kroah-Hartman | b750fa3 | 2015-10-16 16:56:38 -0700 | [diff] [blame] | 96 | struct gb_connection *connection; |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 97 | |
Greg Kroah-Hartman | b750fa3 | 2015-10-16 16:56:38 -0700 | [diff] [blame] | 98 | connection = container_of(kref, struct gb_connection, kref); |
Johan Hovold | c3681f6 | 2016-01-19 12:51:23 +0100 | [diff] [blame] | 99 | |
Alex Elder | 79c8c64 | 2016-06-03 15:55:37 -0500 | [diff] [blame] | 100 | trace_gb_connection_release(connection); |
| 101 | |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 102 | kfree(connection); |
| 103 | } |
| 104 | |
Johan Hovold | 729b260 | 2015-11-25 15:59:14 +0100 | [diff] [blame] | 105 | static 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 Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 117 | "%u/%u:%u", hd_cport_id, intf_id, cport_id); |
Johan Hovold | 729b260 | 2015-11-25 15:59:14 +0100 | [diff] [blame] | 118 | } |
| 119 | |
Alex Elder | 177404b | 2014-10-03 14:14:24 -0500 | [diff] [blame] | 120 | /* |
Johan Hovold | 96c2af5 | 2016-01-21 17:34:15 +0100 | [diff] [blame] | 121 | * _gb_connection_create() - create a Greybus connection |
Johan Hovold | 2566fae6 | 2015-11-25 15:59:11 +0100 | [diff] [blame] | 122 | * @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 Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 127 | * @handler: request handler (may be NULL) |
Johan Hovold | cb03318 | 2016-03-03 13:34:36 +0100 | [diff] [blame] | 128 | * @flags: connection flags |
Johan Hovold | 2566fae6 | 2015-11-25 15:59:11 +0100 | [diff] [blame] | 129 | * |
| 130 | * Create a Greybus connection, representing the bidirectional link |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 131 | * between a CPort on a (local) Greybus host device and a CPort on |
Johan Hovold | 2566fae6 | 2015-11-25 15:59:11 +0100 | [diff] [blame] | 132 | * another Greybus interface. |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 133 | * |
Alex Elder | e88afa5 | 2014-10-01 21:54:15 -0500 | [diff] [blame] | 134 | * A connection also maintains the state of operations sent over the |
| 135 | * connection. |
| 136 | * |
Johan Hovold | 210b508 | 2016-01-19 12:51:26 +0100 | [diff] [blame] | 137 | * Serialised against concurrent create and destroy using the |
| 138 | * gb_connection_mutex. |
| 139 | * |
Johan Hovold | 24e094d | 2016-01-21 17:34:16 +0100 | [diff] [blame] | 140 | * Return: A pointer to the new connection if successful, or an ERR_PTR |
| 141 | * otherwise. |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 142 | */ |
Johan Hovold | 2566fae6 | 2015-11-25 15:59:11 +0100 | [diff] [blame] | 143 | static struct gb_connection * |
Johan Hovold | 96c2af5 | 2016-01-21 17:34:15 +0100 | [diff] [blame] | 144 | _gb_connection_create(struct gb_host_device *hd, int hd_cport_id, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 145 | struct gb_interface *intf, |
| 146 | struct gb_bundle *bundle, int cport_id, |
| 147 | gb_request_handler_t handler, |
| 148 | unsigned long flags) |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 149 | { |
| 150 | struct gb_connection *connection; |
Johan Hovold | 24e094d | 2016-01-21 17:34:16 +0100 | [diff] [blame] | 151 | int ret; |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 152 | |
Johan Hovold | 210b508 | 2016-01-19 12:51:26 +0100 | [diff] [blame] | 153 | mutex_lock(&gb_connection_mutex); |
| 154 | |
Viresh Kumar | 5413122 | 2016-06-10 14:59:09 +0530 | [diff] [blame] | 155 | if (intf && gb_connection_cport_in_use(intf, cport_id)) { |
Johan Hovold | b53e0c9 | 2016-01-19 12:51:27 +0100 | [diff] [blame] | 156 | dev_err(&intf->dev, "cport %u already in use\n", cport_id); |
Johan Hovold | 24e094d | 2016-01-21 17:34:16 +0100 | [diff] [blame] | 157 | ret = -EBUSY; |
Johan Hovold | b53e0c9 | 2016-01-19 12:51:27 +0100 | [diff] [blame] | 158 | goto err_unlock; |
| 159 | } |
| 160 | |
Johan Hovold | f2aae1c | 2016-05-11 10:18:02 +0200 | [diff] [blame] | 161 | ret = gb_hd_cport_allocate(hd, hd_cport_id, flags); |
Johan Hovold | 74a5d93 | 2016-05-11 10:17:59 +0200 | [diff] [blame] | 162 | if (ret < 0) { |
| 163 | dev_err(&hd->dev, "failed to allocate cport: %d\n", ret); |
Johan Hovold | 210b508 | 2016-01-19 12:51:26 +0100 | [diff] [blame] | 164 | goto err_unlock; |
Johan Hovold | 74a5d93 | 2016-05-11 10:17:59 +0200 | [diff] [blame] | 165 | } |
Johan Hovold | 24e094d | 2016-01-21 17:34:16 +0100 | [diff] [blame] | 166 | hd_cport_id = ret; |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 167 | |
Johan Hovold | 10f9fa1 | 2015-07-23 10:50:01 +0200 | [diff] [blame] | 168 | connection = kzalloc(sizeof(*connection), GFP_KERNEL); |
Johan Hovold | 24e094d | 2016-01-21 17:34:16 +0100 | [diff] [blame] | 169 | if (!connection) { |
| 170 | ret = -ENOMEM; |
Johan Hovold | 74a5d93 | 2016-05-11 10:17:59 +0200 | [diff] [blame] | 171 | goto err_hd_cport_release; |
Johan Hovold | 24e094d | 2016-01-21 17:34:16 +0100 | [diff] [blame] | 172 | } |
Johan Hovold | 10f9fa1 | 2015-07-23 10:50:01 +0200 | [diff] [blame] | 173 | |
Johan Hovold | 7c63a82 | 2015-07-23 10:50:00 +0200 | [diff] [blame] | 174 | connection->hd_cport_id = hd_cport_id; |
Alex Elder | f9b0366f1 | 2015-06-12 10:21:08 -0500 | [diff] [blame] | 175 | connection->intf_cport_id = cport_id; |
| 176 | connection->hd = hd; |
Johan Hovold | 2566fae6 | 2015-11-25 15:59:11 +0100 | [diff] [blame] | 177 | connection->intf = intf; |
Greg Kroah-Hartman | 1db0a5f | 2014-12-12 17:10:17 -0500 | [diff] [blame] | 178 | connection->bundle = bundle; |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 179 | connection->handler = handler; |
Johan Hovold | cb03318 | 2016-03-03 13:34:36 +0100 | [diff] [blame] | 180 | connection->flags = flags; |
Johan Hovold | 0e9b41a | 2016-05-11 10:17:55 +0200 | [diff] [blame] | 181 | if (intf && (intf->quirks & GB_INTERFACE_QUIRK_NO_CPORT_FEATURES)) |
| 182 | connection->flags |= GB_CONNECTION_FLAG_NO_FLOWCTRL; |
Alex Elder | 36561f2 | 2014-10-22 02:04:30 -0500 | [diff] [blame] | 183 | connection->state = GB_CONNECTION_STATE_DISABLED; |
Alex Elder | ad1c449 | 2014-10-02 12:30:06 -0500 | [diff] [blame] | 184 | |
Johan Hovold | 4e2b1e4 | 2015-07-22 17:49:19 +0200 | [diff] [blame] | 185 | atomic_set(&connection->op_cycle, 0); |
Johan Hovold | 2326878 | 2016-01-19 12:51:06 +0100 | [diff] [blame] | 186 | mutex_init(&connection->mutex); |
Johan Hovold | 4e2b1e4 | 2015-07-22 17:49:19 +0200 | [diff] [blame] | 187 | spin_lock_init(&connection->lock); |
| 188 | INIT_LIST_HEAD(&connection->operations); |
| 189 | |
Johan Hovold | 5a5bc35 | 2015-07-23 10:50:02 +0200 | [diff] [blame] | 190 | connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1, |
Johan Hovold | 582b3a1 | 2015-11-25 15:59:03 +0100 | [diff] [blame] | 191 | dev_name(&hd->dev), hd_cport_id); |
Johan Hovold | 24e094d | 2016-01-21 17:34:16 +0100 | [diff] [blame] | 192 | if (!connection->wq) { |
| 193 | ret = -ENOMEM; |
Johan Hovold | 5a5bc35 | 2015-07-23 10:50:02 +0200 | [diff] [blame] | 194 | goto err_free_connection; |
Johan Hovold | 24e094d | 2016-01-21 17:34:16 +0100 | [diff] [blame] | 195 | } |
Johan Hovold | 5a5bc35 | 2015-07-23 10:50:02 +0200 | [diff] [blame] | 196 | |
Greg Kroah-Hartman | b750fa3 | 2015-10-16 16:56:38 -0700 | [diff] [blame] | 197 | kref_init(&connection->kref); |
Greg Kroah-Hartman | f0f61b9 | 2014-10-24 17:34:46 +0800 | [diff] [blame] | 198 | |
Johan Hovold | 729b260 | 2015-11-25 15:59:14 +0100 | [diff] [blame] | 199 | gb_connection_init_name(connection); |
| 200 | |
Johan Hovold | 0b1d262 | 2016-06-27 20:07:10 +0200 | [diff] [blame] | 201 | spin_lock_irq(&gb_connections_lock); |
Viresh Kumar | 928f2ab | 2015-06-04 18:16:45 +0530 | [diff] [blame] | 202 | list_add(&connection->hd_links, &hd->connections); |
Viresh Kumar | 75662e5 | 2015-07-21 17:44:16 +0530 | [diff] [blame] | 203 | |
| 204 | if (bundle) |
| 205 | list_add(&connection->bundle_links, &bundle->connections); |
| 206 | else |
| 207 | INIT_LIST_HEAD(&connection->bundle_links); |
| 208 | |
Johan Hovold | 0b1d262 | 2016-06-27 20:07:10 +0200 | [diff] [blame] | 209 | spin_unlock_irq(&gb_connections_lock); |
Alex Elder | 748e123 | 2014-10-03 14:14:22 -0500 | [diff] [blame] | 210 | |
Johan Hovold | 210b508 | 2016-01-19 12:51:26 +0100 | [diff] [blame] | 211 | mutex_unlock(&gb_connection_mutex); |
| 212 | |
Alex Elder | 79c8c64 | 2016-06-03 15:55:37 -0500 | [diff] [blame] | 213 | trace_gb_connection_create(connection); |
| 214 | |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 215 | return connection; |
Johan Hovold | 10f9fa1 | 2015-07-23 10:50:01 +0200 | [diff] [blame] | 216 | |
Johan Hovold | 5a5bc35 | 2015-07-23 10:50:02 +0200 | [diff] [blame] | 217 | err_free_connection: |
| 218 | kfree(connection); |
Johan Hovold | 74a5d93 | 2016-05-11 10:17:59 +0200 | [diff] [blame] | 219 | err_hd_cport_release: |
| 220 | gb_hd_cport_release(hd, hd_cport_id); |
Johan Hovold | 210b508 | 2016-01-19 12:51:26 +0100 | [diff] [blame] | 221 | err_unlock: |
| 222 | mutex_unlock(&gb_connection_mutex); |
Johan Hovold | 10f9fa1 | 2015-07-23 10:50:01 +0200 | [diff] [blame] | 223 | |
Johan Hovold | 24e094d | 2016-01-21 17:34:16 +0100 | [diff] [blame] | 224 | return ERR_PTR(ret); |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 225 | } |
| 226 | |
Johan Hovold | 2566fae6 | 2015-11-25 15:59:11 +0100 | [diff] [blame] | 227 | struct gb_connection * |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 228 | gb_connection_create_static(struct gb_host_device *hd, u16 hd_cport_id, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 229 | gb_request_handler_t handler) |
Johan Hovold | 2566fae6 | 2015-11-25 15:59:11 +0100 | [diff] [blame] | 230 | { |
Johan Hovold | cb03318 | 2016-03-03 13:34:36 +0100 | [diff] [blame] | 231 | return _gb_connection_create(hd, hd_cport_id, NULL, NULL, 0, handler, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 232 | GB_CONNECTION_FLAG_HIGH_PRIO); |
Johan Hovold | 2566fae6 | 2015-11-25 15:59:11 +0100 | [diff] [blame] | 233 | } |
| 234 | |
| 235 | struct gb_connection * |
Johan Hovold | 59507e2 | 2016-01-21 17:34:12 +0100 | [diff] [blame] | 236 | gb_connection_create_control(struct gb_interface *intf) |
| 237 | { |
Johan Hovold | aca7aab | 2016-05-27 17:26:25 +0200 | [diff] [blame] | 238 | return _gb_connection_create(intf->hd, -1, intf, NULL, 0, NULL, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 239 | GB_CONNECTION_FLAG_CONTROL | |
| 240 | GB_CONNECTION_FLAG_HIGH_PRIO); |
Johan Hovold | 59507e2 | 2016-01-21 17:34:12 +0100 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | struct gb_connection * |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 244 | gb_connection_create(struct gb_bundle *bundle, u16 cport_id, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 245 | gb_request_handler_t handler) |
Johan Hovold | 2566fae6 | 2015-11-25 15:59:11 +0100 | [diff] [blame] | 246 | { |
Johan Hovold | 8cff6c6 | 2016-01-21 17:34:14 +0100 | [diff] [blame] | 247 | struct gb_interface *intf = bundle->intf; |
| 248 | |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 249 | return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 250 | handler, 0); |
Johan Hovold | 2566fae6 | 2015-11-25 15:59:11 +0100 | [diff] [blame] | 251 | } |
Johan Hovold | 96c2af5 | 2016-01-21 17:34:15 +0100 | [diff] [blame] | 252 | EXPORT_SYMBOL_GPL(gb_connection_create); |
Johan Hovold | 2566fae6 | 2015-11-25 15:59:11 +0100 | [diff] [blame] | 253 | |
Johan Hovold | cb03318 | 2016-03-03 13:34:36 +0100 | [diff] [blame] | 254 | struct gb_connection * |
| 255 | gb_connection_create_flags(struct gb_bundle *bundle, u16 cport_id, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 256 | gb_request_handler_t handler, |
| 257 | unsigned long flags) |
Johan Hovold | cb03318 | 2016-03-03 13:34:36 +0100 | [diff] [blame] | 258 | { |
| 259 | struct gb_interface *intf = bundle->intf; |
| 260 | |
Johan Hovold | 1ba30c3 | 2016-06-22 11:42:03 +0200 | [diff] [blame] | 261 | if (WARN_ON_ONCE(flags & GB_CONNECTION_FLAG_CORE_MASK)) |
| 262 | flags &= ~GB_CONNECTION_FLAG_CORE_MASK; |
| 263 | |
Johan Hovold | cb03318 | 2016-03-03 13:34:36 +0100 | [diff] [blame] | 264 | return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 265 | handler, flags); |
Johan Hovold | cb03318 | 2016-03-03 13:34:36 +0100 | [diff] [blame] | 266 | } |
| 267 | EXPORT_SYMBOL_GPL(gb_connection_create_flags); |
| 268 | |
Johan Hovold | 781ac866 | 2016-05-11 10:17:57 +0200 | [diff] [blame] | 269 | struct gb_connection * |
| 270 | gb_connection_create_offloaded(struct gb_bundle *bundle, u16 cport_id, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 271 | unsigned long flags) |
Johan Hovold | 781ac866 | 2016-05-11 10:17:57 +0200 | [diff] [blame] | 272 | { |
Johan Hovold | 781ac866 | 2016-05-11 10:17:57 +0200 | [diff] [blame] | 273 | flags |= GB_CONNECTION_FLAG_OFFLOADED; |
| 274 | |
Johan Hovold | 1ba30c3 | 2016-06-22 11:42:03 +0200 | [diff] [blame] | 275 | return gb_connection_create_flags(bundle, cport_id, NULL, flags); |
Johan Hovold | 781ac866 | 2016-05-11 10:17:57 +0200 | [diff] [blame] | 276 | } |
| 277 | EXPORT_SYMBOL_GPL(gb_connection_create_offloaded); |
| 278 | |
Johan Hovold | d7ea30a5 | 2015-09-17 13:17:26 +0200 | [diff] [blame] | 279 | static int gb_connection_hd_cport_enable(struct gb_connection *connection) |
| 280 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 281 | struct gb_host_device *hd = connection->hd; |
Johan Hovold | d7ea30a5 | 2015-09-17 13:17:26 +0200 | [diff] [blame] | 282 | int ret; |
| 283 | |
| 284 | if (!hd->driver->cport_enable) |
| 285 | return 0; |
| 286 | |
Johan Hovold | 6910fa2 | 2016-06-22 11:42:02 +0200 | [diff] [blame] | 287 | ret = hd->driver->cport_enable(hd, connection->hd_cport_id, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 288 | connection->flags); |
Johan Hovold | d7ea30a5 | 2015-09-17 13:17:26 +0200 | [diff] [blame] | 289 | if (ret) { |
Viresh Kumar | 62e0462 | 2016-06-09 16:34:42 +0530 | [diff] [blame] | 290 | dev_err(&hd->dev, "%s: failed to enable host cport: %d\n", |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 291 | connection->name, ret); |
Johan Hovold | d7ea30a5 | 2015-09-17 13:17:26 +0200 | [diff] [blame] | 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | |
| 298 | static void gb_connection_hd_cport_disable(struct gb_connection *connection) |
| 299 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 300 | struct gb_host_device *hd = connection->hd; |
Johan Hovold | 3cbe52c | 2016-05-27 17:26:28 +0200 | [diff] [blame] | 301 | int ret; |
Johan Hovold | d7ea30a5 | 2015-09-17 13:17:26 +0200 | [diff] [blame] | 302 | |
| 303 | if (!hd->driver->cport_disable) |
| 304 | return; |
| 305 | |
Johan Hovold | 3cbe52c | 2016-05-27 17:26:28 +0200 | [diff] [blame] | 306 | ret = hd->driver->cport_disable(hd, connection->hd_cport_id); |
| 307 | if (ret) { |
Viresh Kumar | 62e0462 | 2016-06-09 16:34:42 +0530 | [diff] [blame] | 308 | dev_err(&hd->dev, "%s: failed to disable host cport: %d\n", |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 309 | connection->name, ret); |
Johan Hovold | 3cbe52c | 2016-05-27 17:26:28 +0200 | [diff] [blame] | 310 | } |
Johan Hovold | d7ea30a5 | 2015-09-17 13:17:26 +0200 | [diff] [blame] | 311 | } |
| 312 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 313 | static 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 Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 324 | connection->name, ret); |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 325 | return ret; |
| 326 | } |
| 327 | |
| 328 | return 0; |
| 329 | } |
| 330 | |
Johan Hovold | 800d6c8 | 2016-05-27 17:26:37 +0200 | [diff] [blame] | 331 | static 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 Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 342 | connection->name, ret); |
Johan Hovold | 800d6c8 | 2016-05-27 17:26:37 +0200 | [diff] [blame] | 343 | return ret; |
| 344 | } |
| 345 | |
| 346 | return 0; |
| 347 | } |
| 348 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 349 | static int gb_connection_hd_cport_quiesce(struct gb_connection *connection) |
Fabien Parent | 9ed5e1b | 2016-02-23 18:46:08 +0100 | [diff] [blame] | 350 | { |
| 351 | struct gb_host_device *hd = connection->hd; |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 352 | size_t peer_space; |
Fabien Parent | 9ed5e1b | 2016-02-23 18:46:08 +0100 | [diff] [blame] | 353 | int ret; |
| 354 | |
Jason Hrycay | f05a88a | 2016-12-20 14:49:27 -0600 | [diff] [blame] | 355 | if (!hd->driver->cport_quiesce) |
| 356 | return 0; |
| 357 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 358 | peer_space = sizeof(struct gb_operation_msg_hdr) + |
| 359 | sizeof(struct gb_cport_shutdown_request); |
Fabien Parent | 9ed5e1b | 2016-02-23 18:46:08 +0100 | [diff] [blame] | 360 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 361 | 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 Parent | 9ed5e1b | 2016-02-23 18:46:08 +0100 | [diff] [blame] | 367 | if (ret) { |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 368 | dev_err(&hd->dev, "%s: failed to quiesce host cport: %d\n", |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 369 | connection->name, ret); |
Fabien Parent | 9ed5e1b | 2016-02-23 18:46:08 +0100 | [diff] [blame] | 370 | return ret; |
| 371 | } |
| 372 | |
| 373 | return 0; |
| 374 | } |
| 375 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 376 | static int gb_connection_hd_cport_clear(struct gb_connection *connection) |
Fabien Parent | 9ed5e1b | 2016-02-23 18:46:08 +0100 | [diff] [blame] | 377 | { |
| 378 | struct gb_host_device *hd = connection->hd; |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 379 | int ret; |
Fabien Parent | 9ed5e1b | 2016-02-23 18:46:08 +0100 | [diff] [blame] | 380 | |
Jason Hrycay | f05a88a | 2016-12-20 14:49:27 -0600 | [diff] [blame] | 381 | if (!hd->driver->cport_clear) |
| 382 | return 0; |
| 383 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 384 | 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 Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 387 | connection->name, ret); |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 388 | return ret; |
| 389 | } |
Fabien Parent | 9ed5e1b | 2016-02-23 18:46:08 +0100 | [diff] [blame] | 390 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 391 | return 0; |
Fabien Parent | 9ed5e1b | 2016-02-23 18:46:08 +0100 | [diff] [blame] | 392 | } |
| 393 | |
Alex Elder | c68adb2 | 2014-10-01 21:54:14 -0500 | [diff] [blame] | 394 | /* |
Johan Hovold | a95c258 | 2015-09-17 13:17:21 +0200 | [diff] [blame] | 395 | * Request the SVC to create a connection from AP's cport to interface's |
| 396 | * cport. |
| 397 | */ |
| 398 | static int |
| 399 | gb_connection_svc_connection_create(struct gb_connection *connection) |
| 400 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 401 | struct gb_host_device *hd = connection->hd; |
Viresh Kumar | 1575ef1 | 2015-10-07 15:40:24 -0400 | [diff] [blame] | 402 | struct gb_interface *intf; |
Johan Hovold | 27f25c1 | 2016-03-03 13:34:38 +0100 | [diff] [blame] | 403 | u8 cport_flags; |
Johan Hovold | a95c258 | 2015-09-17 13:17:21 +0200 | [diff] [blame] | 404 | int ret; |
| 405 | |
Johan Hovold | 4ec1574 | 2015-11-25 15:59:13 +0100 | [diff] [blame] | 406 | if (gb_connection_is_static(connection)) |
Johan Hovold | 00ad697 | 2016-05-27 17:26:31 +0200 | [diff] [blame] | 407 | return 0; |
Johan Hovold | a95c258 | 2015-09-17 13:17:21 +0200 | [diff] [blame] | 408 | |
Johan Hovold | 35822c0 | 2015-11-25 15:59:22 +0100 | [diff] [blame] | 409 | intf = connection->intf; |
Johan Hovold | 27f25c1 | 2016-03-03 13:34:38 +0100 | [diff] [blame] | 410 | |
Johan Hovold | ec199cc | 2016-03-29 18:56:03 -0400 | [diff] [blame] | 411 | /* |
Johan Hovold | 0e9b41a | 2016-05-11 10:17:55 +0200 | [diff] [blame] | 412 | * Enable either E2EFC or CSD, unless no flow control is requested. |
Johan Hovold | ec199cc | 2016-03-29 18:56:03 -0400 | [diff] [blame] | 413 | */ |
Johan Hovold | 27f25c1 | 2016-03-03 13:34:38 +0100 | [diff] [blame] | 414 | cport_flags = GB_SVC_CPORT_FLAG_CSV_N; |
Johan Hovold | 0e9b41a | 2016-05-11 10:17:55 +0200 | [diff] [blame] | 415 | if (gb_connection_flow_control_disabled(connection)) { |
Johan Hovold | 27f25c1 | 2016-03-03 13:34:38 +0100 | [diff] [blame] | 416 | cport_flags |= GB_SVC_CPORT_FLAG_CSD_N; |
Johan Hovold | 64a6d13 | 2016-03-03 13:34:39 +0100 | [diff] [blame] | 417 | } else if (gb_connection_e2efc_enabled(connection)) { |
Johan Hovold | 27f25c1 | 2016-03-03 13:34:38 +0100 | [diff] [blame] | 418 | cport_flags |= GB_SVC_CPORT_FLAG_CSD_N | |
| 419 | GB_SVC_CPORT_FLAG_E2EFC; |
| 420 | } |
| 421 | |
Johan Hovold | a95c258 | 2015-09-17 13:17:21 +0200 | [diff] [blame] | 422 | ret = gb_svc_connection_create(hd->svc, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 423 | hd->svc->ap_intf_id, |
| 424 | connection->hd_cport_id, |
| 425 | intf->interface_id, |
| 426 | connection->intf_cport_id, |
| 427 | cport_flags); |
Johan Hovold | a95c258 | 2015-09-17 13:17:21 +0200 | [diff] [blame] | 428 | if (ret) { |
Johan Hovold | 4c4b502 | 2015-11-25 15:59:15 +0100 | [diff] [blame] | 429 | dev_err(&connection->hd->dev, |
| 430 | "%s: failed to create svc connection: %d\n", |
| 431 | connection->name, ret); |
Johan Hovold | a95c258 | 2015-09-17 13:17:21 +0200 | [diff] [blame] | 432 | return ret; |
| 433 | } |
| 434 | |
Johan Hovold | a95c258 | 2015-09-17 13:17:21 +0200 | [diff] [blame] | 435 | return 0; |
| 436 | } |
| 437 | |
Viresh Kumar | 1b7a9cd | 2015-09-07 16:01:22 +0530 | [diff] [blame] | 438 | static void |
| 439 | gb_connection_svc_connection_destroy(struct gb_connection *connection) |
| 440 | { |
Johan Hovold | 4ec1574 | 2015-11-25 15:59:13 +0100 | [diff] [blame] | 441 | if (gb_connection_is_static(connection)) |
Viresh Kumar | 1b7a9cd | 2015-09-07 16:01:22 +0530 | [diff] [blame] | 442 | return; |
| 443 | |
| 444 | gb_svc_connection_destroy(connection->hd->svc, |
Johan Hovold | 66069fb | 2015-11-25 15:59:09 +0100 | [diff] [blame] | 445 | connection->hd->svc->ap_intf_id, |
Viresh Kumar | 1b7a9cd | 2015-09-07 16:01:22 +0530 | [diff] [blame] | 446 | connection->hd_cport_id, |
Johan Hovold | 35822c0 | 2015-11-25 15:59:22 +0100 | [diff] [blame] | 447 | connection->intf->interface_id, |
Viresh Kumar | 1b7a9cd | 2015-09-07 16:01:22 +0530 | [diff] [blame] | 448 | connection->intf_cport_id); |
| 449 | } |
| 450 | |
Johan Hovold | 9d7fc25 | 2015-09-17 13:17:24 +0200 | [diff] [blame] | 451 | /* Inform Interface about active CPorts */ |
| 452 | static int gb_connection_control_connected(struct gb_connection *connection) |
| 453 | { |
Johan Hovold | 9d7fc25 | 2015-09-17 13:17:24 +0200 | [diff] [blame] | 454 | struct gb_control *control; |
| 455 | u16 cport_id = connection->intf_cport_id; |
| 456 | int ret; |
| 457 | |
Johan Hovold | bc3be17 | 2016-01-19 12:51:12 +0100 | [diff] [blame] | 458 | if (gb_connection_is_static(connection)) |
Johan Hovold | 9d7fc25 | 2015-09-17 13:17:24 +0200 | [diff] [blame] | 459 | return 0; |
| 460 | |
Johan Hovold | aca7aab | 2016-05-27 17:26:25 +0200 | [diff] [blame] | 461 | if (gb_connection_is_control(connection)) |
Johan Hovold | bc3be17 | 2016-01-19 12:51:12 +0100 | [diff] [blame] | 462 | return 0; |
Johan Hovold | 9d7fc25 | 2015-09-17 13:17:24 +0200 | [diff] [blame] | 463 | |
Johan Hovold | aca7aab | 2016-05-27 17:26:25 +0200 | [diff] [blame] | 464 | control = connection->intf->control; |
| 465 | |
Johan Hovold | 9d7fc25 | 2015-09-17 13:17:24 +0200 | [diff] [blame] | 466 | ret = gb_control_connected_operation(control, cport_id); |
| 467 | if (ret) { |
Greg Kroah-Hartman | 30482c1 | 2015-10-16 16:56:23 -0700 | [diff] [blame] | 468 | dev_err(&connection->bundle->dev, |
| 469 | "failed to connect cport: %d\n", ret); |
Johan Hovold | 9d7fc25 | 2015-09-17 13:17:24 +0200 | [diff] [blame] | 470 | return ret; |
| 471 | } |
| 472 | |
| 473 | return 0; |
| 474 | } |
| 475 | |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 476 | static void |
| 477 | gb_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 Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 491 | "%s: failed to send disconnecting: %d\n", |
| 492 | connection->name, ret); |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 493 | } |
| 494 | } |
| 495 | |
Johan Hovold | 72d7482 | 2015-09-17 13:17:23 +0200 | [diff] [blame] | 496 | static void |
| 497 | gb_connection_control_disconnected(struct gb_connection *connection) |
Viresh Kumar | 1869065 | 2015-08-11 07:35:56 +0530 | [diff] [blame] | 498 | { |
| 499 | struct gb_control *control; |
Johan Hovold | 72d7482 | 2015-09-17 13:17:23 +0200 | [diff] [blame] | 500 | u16 cport_id = connection->intf_cport_id; |
Viresh Kumar | 1869065 | 2015-08-11 07:35:56 +0530 | [diff] [blame] | 501 | int ret; |
| 502 | |
Johan Hovold | bc3be17 | 2016-01-19 12:51:12 +0100 | [diff] [blame] | 503 | if (gb_connection_is_static(connection)) |
Viresh Kumar | 1869065 | 2015-08-11 07:35:56 +0530 | [diff] [blame] | 504 | return; |
| 505 | |
Johan Hovold | aca7aab | 2016-05-27 17:26:25 +0200 | [diff] [blame] | 506 | control = connection->intf->control; |
| 507 | |
Johan Hovold | 55742d2 | 2016-05-27 17:26:40 +0200 | [diff] [blame] | 508 | 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 Kumar | 1869065 | 2015-08-11 07:35:56 +0530 | [diff] [blame] | 523 | ret = gb_control_disconnected_operation(control, cport_id); |
Johan Hovold | 72d7482 | 2015-09-17 13:17:23 +0200 | [diff] [blame] | 524 | if (ret) { |
Greg Kroah-Hartman | 30482c1 | 2015-10-16 16:56:23 -0700 | [diff] [blame] | 525 | dev_warn(&connection->bundle->dev, |
| 526 | "failed to disconnect cport: %d\n", ret); |
Johan Hovold | 72d7482 | 2015-09-17 13:17:23 +0200 | [diff] [blame] | 527 | } |
Viresh Kumar | 1869065 | 2015-08-11 07:35:56 +0530 | [diff] [blame] | 528 | } |
| 529 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 530 | static int gb_connection_shutdown_operation(struct gb_connection *connection, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 531 | u8 phase) |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 532 | { |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 533 | struct gb_cport_shutdown_request *req; |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 534 | struct gb_operation *operation; |
| 535 | int ret; |
| 536 | |
| 537 | operation = gb_operation_create_core(connection, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 538 | GB_REQUEST_TYPE_CPORT_SHUTDOWN, |
| 539 | sizeof(*req), 0, 0, |
| 540 | GFP_KERNEL); |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 541 | if (!operation) |
| 542 | return -ENOMEM; |
| 543 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 544 | req = operation->request->payload; |
| 545 | req->phase = phase; |
| 546 | |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 547 | ret = gb_operation_request_send_sync(operation); |
| 548 | |
| 549 | gb_operation_put(operation); |
| 550 | |
| 551 | return ret; |
| 552 | } |
| 553 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 554 | static int gb_connection_cport_shutdown(struct gb_connection *connection, |
| 555 | u8 phase) |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 556 | { |
| 557 | struct gb_host_device *hd = connection->hd; |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 558 | const struct gb_hd_driver *drv = hd->driver; |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 559 | int ret; |
| 560 | |
| 561 | if (gb_connection_is_static(connection)) |
| 562 | return 0; |
| 563 | |
| 564 | if (gb_connection_is_offloaded(connection)) { |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 565 | if (!drv->cport_shutdown) |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 566 | return 0; |
| 567 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 568 | ret = drv->cport_shutdown(hd, connection->hd_cport_id, phase, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 569 | GB_OPERATION_TIMEOUT_DEFAULT); |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 570 | } else { |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 571 | ret = gb_connection_shutdown_operation(connection, phase); |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 572 | } |
| 573 | |
| 574 | if (ret) { |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 575 | dev_err(&hd->dev, "%s: failed to send cport shutdown (phase %d): %d\n", |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 576 | connection->name, phase, ret); |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 577 | return ret; |
| 578 | } |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 583 | static int |
| 584 | gb_connection_cport_shutdown_phase_1(struct gb_connection *connection) |
| 585 | { |
| 586 | return gb_connection_cport_shutdown(connection, 1); |
| 587 | } |
| 588 | |
| 589 | static int |
| 590 | gb_connection_cport_shutdown_phase_2(struct gb_connection *connection) |
| 591 | { |
| 592 | return gb_connection_cport_shutdown(connection, 2); |
| 593 | } |
| 594 | |
Johan Hovold | 2846d3e | 2015-09-17 13:17:25 +0200 | [diff] [blame] | 595 | /* |
Johan Hovold | 520c6ea | 2016-01-19 12:51:04 +0100 | [diff] [blame] | 596 | * Cancel all active operations on a connection. |
| 597 | * |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 598 | * Locking: Called with connection lock held and state set to DISABLED or |
| 599 | * DISCONNECTING. |
Johan Hovold | 520c6ea | 2016-01-19 12:51:04 +0100 | [diff] [blame] | 600 | */ |
| 601 | static void gb_connection_cancel_operations(struct gb_connection *connection, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 602 | int errno) |
Viresh Kumar | 127c1fb | 2016-01-28 15:50:48 +0530 | [diff] [blame] | 603 | __must_hold(&connection->lock) |
Johan Hovold | 520c6ea | 2016-01-19 12:51:04 +0100 | [diff] [blame] | 604 | { |
| 605 | struct gb_operation *operation; |
| 606 | |
| 607 | while (!list_empty(&connection->operations)) { |
| 608 | operation = list_last_entry(&connection->operations, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 609 | struct gb_operation, links); |
Johan Hovold | 520c6ea | 2016-01-19 12:51:04 +0100 | [diff] [blame] | 610 | gb_operation_get(operation); |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 611 | spin_unlock_irq(&connection->lock); |
Johan Hovold | 520c6ea | 2016-01-19 12:51:04 +0100 | [diff] [blame] | 612 | |
| 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 Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 620 | spin_lock_irq(&connection->lock); |
Johan Hovold | 520c6ea | 2016-01-19 12:51:04 +0100 | [diff] [blame] | 621 | } |
| 622 | } |
| 623 | |
Johan Hovold | beb6b7f | 2016-01-19 12:51:08 +0100 | [diff] [blame] | 624 | /* |
| 625 | * Cancel all active incoming operations on a connection. |
| 626 | * |
| 627 | * Locking: Called with connection lock held and state set to ENABLED_TX. |
| 628 | */ |
| 629 | static void |
| 630 | gb_connection_flush_incoming_operations(struct gb_connection *connection, |
Cristian Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 631 | int errno) |
Viresh Kumar | 127c1fb | 2016-01-28 15:50:48 +0530 | [diff] [blame] | 632 | __must_hold(&connection->lock) |
Johan Hovold | beb6b7f | 2016-01-19 12:51:08 +0100 | [diff] [blame] | 633 | { |
| 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 Sicilia | 8478c35 | 2018-11-25 17:58:15 +0100 | [diff] [blame] | 640 | links) { |
Johan Hovold | beb6b7f | 2016-01-19 12:51:08 +0100 | [diff] [blame] | 641 | 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 Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 651 | spin_unlock_irq(&connection->lock); |
Johan Hovold | beb6b7f | 2016-01-19 12:51:08 +0100 | [diff] [blame] | 652 | |
| 653 | /* FIXME: flush, not cancel? */ |
| 654 | gb_operation_cancel_incoming(operation, errno); |
| 655 | gb_operation_put(operation); |
| 656 | |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 657 | spin_lock_irq(&connection->lock); |
Johan Hovold | beb6b7f | 2016-01-19 12:51:08 +0100 | [diff] [blame] | 658 | } |
| 659 | } |
| 660 | |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 661 | /* |
| 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 | */ |
| 671 | static int _gb_connection_enable(struct gb_connection *connection, bool rx) |
Alex Elder | 574341c | 2014-10-16 06:35:35 -0500 | [diff] [blame] | 672 | { |
Alex Elder | 36561f2 | 2014-10-22 02:04:30 -0500 | [diff] [blame] | 673 | int ret; |
| 674 | |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 675 | /* Handle ENABLED_TX -> ENABLED transitions. */ |
Johan Hovold | 570dfa7 | 2016-01-19 12:51:07 +0100 | [diff] [blame] | 676 | if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) { |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 677 | if (!(connection->handler && rx)) |
| 678 | return 0; |
Johan Hovold | 570dfa7 | 2016-01-19 12:51:07 +0100 | [diff] [blame] | 679 | |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 680 | spin_lock_irq(&connection->lock); |
Johan Hovold | 570dfa7 | 2016-01-19 12:51:07 +0100 | [diff] [blame] | 681 | connection->state = GB_CONNECTION_STATE_ENABLED; |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 682 | spin_unlock_irq(&connection->lock); |
Johan Hovold | 570dfa7 | 2016-01-19 12:51:07 +0100 | [diff] [blame] | 683 | |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 684 | return 0; |
Johan Hovold | 570dfa7 | 2016-01-19 12:51:07 +0100 | [diff] [blame] | 685 | } |
| 686 | |
Johan Hovold | d7ea30a5 | 2015-09-17 13:17:26 +0200 | [diff] [blame] | 687 | ret = gb_connection_hd_cport_enable(connection); |
Johan Hovold | a95c258 | 2015-09-17 13:17:21 +0200 | [diff] [blame] | 688 | if (ret) |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 689 | return ret; |
Viresh Kumar | a1163fa | 2015-09-07 16:01:21 +0530 | [diff] [blame] | 690 | |
Johan Hovold | d7ea30a5 | 2015-09-17 13:17:26 +0200 | [diff] [blame] | 691 | ret = gb_connection_svc_connection_create(connection); |
| 692 | if (ret) |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 693 | goto err_hd_cport_clear; |
Johan Hovold | d7ea30a5 | 2015-09-17 13:17:26 +0200 | [diff] [blame] | 694 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 695 | ret = gb_connection_hd_cport_connected(connection); |
Johan Hovold | 00ad697 | 2016-05-27 17:26:31 +0200 | [diff] [blame] | 696 | if (ret) |
| 697 | goto err_svc_connection_destroy; |
| 698 | |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 699 | spin_lock_irq(&connection->lock); |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 700 | if (connection->handler && rx) |
Johan Hovold | 570dfa7 | 2016-01-19 12:51:07 +0100 | [diff] [blame] | 701 | connection->state = GB_CONNECTION_STATE_ENABLED; |
| 702 | else |
| 703 | connection->state = GB_CONNECTION_STATE_ENABLED_TX; |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 704 | spin_unlock_irq(&connection->lock); |
Johan Hovold | cad09a8 | 2015-07-14 15:43:30 +0200 | [diff] [blame] | 705 | |
Johan Hovold | 4d0bee1 | 2016-01-08 20:13:45 +0100 | [diff] [blame] | 706 | ret = gb_connection_control_connected(connection); |
| 707 | if (ret) |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 708 | goto err_control_disconnecting; |
Johan Hovold | 4d0bee1 | 2016-01-08 20:13:45 +0100 | [diff] [blame] | 709 | |
Johan Hovold | 8d7a712 | 2015-09-17 13:17:22 +0200 | [diff] [blame] | 710 | return 0; |
| 711 | |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 712 | err_control_disconnecting: |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 713 | spin_lock_irq(&connection->lock); |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 714 | connection->state = GB_CONNECTION_STATE_DISCONNECTING; |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 715 | gb_connection_cancel_operations(connection, -ESHUTDOWN); |
| 716 | spin_unlock_irq(&connection->lock); |
Johan Hovold | 3ea6a81 | 2016-01-08 20:13:46 +0100 | [diff] [blame] | 717 | |
Johan Hovold | 800d6c8 | 2016-05-27 17:26:37 +0200 | [diff] [blame] | 718 | /* Transmit queue should already be empty. */ |
| 719 | gb_connection_hd_cport_flush(connection); |
| 720 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 721 | 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 Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 725 | gb_connection_control_disconnected(connection); |
| 726 | connection->state = GB_CONNECTION_STATE_DISABLED; |
Johan Hovold | 00ad697 | 2016-05-27 17:26:31 +0200 | [diff] [blame] | 727 | err_svc_connection_destroy: |
Johan Hovold | 3ea6a81 | 2016-01-08 20:13:46 +0100 | [diff] [blame] | 728 | gb_connection_svc_connection_destroy(connection); |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 729 | err_hd_cport_clear: |
| 730 | gb_connection_hd_cport_clear(connection); |
| 731 | |
Johan Hovold | 3ea6a81 | 2016-01-08 20:13:46 +0100 | [diff] [blame] | 732 | gb_connection_hd_cport_disable(connection); |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 733 | |
| 734 | return ret; |
| 735 | } |
| 736 | |
| 737 | int 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 Elder | 79c8c64 | 2016-06-03 15:55:37 -0500 | [diff] [blame] | 747 | if (!ret) |
| 748 | trace_gb_connection_enable(connection); |
| 749 | |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 750 | out_unlock: |
Johan Hovold | 2326878 | 2016-01-19 12:51:06 +0100 | [diff] [blame] | 751 | mutex_unlock(&connection->mutex); |
Johan Hovold | 3ea6a81 | 2016-01-08 20:13:46 +0100 | [diff] [blame] | 752 | |
| 753 | return ret; |
| 754 | } |
| 755 | EXPORT_SYMBOL_GPL(gb_connection_enable); |
| 756 | |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 757 | int 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 Elder | 79c8c64 | 2016-06-03 15:55:37 -0500 | [diff] [blame] | 772 | if (!ret) |
| 773 | trace_gb_connection_enable(connection); |
| 774 | |
Johan Hovold | f7ee081 | 2016-01-21 17:34:21 +0100 | [diff] [blame] | 775 | out_unlock: |
| 776 | mutex_unlock(&connection->mutex); |
| 777 | |
| 778 | return ret; |
| 779 | } |
| 780 | EXPORT_SYMBOL_GPL(gb_connection_enable_tx); |
| 781 | |
Johan Hovold | beb6b7f | 2016-01-19 12:51:08 +0100 | [diff] [blame] | 782 | void gb_connection_disable_rx(struct gb_connection *connection) |
| 783 | { |
| 784 | mutex_lock(&connection->mutex); |
| 785 | |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 786 | spin_lock_irq(&connection->lock); |
Johan Hovold | beb6b7f | 2016-01-19 12:51:08 +0100 | [diff] [blame] | 787 | if (connection->state != GB_CONNECTION_STATE_ENABLED) { |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 788 | spin_unlock_irq(&connection->lock); |
Johan Hovold | beb6b7f | 2016-01-19 12:51:08 +0100 | [diff] [blame] | 789 | goto out_unlock; |
| 790 | } |
| 791 | connection->state = GB_CONNECTION_STATE_ENABLED_TX; |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 792 | gb_connection_flush_incoming_operations(connection, -ESHUTDOWN); |
| 793 | spin_unlock_irq(&connection->lock); |
Johan Hovold | beb6b7f | 2016-01-19 12:51:08 +0100 | [diff] [blame] | 794 | |
Alex Elder | 79c8c64 | 2016-06-03 15:55:37 -0500 | [diff] [blame] | 795 | trace_gb_connection_disable(connection); |
| 796 | |
Johan Hovold | beb6b7f | 2016-01-19 12:51:08 +0100 | [diff] [blame] | 797 | out_unlock: |
| 798 | mutex_unlock(&connection->mutex); |
| 799 | } |
Vaibhav Hiremath | 6d58e71 | 2016-05-05 14:32:29 +0530 | [diff] [blame] | 800 | EXPORT_SYMBOL_GPL(gb_connection_disable_rx); |
Johan Hovold | beb6b7f | 2016-01-19 12:51:08 +0100 | [diff] [blame] | 801 | |
Johan Hovold | 55742d2 | 2016-05-27 17:26:40 +0200 | [diff] [blame] | 802 | void gb_connection_mode_switch_prepare(struct gb_connection *connection) |
| 803 | { |
| 804 | connection->mode_switch = true; |
| 805 | } |
| 806 | |
| 807 | void gb_connection_mode_switch_complete(struct gb_connection *connection) |
| 808 | { |
| 809 | gb_connection_svc_connection_destroy(connection); |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 810 | gb_connection_hd_cport_clear(connection); |
| 811 | |
Johan Hovold | 55742d2 | 2016-05-27 17:26:40 +0200 | [diff] [blame] | 812 | gb_connection_hd_cport_disable(connection); |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 813 | |
Johan Hovold | 55742d2 | 2016-05-27 17:26:40 +0200 | [diff] [blame] | 814 | connection->mode_switch = false; |
| 815 | } |
| 816 | |
Johan Hovold | 3ea6a81 | 2016-01-08 20:13:46 +0100 | [diff] [blame] | 817 | void gb_connection_disable(struct gb_connection *connection) |
| 818 | { |
Johan Hovold | 2326878 | 2016-01-19 12:51:06 +0100 | [diff] [blame] | 819 | mutex_lock(&connection->mutex); |
| 820 | |
Johan Hovold | 81fba24 | 2016-01-19 12:51:03 +0100 | [diff] [blame] | 821 | if (connection->state == GB_CONNECTION_STATE_DISABLED) |
Johan Hovold | 2326878 | 2016-01-19 12:51:06 +0100 | [diff] [blame] | 822 | goto out_unlock; |
Johan Hovold | 81fba24 | 2016-01-19 12:51:03 +0100 | [diff] [blame] | 823 | |
Viresh Kumar | 0698be0 | 2016-06-15 08:25:56 +0530 | [diff] [blame] | 824 | trace_gb_connection_disable(connection); |
| 825 | |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 826 | spin_lock_irq(&connection->lock); |
Johan Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 827 | connection->state = GB_CONNECTION_STATE_DISCONNECTING; |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 828 | gb_connection_cancel_operations(connection, -ESHUTDOWN); |
| 829 | spin_unlock_irq(&connection->lock); |
Johan Hovold | 81fba24 | 2016-01-19 12:51:03 +0100 | [diff] [blame] | 830 | |
Johan Hovold | 800d6c8 | 2016-05-27 17:26:37 +0200 | [diff] [blame] | 831 | gb_connection_hd_cport_flush(connection); |
| 832 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 833 | 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 Hovold | 3de5acf | 2016-05-27 17:26:36 +0200 | [diff] [blame] | 837 | gb_connection_control_disconnected(connection); |
| 838 | |
| 839 | connection->state = GB_CONNECTION_STATE_DISABLED; |
| 840 | |
Johan Hovold | 55742d2 | 2016-05-27 17:26:40 +0200 | [diff] [blame] | 841 | /* control-connection tear down is deferred when mode switching */ |
| 842 | if (!connection->mode_switch) { |
| 843 | gb_connection_svc_connection_destroy(connection); |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 844 | gb_connection_hd_cport_clear(connection); |
| 845 | |
Johan Hovold | 55742d2 | 2016-05-27 17:26:40 +0200 | [diff] [blame] | 846 | gb_connection_hd_cport_disable(connection); |
| 847 | } |
Johan Hovold | 2326878 | 2016-01-19 12:51:06 +0100 | [diff] [blame] | 848 | |
| 849 | out_unlock: |
| 850 | mutex_unlock(&connection->mutex); |
Johan Hovold | 3ea6a81 | 2016-01-08 20:13:46 +0100 | [diff] [blame] | 851 | } |
| 852 | EXPORT_SYMBOL_GPL(gb_connection_disable); |
| 853 | |
Johan Hovold | 7aefe79 | 2016-05-27 17:26:22 +0200 | [diff] [blame] | 854 | /* Disable a connection without communicating with the remote end. */ |
| 855 | void 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 Kumar | 0698be0 | 2016-06-15 08:25:56 +0530 | [diff] [blame] | 862 | trace_gb_connection_disable(connection); |
| 863 | |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 864 | spin_lock_irq(&connection->lock); |
Johan Hovold | 7aefe79 | 2016-05-27 17:26:22 +0200 | [diff] [blame] | 865 | connection->state = GB_CONNECTION_STATE_DISABLED; |
Johan Hovold | a29bac6 | 2016-06-27 20:07:09 +0200 | [diff] [blame] | 866 | gb_connection_cancel_operations(connection, -ESHUTDOWN); |
| 867 | spin_unlock_irq(&connection->lock); |
Johan Hovold | 7aefe79 | 2016-05-27 17:26:22 +0200 | [diff] [blame] | 868 | |
Johan Hovold | 800d6c8 | 2016-05-27 17:26:37 +0200 | [diff] [blame] | 869 | gb_connection_hd_cport_flush(connection); |
Johan Hovold | 7aefe79 | 2016-05-27 17:26:22 +0200 | [diff] [blame] | 870 | |
Johan Hovold | aac0839 | 2016-08-26 12:55:49 +0200 | [diff] [blame] | 871 | gb_connection_svc_connection_destroy(connection); |
| 872 | gb_connection_hd_cport_clear(connection); |
| 873 | |
| 874 | gb_connection_hd_cport_disable(connection); |
Johan Hovold | 7aefe79 | 2016-05-27 17:26:22 +0200 | [diff] [blame] | 875 | out_unlock: |
| 876 | mutex_unlock(&connection->mutex); |
| 877 | } |
| 878 | EXPORT_SYMBOL_GPL(gb_connection_disable_forced); |
| 879 | |
Johan Hovold | c3681f6 | 2016-01-19 12:51:23 +0100 | [diff] [blame] | 880 | /* Caller must have disabled the connection before destroying it. */ |
Viresh Kumar | fda2381 | 2015-08-31 17:21:13 +0530 | [diff] [blame] | 881 | void gb_connection_destroy(struct gb_connection *connection) |
| 882 | { |
Johan Hovold | 2edbf5f | 2016-01-19 12:51:22 +0100 | [diff] [blame] | 883 | if (!connection) |
Viresh Kumar | fda2381 | 2015-08-31 17:21:13 +0530 | [diff] [blame] | 884 | return; |
| 885 | |
Alex Elder | 814ae531 | 2016-05-17 09:13:16 -0500 | [diff] [blame] | 886 | if (WARN_ON(connection->state != GB_CONNECTION_STATE_DISABLED)) |
| 887 | gb_connection_disable(connection); |
| 888 | |
Johan Hovold | 210b508 | 2016-01-19 12:51:26 +0100 | [diff] [blame] | 889 | mutex_lock(&gb_connection_mutex); |
| 890 | |
Johan Hovold | 0b1d262 | 2016-06-27 20:07:10 +0200 | [diff] [blame] | 891 | spin_lock_irq(&gb_connections_lock); |
Viresh Kumar | fda2381 | 2015-08-31 17:21:13 +0530 | [diff] [blame] | 892 | list_del(&connection->bundle_links); |
| 893 | list_del(&connection->hd_links); |
Johan Hovold | 0b1d262 | 2016-06-27 20:07:10 +0200 | [diff] [blame] | 894 | spin_unlock_irq(&gb_connections_lock); |
Viresh Kumar | fda2381 | 2015-08-31 17:21:13 +0530 | [diff] [blame] | 895 | |
Johan Hovold | c3681f6 | 2016-01-19 12:51:23 +0100 | [diff] [blame] | 896 | destroy_workqueue(connection->wq); |
| 897 | |
Johan Hovold | 74a5d93 | 2016-05-11 10:17:59 +0200 | [diff] [blame] | 898 | gb_hd_cport_release(connection->hd, connection->hd_cport_id); |
Viresh Kumar | fda2381 | 2015-08-31 17:21:13 +0530 | [diff] [blame] | 899 | connection->hd_cport_id = CPORT_ID_BAD; |
| 900 | |
Johan Hovold | 210b508 | 2016-01-19 12:51:26 +0100 | [diff] [blame] | 901 | mutex_unlock(&gb_connection_mutex); |
| 902 | |
Johan Hovold | 0e46fab | 2016-01-19 12:51:25 +0100 | [diff] [blame] | 903 | gb_connection_put(connection); |
Viresh Kumar | fda2381 | 2015-08-31 17:21:13 +0530 | [diff] [blame] | 904 | } |
Johan Hovold | 98fdf5a | 2016-01-21 17:34:09 +0100 | [diff] [blame] | 905 | EXPORT_SYMBOL_GPL(gb_connection_destroy); |
Viresh Kumar | fda2381 | 2015-08-31 17:21:13 +0530 | [diff] [blame] | 906 | |
Bryan O'Donoghue | e7e2efc | 2015-10-15 16:10:42 +0100 | [diff] [blame] | 907 | void gb_connection_latency_tag_enable(struct gb_connection *connection) |
| 908 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 909 | struct gb_host_device *hd = connection->hd; |
Bryan O'Donoghue | e7e2efc | 2015-10-15 16:10:42 +0100 | [diff] [blame] | 910 | 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 Hovold | 4c4b502 | 2015-11-25 15:59:15 +0100 | [diff] [blame] | 917 | dev_err(&connection->hd->dev, |
| 918 | "%s: failed to enable latency tag: %d\n", |
| 919 | connection->name, ret); |
Bryan O'Donoghue | e7e2efc | 2015-10-15 16:10:42 +0100 | [diff] [blame] | 920 | } |
| 921 | } |
| 922 | EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable); |
| 923 | |
| 924 | void gb_connection_latency_tag_disable(struct gb_connection *connection) |
| 925 | { |
Johan Hovold | 2537636 | 2015-11-03 18:03:23 +0100 | [diff] [blame] | 926 | struct gb_host_device *hd = connection->hd; |
Bryan O'Donoghue | e7e2efc | 2015-10-15 16:10:42 +0100 | [diff] [blame] | 927 | 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 Hovold | 4c4b502 | 2015-11-25 15:59:15 +0100 | [diff] [blame] | 934 | dev_err(&connection->hd->dev, |
| 935 | "%s: failed to disable latency tag: %d\n", |
| 936 | connection->name, ret); |
Bryan O'Donoghue | e7e2efc | 2015-10-15 16:10:42 +0100 | [diff] [blame] | 937 | } |
| 938 | } |
| 939 | EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable); |