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);
99 * gb_connection_create() - create a Greybus connection
100 * @hd: host device of the connection
101 * @hd_cport_id: host-device cport id, or -1 for dynamic allocation
102 * @intf: remote interface, or NULL for static connections
103 * @bundle: remote-interface bundle (may be NULL)
104 * @cport_id: remote-interface cport id, or 0 for static connections
105 * @protocol_id: protocol id
107 * Create a Greybus connection, representing the bidirectional link
108 * between a CPort on a (local) Greybus host device and a CPort on
109 * another Greybus interface.
111 * A connection also maintains the state of operations sent over the
114 * Return: A pointer to the new connection if successful, or NULL otherwise.
116 static struct gb_connection *
117 gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
118 struct gb_interface *intf,
119 struct gb_bundle *bundle, int cport_id,
122 struct gb_connection *connection;
123 struct ida *id_map = &hd->cport_id_map;
124 int ida_start, ida_end;
130 * If a manifest tries to reuse a cport, reject it. We
131 * initialize connections serially so we don't need to worry
132 * about holding the connection lock.
134 if (bundle && gb_connection_intf_find(bundle->intf, cport_id)) {
135 dev_err(&bundle->dev, "cport 0x%04hx already connected\n",
140 if (hd_cport_id < 0) {
142 ida_end = hd->num_cports;
143 } else if (hd_cport_id < hd->num_cports) {
144 ida_start = hd_cport_id;
145 ida_end = hd_cport_id + 1;
147 dev_err(&hd->dev, "cport %d not available\n", hd_cport_id);
151 hd_cport_id = ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
155 connection = kzalloc(sizeof(*connection), GFP_KERNEL);
159 connection->hd_cport_id = hd_cport_id;
160 connection->intf_cport_id = cport_id;
162 connection->intf = intf;
164 connection->protocol_id = protocol_id;
165 connection->major = major;
166 connection->minor = minor;
168 connection->bundle = bundle;
169 connection->state = GB_CONNECTION_STATE_DISABLED;
171 atomic_set(&connection->op_cycle, 0);
172 spin_lock_init(&connection->lock);
173 INIT_LIST_HEAD(&connection->operations);
175 connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
176 dev_name(&hd->dev), hd_cport_id);
178 goto err_free_connection;
180 kref_init(&connection->kref);
182 spin_lock_irq(&gb_connections_lock);
183 list_add(&connection->hd_links, &hd->connections);
186 list_add(&connection->bundle_links, &bundle->connections);
188 INIT_LIST_HEAD(&connection->bundle_links);
190 spin_unlock_irq(&gb_connections_lock);
192 retval = gb_connection_bind_protocol(connection);
194 dev_err(&hd->dev, "%d: failed to bind protocol: %d\n",
196 gb_connection_destroy(connection);
205 ida_simple_remove(id_map, hd_cport_id);
210 struct gb_connection *
211 gb_connection_create_static(struct gb_host_device *hd,
212 u16 hd_cport_id, u8 protocol_id)
214 return gb_connection_create(hd, hd_cport_id, NULL, NULL, 0,
218 struct gb_connection *
219 gb_connection_create_dynamic(struct gb_interface *intf,
220 struct gb_bundle *bundle,
221 u16 cport_id, u8 protocol_id)
223 return gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
227 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
229 struct gb_host_device *hd = connection->hd;
232 if (!hd->driver->cport_enable)
235 ret = hd->driver->cport_enable(hd, connection->hd_cport_id);
238 "failed to enable host cport: %d\n", ret);
245 static void gb_connection_hd_cport_disable(struct gb_connection *connection)
247 struct gb_host_device *hd = connection->hd;
249 if (!hd->driver->cport_disable)
252 hd->driver->cport_disable(hd, connection->hd_cport_id);
256 * Cancel all active operations on a connection.
258 * Should only be called during connection tear down.
260 static void gb_connection_cancel_operations(struct gb_connection *connection,
263 struct gb_operation *operation;
265 spin_lock_irq(&connection->lock);
266 while (!list_empty(&connection->operations)) {
267 operation = list_last_entry(&connection->operations,
268 struct gb_operation, links);
269 gb_operation_get(operation);
270 spin_unlock_irq(&connection->lock);
272 if (gb_operation_is_incoming(operation))
273 gb_operation_cancel_incoming(operation, errno);
275 gb_operation_cancel(operation, errno);
277 gb_operation_put(operation);
279 spin_lock_irq(&connection->lock);
281 spin_unlock_irq(&connection->lock);
285 * Request the SVC to create a connection from AP's cport to interface's
289 gb_connection_svc_connection_create(struct gb_connection *connection)
291 struct gb_host_device *hd = connection->hd;
292 struct gb_protocol *protocol = connection->protocol;
293 struct gb_interface *intf;
296 if (protocol->flags & GB_PROTOCOL_SKIP_SVC_CONNECTION)
299 intf = connection->bundle->intf;
300 ret = gb_svc_connection_create(hd->svc,
302 connection->hd_cport_id,
304 connection->intf_cport_id,
305 intf->boot_over_unipro);
307 dev_err(&connection->bundle->dev,
308 "failed to create svc connection: %d\n", ret);
316 gb_connection_svc_connection_destroy(struct gb_connection *connection)
318 if (connection->protocol->flags & GB_PROTOCOL_SKIP_SVC_CONNECTION)
321 gb_svc_connection_destroy(connection->hd->svc,
322 connection->hd->svc->ap_intf_id,
323 connection->hd_cport_id,
324 connection->bundle->intf->interface_id,
325 connection->intf_cport_id);
328 /* Inform Interface about active CPorts */
329 static int gb_connection_control_connected(struct gb_connection *connection)
331 struct gb_protocol *protocol = connection->protocol;
332 struct gb_control *control;
333 u16 cport_id = connection->intf_cport_id;
336 if (protocol->flags & GB_PROTOCOL_SKIP_CONTROL_CONNECTED)
339 control = connection->bundle->intf->control;
341 ret = gb_control_connected_operation(control, cport_id);
343 dev_err(&connection->bundle->dev,
344 "failed to connect cport: %d\n", ret);
351 /* Inform Interface about inactive CPorts */
353 gb_connection_control_disconnected(struct gb_connection *connection)
355 struct gb_protocol *protocol = connection->protocol;
356 struct gb_control *control;
357 u16 cport_id = connection->intf_cport_id;
360 if (protocol->flags & GB_PROTOCOL_SKIP_CONTROL_DISCONNECTED)
363 control = connection->bundle->intf->control;
365 ret = gb_control_disconnected_operation(control, cport_id);
367 dev_warn(&connection->bundle->dev,
368 "failed to disconnect cport: %d\n", ret);
373 * Request protocol version supported by the module. We don't need to do
374 * this for SVC as that is initiated by the SVC.
376 static int gb_connection_protocol_get_version(struct gb_connection *connection)
378 struct gb_protocol *protocol = connection->protocol;
381 if (protocol->flags & GB_PROTOCOL_SKIP_VERSION)
384 ret = gb_protocol_get_version(connection);
386 dev_err(&connection->bundle->dev,
387 "failed to get protocol version: %d\n", ret);
394 static int gb_connection_init(struct gb_connection *connection)
396 struct gb_protocol *protocol = connection->protocol;
399 ret = gb_connection_hd_cport_enable(connection);
403 ret = gb_connection_svc_connection_create(connection);
405 goto err_hd_cport_disable;
407 ret = gb_connection_control_connected(connection);
409 goto err_svc_destroy;
411 /* Need to enable the connection to initialize it */
412 spin_lock_irq(&connection->lock);
413 connection->state = GB_CONNECTION_STATE_ENABLED;
414 spin_unlock_irq(&connection->lock);
416 ret = gb_connection_protocol_get_version(connection);
420 ret = protocol->connection_init(connection);
427 spin_lock_irq(&connection->lock);
428 connection->state = GB_CONNECTION_STATE_ERROR;
429 spin_unlock_irq(&connection->lock);
431 gb_connection_control_disconnected(connection);
433 gb_connection_svc_connection_destroy(connection);
434 err_hd_cport_disable:
435 gb_connection_hd_cport_disable(connection);
440 static void gb_connection_exit(struct gb_connection *connection)
442 if (!connection->protocol)
445 spin_lock_irq(&connection->lock);
446 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
447 spin_unlock_irq(&connection->lock);
450 connection->state = GB_CONNECTION_STATE_DESTROYING;
451 spin_unlock_irq(&connection->lock);
453 gb_connection_cancel_operations(connection, -ESHUTDOWN);
455 connection->protocol->connection_exit(connection);
456 gb_connection_control_disconnected(connection);
457 gb_connection_svc_connection_destroy(connection);
458 gb_connection_hd_cport_disable(connection);
462 * Tear down a previously set up connection.
464 void gb_connection_destroy(struct gb_connection *connection)
468 if (WARN_ON(!connection))
471 gb_connection_exit(connection);
473 spin_lock_irq(&gb_connections_lock);
474 list_del(&connection->bundle_links);
475 list_del(&connection->hd_links);
476 spin_unlock_irq(&gb_connections_lock);
478 if (connection->protocol)
479 gb_protocol_put(connection->protocol);
480 connection->protocol = NULL;
482 id_map = &connection->hd->cport_id_map;
483 ida_simple_remove(id_map, connection->hd_cport_id);
484 connection->hd_cport_id = CPORT_ID_BAD;
486 kref_put_mutex(&connection->kref, gb_connection_kref_release,
490 void gb_connection_latency_tag_enable(struct gb_connection *connection)
492 struct gb_host_device *hd = connection->hd;
495 if (!hd->driver->latency_tag_enable)
498 ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
500 dev_err(&connection->bundle->dev,
501 "failed to enable latency tag: %d\n", ret);
504 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
506 void gb_connection_latency_tag_disable(struct gb_connection *connection)
508 struct gb_host_device *hd = connection->hd;
511 if (!hd->driver->latency_tag_disable)
514 ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
516 dev_err(&connection->bundle->dev,
517 "failed to disable latency tag: %d\n", ret);
520 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);
522 int gb_connection_bind_protocol(struct gb_connection *connection)
524 struct gb_protocol *protocol;
527 /* If we already have a protocol bound here, just return */
528 if (connection->protocol)
531 protocol = gb_protocol_get(connection->protocol_id,
535 dev_warn(&connection->hd->dev,
536 "protocol 0x%02hhx version %hhu.%hhu not found\n",
537 connection->protocol_id,
538 connection->major, connection->minor);
541 connection->protocol = protocol;
544 * If we have a valid device_id for the interface block, then we have an
545 * active device, so bring up the connection at the same time.
547 if ((!connection->bundle &&
548 protocol->flags & GB_PROTOCOL_NO_BUNDLE) ||
549 connection->bundle->intf->device_id != GB_DEVICE_ID_BAD) {
550 ret = gb_connection_init(connection);
552 gb_protocol_put(protocol);
553 connection->protocol = NULL;