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