4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/workqueue.h>
14 static DEFINE_SPINLOCK(gb_connections_lock);
16 /* This is only used at initialization time; no locking is required. */
17 static struct gb_connection *
18 gb_connection_intf_find(struct gb_interface *intf, u16 cport_id)
20 struct gb_host_device *hd = intf->hd;
21 struct gb_connection *connection;
23 list_for_each_entry(connection, &hd->connections, hd_links) {
24 if (connection->intf == intf &&
25 connection->intf_cport_id == cport_id)
32 static struct gb_connection *
33 gb_connection_hd_find(struct gb_host_device *hd, u16 cport_id)
35 struct gb_connection *connection;
38 spin_lock_irqsave(&gb_connections_lock, flags);
39 list_for_each_entry(connection, &hd->connections, hd_links)
40 if (connection->hd_cport_id == cport_id)
44 spin_unlock_irqrestore(&gb_connections_lock, flags);
50 * Callback from the host driver to let us know that data has been
51 * received on the bundle.
53 void greybus_data_rcvd(struct gb_host_device *hd, u16 cport_id,
54 u8 *data, size_t length)
56 struct gb_connection *connection;
58 connection = gb_connection_hd_find(hd, cport_id);
61 "nonexistent connection (%zu bytes dropped)\n", length);
64 gb_connection_recv(connection, data, length);
66 EXPORT_SYMBOL_GPL(greybus_data_rcvd);
68 static DEFINE_MUTEX(connection_mutex);
70 static void gb_connection_kref_release(struct kref *kref)
72 struct gb_connection *connection;
74 connection = container_of(kref, struct gb_connection, kref);
75 destroy_workqueue(connection->wq);
77 mutex_unlock(&connection_mutex);
80 int svc_update_connection(struct gb_interface *intf,
81 struct gb_connection *connection)
83 struct gb_bundle *bundle;
85 bundle = gb_bundle_create(intf, GB_SVC_BUNDLE_ID, GREYBUS_CLASS_SVC);
89 connection->bundle = bundle;
91 spin_lock_irq(&gb_connections_lock);
92 list_add(&connection->bundle_links, &bundle->connections);
93 spin_unlock_irq(&gb_connections_lock);
98 static void gb_connection_init_name(struct gb_connection *connection)
100 u16 hd_cport_id = connection->hd_cport_id;
104 if (connection->intf) {
105 intf_id = connection->intf->interface_id;
106 cport_id = connection->intf_cport_id;
109 snprintf(connection->name, sizeof(connection->name),
110 "%hu/%hhu:%hu", hd_cport_id, intf_id, cport_id);
114 * gb_connection_create() - create a Greybus connection
115 * @hd: host device of the connection
116 * @hd_cport_id: host-device cport id, or -1 for dynamic allocation
117 * @intf: remote interface, or NULL for static connections
118 * @bundle: remote-interface bundle (may be NULL)
119 * @cport_id: remote-interface cport id, or 0 for static connections
120 * @protocol_id: protocol id
122 * Create a Greybus connection, representing the bidirectional link
123 * between a CPort on a (local) Greybus host device and a CPort on
124 * another Greybus interface.
126 * A connection also maintains the state of operations sent over the
129 * Return: A pointer to the new connection if successful, or NULL otherwise.
131 static struct gb_connection *
132 gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
133 struct gb_interface *intf,
134 struct gb_bundle *bundle, int cport_id,
137 struct gb_connection *connection;
138 struct ida *id_map = &hd->cport_id_map;
139 int ida_start, ida_end;
145 * If a manifest tries to reuse a cport, reject it. We
146 * initialize connections serially so we don't need to worry
147 * about holding the connection lock.
149 if (bundle && gb_connection_intf_find(bundle->intf, cport_id)) {
150 dev_err(&bundle->dev, "cport 0x%04hx already connected\n",
155 if (hd_cport_id < 0) {
157 ida_end = hd->num_cports;
158 } else if (hd_cport_id < hd->num_cports) {
159 ida_start = hd_cport_id;
160 ida_end = hd_cport_id + 1;
162 dev_err(&hd->dev, "cport %d not available\n", hd_cport_id);
166 hd_cport_id = ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
170 connection = kzalloc(sizeof(*connection), GFP_KERNEL);
174 connection->hd_cport_id = hd_cport_id;
175 connection->intf_cport_id = cport_id;
177 connection->intf = intf;
179 connection->protocol_id = protocol_id;
180 connection->major = major;
181 connection->minor = minor;
183 connection->bundle = bundle;
184 connection->state = GB_CONNECTION_STATE_DISABLED;
186 atomic_set(&connection->op_cycle, 0);
187 spin_lock_init(&connection->lock);
188 INIT_LIST_HEAD(&connection->operations);
190 connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
191 dev_name(&hd->dev), hd_cport_id);
193 goto err_free_connection;
195 kref_init(&connection->kref);
197 gb_connection_init_name(connection);
199 spin_lock_irq(&gb_connections_lock);
200 list_add(&connection->hd_links, &hd->connections);
203 list_add(&connection->bundle_links, &bundle->connections);
205 INIT_LIST_HEAD(&connection->bundle_links);
207 spin_unlock_irq(&gb_connections_lock);
209 retval = gb_connection_bind_protocol(connection);
211 dev_err(&hd->dev, "%d: failed to bind protocol: %d\n",
213 gb_connection_destroy(connection);
222 ida_simple_remove(id_map, hd_cport_id);
227 struct gb_connection *
228 gb_connection_create_static(struct gb_host_device *hd,
229 u16 hd_cport_id, u8 protocol_id)
231 return gb_connection_create(hd, hd_cport_id, NULL, NULL, 0,
235 struct gb_connection *
236 gb_connection_create_dynamic(struct gb_interface *intf,
237 struct gb_bundle *bundle,
238 u16 cport_id, u8 protocol_id)
240 return gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
244 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
246 struct gb_host_device *hd = connection->hd;
249 if (!hd->driver->cport_enable)
252 ret = hd->driver->cport_enable(hd, connection->hd_cport_id);
255 "failed to enable host cport: %d\n", ret);
262 static void gb_connection_hd_cport_disable(struct gb_connection *connection)
264 struct gb_host_device *hd = connection->hd;
266 if (!hd->driver->cport_disable)
269 hd->driver->cport_disable(hd, connection->hd_cport_id);
273 * Cancel all active operations on a connection.
275 * Should only be called during connection tear down.
277 static void gb_connection_cancel_operations(struct gb_connection *connection,
280 struct gb_operation *operation;
282 spin_lock_irq(&connection->lock);
283 while (!list_empty(&connection->operations)) {
284 operation = list_last_entry(&connection->operations,
285 struct gb_operation, links);
286 gb_operation_get(operation);
287 spin_unlock_irq(&connection->lock);
289 if (gb_operation_is_incoming(operation))
290 gb_operation_cancel_incoming(operation, errno);
292 gb_operation_cancel(operation, errno);
294 gb_operation_put(operation);
296 spin_lock_irq(&connection->lock);
298 spin_unlock_irq(&connection->lock);
302 * Request the SVC to create a connection from AP's cport to interface's
306 gb_connection_svc_connection_create(struct gb_connection *connection)
308 struct gb_host_device *hd = connection->hd;
309 struct gb_interface *intf;
312 if (gb_connection_is_static(connection))
315 intf = connection->bundle->intf;
316 ret = gb_svc_connection_create(hd->svc,
318 connection->hd_cport_id,
320 connection->intf_cport_id,
321 intf->boot_over_unipro);
323 dev_err(&connection->bundle->dev,
324 "failed to create svc connection: %d\n", ret);
332 gb_connection_svc_connection_destroy(struct gb_connection *connection)
334 if (gb_connection_is_static(connection))
337 gb_svc_connection_destroy(connection->hd->svc,
338 connection->hd->svc->ap_intf_id,
339 connection->hd_cport_id,
340 connection->bundle->intf->interface_id,
341 connection->intf_cport_id);
344 /* Inform Interface about active CPorts */
345 static int gb_connection_control_connected(struct gb_connection *connection)
347 struct gb_protocol *protocol = connection->protocol;
348 struct gb_control *control;
349 u16 cport_id = connection->intf_cport_id;
352 if (protocol->flags & GB_PROTOCOL_SKIP_CONTROL_CONNECTED)
355 control = connection->bundle->intf->control;
357 ret = gb_control_connected_operation(control, cport_id);
359 dev_err(&connection->bundle->dev,
360 "failed to connect cport: %d\n", ret);
367 /* Inform Interface about inactive CPorts */
369 gb_connection_control_disconnected(struct gb_connection *connection)
371 struct gb_protocol *protocol = connection->protocol;
372 struct gb_control *control;
373 u16 cport_id = connection->intf_cport_id;
376 if (protocol->flags & GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED)
379 control = connection->bundle->intf->control;
381 ret = gb_control_disconnected_operation(control, cport_id);
383 dev_warn(&connection->bundle->dev,
384 "failed to disconnect cport: %d\n", ret);
389 * Request protocol version supported by the module. We don't need to do
390 * this for SVC as that is initiated by the SVC.
392 static int gb_connection_protocol_get_version(struct gb_connection *connection)
394 struct gb_protocol *protocol = connection->protocol;
397 if (protocol->flags & GB_PROTOCOL_SKIP_VERSION)
400 ret = gb_protocol_get_version(connection);
402 dev_err(&connection->bundle->dev,
403 "failed to get protocol version: %d\n", ret);
410 static int gb_connection_init(struct gb_connection *connection)
412 struct gb_protocol *protocol = connection->protocol;
415 ret = gb_connection_hd_cport_enable(connection);
419 ret = gb_connection_svc_connection_create(connection);
421 goto err_hd_cport_disable;
423 ret = gb_connection_control_connected(connection);
425 goto err_svc_destroy;
427 /* Need to enable the connection to initialize it */
428 spin_lock_irq(&connection->lock);
429 connection->state = GB_CONNECTION_STATE_ENABLED;
430 spin_unlock_irq(&connection->lock);
432 ret = gb_connection_protocol_get_version(connection);
436 ret = protocol->connection_init(connection);
443 spin_lock_irq(&connection->lock);
444 connection->state = GB_CONNECTION_STATE_ERROR;
445 spin_unlock_irq(&connection->lock);
447 gb_connection_control_disconnected(connection);
449 gb_connection_svc_connection_destroy(connection);
450 err_hd_cport_disable:
451 gb_connection_hd_cport_disable(connection);
456 static void gb_connection_exit(struct gb_connection *connection)
458 if (!connection->protocol)
461 spin_lock_irq(&connection->lock);
462 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
463 spin_unlock_irq(&connection->lock);
466 connection->state = GB_CONNECTION_STATE_DESTROYING;
467 spin_unlock_irq(&connection->lock);
469 gb_connection_cancel_operations(connection, -ESHUTDOWN);
471 connection->protocol->connection_exit(connection);
472 gb_connection_control_disconnected(connection);
473 gb_connection_svc_connection_destroy(connection);
474 gb_connection_hd_cport_disable(connection);
478 * Tear down a previously set up connection.
480 void gb_connection_destroy(struct gb_connection *connection)
484 if (WARN_ON(!connection))
487 gb_connection_exit(connection);
489 spin_lock_irq(&gb_connections_lock);
490 list_del(&connection->bundle_links);
491 list_del(&connection->hd_links);
492 spin_unlock_irq(&gb_connections_lock);
494 if (connection->protocol)
495 gb_protocol_put(connection->protocol);
496 connection->protocol = NULL;
498 id_map = &connection->hd->cport_id_map;
499 ida_simple_remove(id_map, connection->hd_cport_id);
500 connection->hd_cport_id = CPORT_ID_BAD;
502 kref_put_mutex(&connection->kref, gb_connection_kref_release,
506 void gb_connection_latency_tag_enable(struct gb_connection *connection)
508 struct gb_host_device *hd = connection->hd;
511 if (!hd->driver->latency_tag_enable)
514 ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
516 dev_err(&connection->bundle->dev,
517 "failed to enable latency tag: %d\n", ret);
520 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
522 void gb_connection_latency_tag_disable(struct gb_connection *connection)
524 struct gb_host_device *hd = connection->hd;
527 if (!hd->driver->latency_tag_disable)
530 ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
532 dev_err(&connection->bundle->dev,
533 "failed to disable latency tag: %d\n", ret);
536 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);
538 int gb_connection_bind_protocol(struct gb_connection *connection)
540 struct gb_protocol *protocol;
543 /* If we already have a protocol bound here, just return */
544 if (connection->protocol)
547 protocol = gb_protocol_get(connection->protocol_id,
551 dev_warn(&connection->hd->dev,
552 "protocol 0x%02hhx version %hhu.%hhu not found\n",
553 connection->protocol_id,
554 connection->major, connection->minor);
557 connection->protocol = protocol;
560 * If we have a valid device_id for the interface block, then we have an
561 * active device, so bring up the connection at the same time.
563 if ((!connection->bundle &&
564 protocol->flags & GB_PROTOCOL_NO_BUNDLE) ||
565 connection->bundle->intf->device_id != GB_DEVICE_ID_BAD) {
566 ret = gb_connection_init(connection);
568 gb_protocol_put(protocol);
569 connection->protocol = NULL;