4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/workqueue.h>
15 static int gb_connection_bind_protocol(struct gb_connection *connection);
16 static void gb_connection_unbind_protocol(struct gb_connection *connection);
19 static DEFINE_SPINLOCK(gb_connections_lock);
21 /* This is only used at initialization time; no locking is required. */
22 static struct gb_connection *
23 gb_connection_intf_find(struct gb_interface *intf, u16 cport_id)
25 struct gb_host_device *hd = intf->hd;
26 struct gb_connection *connection;
28 list_for_each_entry(connection, &hd->connections, hd_links) {
29 if (connection->intf == intf &&
30 connection->intf_cport_id == cport_id)
37 static struct gb_connection *
38 gb_connection_hd_find(struct gb_host_device *hd, u16 cport_id)
40 struct gb_connection *connection;
43 spin_lock_irqsave(&gb_connections_lock, flags);
44 list_for_each_entry(connection, &hd->connections, hd_links)
45 if (connection->hd_cport_id == cport_id)
49 spin_unlock_irqrestore(&gb_connections_lock, flags);
55 * Callback from the host driver to let us know that data has been
56 * received on the bundle.
58 void greybus_data_rcvd(struct gb_host_device *hd, u16 cport_id,
59 u8 *data, size_t length)
61 struct gb_connection *connection;
63 connection = gb_connection_hd_find(hd, cport_id);
66 "nonexistent connection (%zu bytes dropped)\n", length);
69 gb_connection_recv(connection, data, length);
71 EXPORT_SYMBOL_GPL(greybus_data_rcvd);
73 static DEFINE_MUTEX(connection_mutex);
75 static void gb_connection_kref_release(struct kref *kref)
77 struct gb_connection *connection;
79 connection = container_of(kref, struct gb_connection, kref);
80 destroy_workqueue(connection->wq);
82 mutex_unlock(&connection_mutex);
85 static void gb_connection_init_name(struct gb_connection *connection)
87 u16 hd_cport_id = connection->hd_cport_id;
91 if (connection->intf) {
92 intf_id = connection->intf->interface_id;
93 cport_id = connection->intf_cport_id;
96 snprintf(connection->name, sizeof(connection->name),
97 "%u/%u:%u", hd_cport_id, intf_id, cport_id);
101 * gb_connection_create() - create a Greybus connection
102 * @hd: host device of the connection
103 * @hd_cport_id: host-device cport id, or -1 for dynamic allocation
104 * @intf: remote interface, or NULL for static connections
105 * @bundle: remote-interface bundle (may be NULL)
106 * @cport_id: remote-interface cport id, or 0 for static connections
107 * @protocol_id: protocol id
109 * Create a Greybus connection, representing the bidirectional link
110 * between a CPort on a (local) Greybus host device and a CPort on
111 * another Greybus interface.
113 * A connection also maintains the state of operations sent over the
116 * Return: A pointer to the new connection if successful, or NULL otherwise.
118 static struct gb_connection *
119 gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
120 struct gb_interface *intf,
121 struct gb_bundle *bundle, int cport_id,
124 struct gb_connection *connection;
125 struct ida *id_map = &hd->cport_id_map;
126 int ida_start, ida_end;
131 * If a manifest tries to reuse a cport, reject it. We
132 * initialize connections serially so we don't need to worry
133 * about holding the connection lock.
135 if (bundle && gb_connection_intf_find(bundle->intf, cport_id)) {
136 dev_err(&bundle->dev, "cport %u already connected\n",
141 if (hd_cport_id < 0) {
143 ida_end = hd->num_cports;
144 } else if (hd_cport_id < hd->num_cports) {
145 ida_start = hd_cport_id;
146 ida_end = hd_cport_id + 1;
148 dev_err(&hd->dev, "cport %d not available\n", hd_cport_id);
152 hd_cport_id = ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
156 connection = kzalloc(sizeof(*connection), GFP_KERNEL);
160 connection->hd_cport_id = hd_cport_id;
161 connection->intf_cport_id = cport_id;
163 connection->intf = intf;
165 connection->protocol_id = protocol_id;
166 connection->major = major;
167 connection->minor = minor;
169 connection->bundle = bundle;
170 connection->state = GB_CONNECTION_STATE_DISABLED;
172 atomic_set(&connection->op_cycle, 0);
173 mutex_init(&connection->mutex);
174 spin_lock_init(&connection->lock);
175 INIT_LIST_HEAD(&connection->operations);
177 connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
178 dev_name(&hd->dev), hd_cport_id);
180 goto err_free_connection;
182 kref_init(&connection->kref);
184 gb_connection_init_name(connection);
186 spin_lock_irq(&gb_connections_lock);
187 list_add(&connection->hd_links, &hd->connections);
190 list_add(&connection->bundle_links, &bundle->connections);
192 INIT_LIST_HEAD(&connection->bundle_links);
194 spin_unlock_irq(&gb_connections_lock);
201 ida_simple_remove(id_map, hd_cport_id);
206 struct gb_connection *
207 gb_connection_create_static(struct gb_host_device *hd,
208 u16 hd_cport_id, u8 protocol_id)
210 return gb_connection_create(hd, hd_cport_id, NULL, NULL, 0,
214 struct gb_connection *
215 gb_connection_create_dynamic(struct gb_interface *intf,
216 struct gb_bundle *bundle,
217 u16 cport_id, u8 protocol_id)
219 return gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
223 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
225 struct gb_host_device *hd = connection->hd;
228 if (!hd->driver->cport_enable)
231 ret = hd->driver->cport_enable(hd, connection->hd_cport_id);
234 "failed to enable host cport: %d\n", ret);
241 static void gb_connection_hd_cport_disable(struct gb_connection *connection)
243 struct gb_host_device *hd = connection->hd;
245 if (!hd->driver->cport_disable)
248 hd->driver->cport_disable(hd, connection->hd_cport_id);
252 * Request the SVC to create a connection from AP's cport to interface's
256 gb_connection_svc_connection_create(struct gb_connection *connection)
258 struct gb_host_device *hd = connection->hd;
259 struct gb_interface *intf;
262 if (gb_connection_is_static(connection))
265 intf = connection->intf;
266 ret = gb_svc_connection_create(hd->svc,
268 connection->hd_cport_id,
270 connection->intf_cport_id,
271 intf->boot_over_unipro);
273 dev_err(&connection->hd->dev,
274 "%s: failed to create svc connection: %d\n",
275 connection->name, ret);
283 gb_connection_svc_connection_destroy(struct gb_connection *connection)
285 if (gb_connection_is_static(connection))
288 gb_svc_connection_destroy(connection->hd->svc,
289 connection->hd->svc->ap_intf_id,
290 connection->hd_cport_id,
291 connection->intf->interface_id,
292 connection->intf_cport_id);
295 /* Inform Interface about active CPorts */
296 static int gb_connection_control_connected(struct gb_connection *connection)
298 struct gb_control *control;
299 u16 cport_id = connection->intf_cport_id;
302 if (gb_connection_is_static(connection))
305 control = connection->intf->control;
307 if (connection == control->connection)
310 ret = gb_control_connected_operation(control, cport_id);
312 dev_err(&connection->bundle->dev,
313 "failed to connect cport: %d\n", ret);
320 /* Inform Interface about inactive CPorts */
322 gb_connection_control_disconnected(struct gb_connection *connection)
324 struct gb_control *control;
325 u16 cport_id = connection->intf_cport_id;
328 if (gb_connection_is_static(connection))
331 control = connection->intf->control;
333 if (connection == control->connection)
336 ret = gb_control_disconnected_operation(control, cport_id);
338 dev_warn(&connection->bundle->dev,
339 "failed to disconnect cport: %d\n", ret);
344 * Request protocol version supported by the module.
346 static int gb_connection_protocol_get_version(struct gb_connection *connection)
350 ret = gb_protocol_get_version(connection);
352 dev_err(&connection->hd->dev,
353 "%s: failed to get protocol version: %d\n",
354 connection->name, ret);
362 * Cancel all active operations on a connection.
364 * Locking: Called with connection lock held and state set to DISABLED.
366 static void gb_connection_cancel_operations(struct gb_connection *connection,
369 struct gb_operation *operation;
371 while (!list_empty(&connection->operations)) {
372 operation = list_last_entry(&connection->operations,
373 struct gb_operation, links);
374 gb_operation_get(operation);
375 spin_unlock_irq(&connection->lock);
377 if (gb_operation_is_incoming(operation))
378 gb_operation_cancel_incoming(operation, errno);
380 gb_operation_cancel(operation, errno);
382 gb_operation_put(operation);
384 spin_lock_irq(&connection->lock);
389 * Cancel all active incoming operations on a connection.
391 * Locking: Called with connection lock held and state set to ENABLED_TX.
394 gb_connection_flush_incoming_operations(struct gb_connection *connection,
397 struct gb_operation *operation;
400 while (!list_empty(&connection->operations)) {
402 list_for_each_entry(operation, &connection->operations,
404 if (gb_operation_is_incoming(operation)) {
405 gb_operation_get(operation);
414 spin_unlock_irq(&connection->lock);
416 /* FIXME: flush, not cancel? */
417 gb_operation_cancel_incoming(operation, errno);
418 gb_operation_put(operation);
420 spin_lock_irq(&connection->lock);
424 int gb_connection_enable(struct gb_connection *connection,
425 gb_request_handler_t handler)
429 mutex_lock(&connection->mutex);
431 if (connection->state == GB_CONNECTION_STATE_ENABLED)
434 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
438 spin_lock_irq(&connection->lock);
439 connection->handler = handler;
440 connection->state = GB_CONNECTION_STATE_ENABLED;
441 spin_unlock_irq(&connection->lock);
446 ret = gb_connection_hd_cport_enable(connection);
450 ret = gb_connection_svc_connection_create(connection);
452 goto err_hd_cport_disable;
454 spin_lock_irq(&connection->lock);
455 connection->handler = handler;
457 connection->state = GB_CONNECTION_STATE_ENABLED;
459 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
460 spin_unlock_irq(&connection->lock);
462 ret = gb_connection_control_connected(connection);
464 goto err_svc_destroy;
467 mutex_unlock(&connection->mutex);
472 spin_lock_irq(&connection->lock);
473 connection->state = GB_CONNECTION_STATE_DISABLED;
474 gb_connection_cancel_operations(connection, -ESHUTDOWN);
475 connection->handler = NULL;
476 spin_unlock_irq(&connection->lock);
478 gb_connection_svc_connection_destroy(connection);
479 err_hd_cport_disable:
480 gb_connection_hd_cport_disable(connection);
482 mutex_unlock(&connection->mutex);
486 EXPORT_SYMBOL_GPL(gb_connection_enable);
488 void gb_connection_disable_rx(struct gb_connection *connection)
490 mutex_lock(&connection->mutex);
492 spin_lock_irq(&connection->lock);
493 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
494 spin_unlock_irq(&connection->lock);
497 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
498 gb_connection_flush_incoming_operations(connection, -ESHUTDOWN);
499 connection->handler = NULL;
500 spin_unlock_irq(&connection->lock);
503 mutex_unlock(&connection->mutex);
506 void gb_connection_disable(struct gb_connection *connection)
508 mutex_lock(&connection->mutex);
510 if (connection->state == GB_CONNECTION_STATE_DISABLED)
513 gb_connection_control_disconnected(connection);
515 spin_lock_irq(&connection->lock);
516 connection->state = GB_CONNECTION_STATE_DISABLED;
517 gb_connection_cancel_operations(connection, -ESHUTDOWN);
518 connection->handler = NULL;
519 spin_unlock_irq(&connection->lock);
521 gb_connection_svc_connection_destroy(connection);
522 gb_connection_hd_cport_disable(connection);
525 mutex_unlock(&connection->mutex);
527 EXPORT_SYMBOL_GPL(gb_connection_disable);
529 static int gb_legacy_request_handler(struct gb_operation *operation)
531 struct gb_protocol *protocol = operation->connection->protocol;
533 return protocol->request_recv(operation->type, operation);
536 int gb_connection_legacy_init(struct gb_connection *connection)
538 gb_request_handler_t handler;
541 ret = gb_connection_bind_protocol(connection);
545 if (connection->protocol->request_recv)
546 handler = gb_legacy_request_handler;
550 ret = gb_connection_enable(connection, handler);
552 goto err_unbind_protocol;
554 ret = gb_connection_protocol_get_version(connection);
558 ret = connection->protocol->connection_init(connection);
565 gb_connection_disable(connection);
567 gb_connection_unbind_protocol(connection);
571 EXPORT_SYMBOL_GPL(gb_connection_legacy_init);
573 void gb_connection_legacy_exit(struct gb_connection *connection)
575 if (connection->state == GB_CONNECTION_STATE_DISABLED)
578 gb_connection_disable(connection);
580 connection->protocol->connection_exit(connection);
582 gb_connection_unbind_protocol(connection);
584 EXPORT_SYMBOL_GPL(gb_connection_legacy_exit);
587 * Tear down a previously set up connection.
589 void gb_connection_destroy(struct gb_connection *connection)
593 if (WARN_ON(!connection))
596 spin_lock_irq(&gb_connections_lock);
597 list_del(&connection->bundle_links);
598 list_del(&connection->hd_links);
599 spin_unlock_irq(&gb_connections_lock);
601 id_map = &connection->hd->cport_id_map;
602 ida_simple_remove(id_map, connection->hd_cport_id);
603 connection->hd_cport_id = CPORT_ID_BAD;
605 kref_put_mutex(&connection->kref, gb_connection_kref_release,
609 void gb_connection_latency_tag_enable(struct gb_connection *connection)
611 struct gb_host_device *hd = connection->hd;
614 if (!hd->driver->latency_tag_enable)
617 ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
619 dev_err(&connection->hd->dev,
620 "%s: failed to enable latency tag: %d\n",
621 connection->name, ret);
624 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
626 void gb_connection_latency_tag_disable(struct gb_connection *connection)
628 struct gb_host_device *hd = connection->hd;
631 if (!hd->driver->latency_tag_disable)
634 ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
636 dev_err(&connection->hd->dev,
637 "%s: failed to disable latency tag: %d\n",
638 connection->name, ret);
641 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);
643 static int gb_connection_bind_protocol(struct gb_connection *connection)
645 struct gb_protocol *protocol;
647 protocol = gb_protocol_get(connection->protocol_id,
651 dev_err(&connection->hd->dev,
652 "protocol 0x%02x version %u.%u not found\n",
653 connection->protocol_id,
654 connection->major, connection->minor);
655 return -EPROTONOSUPPORT;
657 connection->protocol = protocol;
662 static void gb_connection_unbind_protocol(struct gb_connection *connection)
664 struct gb_protocol *protocol = connection->protocol;
666 gb_protocol_put(protocol);
668 connection->protocol = NULL;