4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/workqueue.h>
15 static void gb_connection_kref_release(struct kref *kref);
18 static DEFINE_SPINLOCK(gb_connections_lock);
19 static DEFINE_MUTEX(gb_connection_mutex);
22 /* Caller holds gb_connection_mutex. */
23 static struct gb_connection *
24 gb_connection_intf_find(struct gb_interface *intf, u16 cport_id)
26 struct gb_host_device *hd = intf->hd;
27 struct gb_connection *connection;
29 list_for_each_entry(connection, &hd->connections, hd_links) {
30 if (connection->intf == intf &&
31 connection->intf_cport_id == cport_id)
38 static void gb_connection_get(struct gb_connection *connection)
40 kref_get(&connection->kref);
43 static void gb_connection_put(struct gb_connection *connection)
45 kref_put(&connection->kref, gb_connection_kref_release);
49 * Returns a reference-counted pointer to the connection if found.
51 static struct gb_connection *
52 gb_connection_hd_find(struct gb_host_device *hd, u16 cport_id)
54 struct gb_connection *connection;
57 spin_lock_irqsave(&gb_connections_lock, flags);
58 list_for_each_entry(connection, &hd->connections, hd_links)
59 if (connection->hd_cport_id == cport_id) {
60 gb_connection_get(connection);
65 spin_unlock_irqrestore(&gb_connections_lock, flags);
71 * Callback from the host driver to let us know that data has been
72 * received on the bundle.
74 void greybus_data_rcvd(struct gb_host_device *hd, u16 cport_id,
75 u8 *data, size_t length)
77 struct gb_connection *connection;
79 connection = gb_connection_hd_find(hd, cport_id);
82 "nonexistent connection (%zu bytes dropped)\n", length);
85 gb_connection_recv(connection, data, length);
86 gb_connection_put(connection);
88 EXPORT_SYMBOL_GPL(greybus_data_rcvd);
90 static void gb_connection_kref_release(struct kref *kref)
92 struct gb_connection *connection;
94 connection = container_of(kref, struct gb_connection, kref);
99 static void gb_connection_init_name(struct gb_connection *connection)
101 u16 hd_cport_id = connection->hd_cport_id;
105 if (connection->intf) {
106 intf_id = connection->intf->interface_id;
107 cport_id = connection->intf_cport_id;
110 snprintf(connection->name, sizeof(connection->name),
111 "%u/%u:%u", hd_cport_id, intf_id, cport_id);
115 * _gb_connection_create() - create a Greybus connection
116 * @hd: host device of the connection
117 * @hd_cport_id: host-device cport id, or -1 for dynamic allocation
118 * @intf: remote interface, or NULL for static connections
119 * @bundle: remote-interface bundle (may be NULL)
120 * @cport_id: remote-interface cport id, or 0 for static connections
121 * @handler: request handler (may be NULL)
123 * Create a Greybus connection, representing the bidirectional link
124 * between a CPort on a (local) Greybus host device and a CPort on
125 * another Greybus interface.
127 * A connection also maintains the state of operations sent over the
130 * Serialised against concurrent create and destroy using the
131 * gb_connection_mutex.
133 * Return: A pointer to the new connection if successful, or an ERR_PTR
136 static struct gb_connection *
137 _gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
138 struct gb_interface *intf,
139 struct gb_bundle *bundle, int cport_id,
140 gb_request_handler_t handler)
142 struct gb_connection *connection;
143 struct ida *id_map = &hd->cport_id_map;
144 int ida_start, ida_end;
147 if (hd_cport_id < 0) {
149 ida_end = hd->num_cports;
150 } else if (hd_cport_id < hd->num_cports) {
151 ida_start = hd_cport_id;
152 ida_end = hd_cport_id + 1;
154 dev_err(&hd->dev, "cport %d not available\n", hd_cport_id);
155 return ERR_PTR(-EINVAL);
158 mutex_lock(&gb_connection_mutex);
160 if (intf && gb_connection_intf_find(intf, cport_id)) {
161 dev_err(&intf->dev, "cport %u already in use\n", cport_id);
166 ret = ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
171 connection = kzalloc(sizeof(*connection), GFP_KERNEL);
177 connection->hd_cport_id = hd_cport_id;
178 connection->intf_cport_id = cport_id;
180 connection->intf = intf;
181 connection->bundle = bundle;
182 connection->handler = handler;
183 connection->state = GB_CONNECTION_STATE_DISABLED;
185 atomic_set(&connection->op_cycle, 0);
186 mutex_init(&connection->mutex);
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);
192 if (!connection->wq) {
194 goto err_free_connection;
197 kref_init(&connection->kref);
199 gb_connection_init_name(connection);
201 spin_lock_irq(&gb_connections_lock);
202 list_add(&connection->hd_links, &hd->connections);
205 list_add(&connection->bundle_links, &bundle->connections);
207 INIT_LIST_HEAD(&connection->bundle_links);
209 spin_unlock_irq(&gb_connections_lock);
211 mutex_unlock(&gb_connection_mutex);
218 ida_simple_remove(id_map, hd_cport_id);
220 mutex_unlock(&gb_connection_mutex);
225 struct gb_connection *
226 gb_connection_create_static(struct gb_host_device *hd, u16 hd_cport_id,
227 gb_request_handler_t handler)
229 return _gb_connection_create(hd, hd_cport_id, NULL, NULL, 0, handler);
232 struct gb_connection *
233 gb_connection_create_control(struct gb_interface *intf)
235 return _gb_connection_create(intf->hd, -1, intf, NULL, 0, NULL);
238 struct gb_connection *
239 gb_connection_create(struct gb_bundle *bundle, u16 cport_id,
240 gb_request_handler_t handler)
242 struct gb_interface *intf = bundle->intf;
244 return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
247 EXPORT_SYMBOL_GPL(gb_connection_create);
249 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
251 struct gb_host_device *hd = connection->hd;
254 if (!hd->driver->cport_enable)
257 ret = hd->driver->cport_enable(hd, connection->hd_cport_id);
260 "failed to enable host cport: %d\n", ret);
267 static void gb_connection_hd_cport_disable(struct gb_connection *connection)
269 struct gb_host_device *hd = connection->hd;
271 if (!hd->driver->cport_disable)
274 hd->driver->cport_disable(hd, connection->hd_cport_id);
277 static int gb_connection_hd_fct_flow_enable(struct gb_connection *connection)
279 struct gb_host_device *hd = connection->hd;
282 if (!hd->driver->fct_flow_enable)
285 ret = hd->driver->fct_flow_enable(hd, connection->hd_cport_id);
287 dev_err(&hd->dev, "%s: failed to enable FCT flow: %d\n",
288 connection->name, ret);
295 static void gb_connection_hd_fct_flow_disable(struct gb_connection *connection)
297 struct gb_host_device *hd = connection->hd;
299 if (!hd->driver->fct_flow_disable)
302 hd->driver->fct_flow_disable(hd, connection->hd_cport_id);
306 * Request the SVC to create a connection from AP's cport to interface's
310 gb_connection_svc_connection_create(struct gb_connection *connection)
312 struct gb_host_device *hd = connection->hd;
313 struct gb_interface *intf;
316 if (gb_connection_is_static(connection))
319 intf = connection->intf;
320 ret = gb_svc_connection_create(hd->svc,
322 connection->hd_cport_id,
324 connection->intf_cport_id,
325 intf->boot_over_unipro);
327 dev_err(&connection->hd->dev,
328 "%s: failed to create svc connection: %d\n",
329 connection->name, ret);
337 gb_connection_svc_connection_destroy(struct gb_connection *connection)
339 if (gb_connection_is_static(connection))
342 gb_svc_connection_destroy(connection->hd->svc,
343 connection->hd->svc->ap_intf_id,
344 connection->hd_cport_id,
345 connection->intf->interface_id,
346 connection->intf_cport_id);
349 /* Inform Interface about active CPorts */
350 static int gb_connection_control_connected(struct gb_connection *connection)
352 struct gb_control *control;
353 u16 cport_id = connection->intf_cport_id;
356 if (gb_connection_is_static(connection))
359 control = connection->intf->control;
361 if (connection == control->connection)
364 ret = gb_control_connected_operation(control, cport_id);
366 dev_err(&connection->bundle->dev,
367 "failed to connect cport: %d\n", ret);
374 /* Inform Interface about inactive CPorts */
376 gb_connection_control_disconnected(struct gb_connection *connection)
378 struct gb_control *control;
379 u16 cport_id = connection->intf_cport_id;
382 if (gb_connection_is_static(connection))
385 control = connection->intf->control;
387 if (connection == control->connection)
390 ret = gb_control_disconnected_operation(control, cport_id);
392 dev_warn(&connection->bundle->dev,
393 "failed to disconnect cport: %d\n", ret);
398 * Cancel all active operations on a connection.
400 * Locking: Called with connection lock held and state set to DISABLED.
402 static void gb_connection_cancel_operations(struct gb_connection *connection,
404 __must_hold(&connection->lock)
406 struct gb_operation *operation;
408 while (!list_empty(&connection->operations)) {
409 operation = list_last_entry(&connection->operations,
410 struct gb_operation, links);
411 gb_operation_get(operation);
412 spin_unlock_irq(&connection->lock);
414 if (gb_operation_is_incoming(operation))
415 gb_operation_cancel_incoming(operation, errno);
417 gb_operation_cancel(operation, errno);
419 gb_operation_put(operation);
421 spin_lock_irq(&connection->lock);
426 * Cancel all active incoming operations on a connection.
428 * Locking: Called with connection lock held and state set to ENABLED_TX.
431 gb_connection_flush_incoming_operations(struct gb_connection *connection,
433 __must_hold(&connection->lock)
435 struct gb_operation *operation;
438 while (!list_empty(&connection->operations)) {
440 list_for_each_entry(operation, &connection->operations,
442 if (gb_operation_is_incoming(operation)) {
443 gb_operation_get(operation);
452 spin_unlock_irq(&connection->lock);
454 /* FIXME: flush, not cancel? */
455 gb_operation_cancel_incoming(operation, errno);
456 gb_operation_put(operation);
458 spin_lock_irq(&connection->lock);
463 * _gb_connection_enable() - enable a connection
464 * @connection: connection to enable
465 * @rx: whether to enable incoming requests
467 * Connection-enable helper for DISABLED->ENABLED, DISABLED->ENABLED_TX, and
468 * ENABLED_TX->ENABLED state transitions.
470 * Locking: Caller holds connection->mutex.
472 static int _gb_connection_enable(struct gb_connection *connection, bool rx)
476 /* Handle ENABLED_TX -> ENABLED transitions. */
477 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
478 if (!(connection->handler && rx))
481 spin_lock_irq(&connection->lock);
482 connection->state = GB_CONNECTION_STATE_ENABLED;
483 spin_unlock_irq(&connection->lock);
488 ret = gb_connection_hd_cport_enable(connection);
492 ret = gb_connection_svc_connection_create(connection);
494 goto err_hd_cport_disable;
496 spin_lock_irq(&connection->lock);
497 if (connection->handler && rx)
498 connection->state = GB_CONNECTION_STATE_ENABLED;
500 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
501 spin_unlock_irq(&connection->lock);
503 ret = gb_connection_control_connected(connection);
505 goto err_svc_destroy;
510 spin_lock_irq(&connection->lock);
511 connection->state = GB_CONNECTION_STATE_DISABLED;
512 gb_connection_cancel_operations(connection, -ESHUTDOWN);
513 spin_unlock_irq(&connection->lock);
515 gb_connection_svc_connection_destroy(connection);
516 err_hd_cport_disable:
517 gb_connection_hd_cport_disable(connection);
522 int gb_connection_enable(struct gb_connection *connection)
526 mutex_lock(&connection->mutex);
528 if (connection->state == GB_CONNECTION_STATE_ENABLED)
531 ret = _gb_connection_enable(connection, true);
533 mutex_unlock(&connection->mutex);
537 EXPORT_SYMBOL_GPL(gb_connection_enable);
539 int gb_connection_enable_tx(struct gb_connection *connection)
543 mutex_lock(&connection->mutex);
545 if (connection->state == GB_CONNECTION_STATE_ENABLED) {
550 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX)
553 ret = _gb_connection_enable(connection, false);
555 mutex_unlock(&connection->mutex);
559 EXPORT_SYMBOL_GPL(gb_connection_enable_tx);
561 void gb_connection_disable_rx(struct gb_connection *connection)
563 mutex_lock(&connection->mutex);
565 spin_lock_irq(&connection->lock);
566 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
567 spin_unlock_irq(&connection->lock);
570 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
571 gb_connection_flush_incoming_operations(connection, -ESHUTDOWN);
572 spin_unlock_irq(&connection->lock);
575 mutex_unlock(&connection->mutex);
578 void gb_connection_disable(struct gb_connection *connection)
580 mutex_lock(&connection->mutex);
582 if (connection->state == GB_CONNECTION_STATE_DISABLED)
585 gb_connection_control_disconnected(connection);
587 spin_lock_irq(&connection->lock);
588 connection->state = GB_CONNECTION_STATE_DISABLED;
589 gb_connection_cancel_operations(connection, -ESHUTDOWN);
590 spin_unlock_irq(&connection->lock);
592 gb_connection_svc_connection_destroy(connection);
593 gb_connection_hd_cport_disable(connection);
596 mutex_unlock(&connection->mutex);
598 EXPORT_SYMBOL_GPL(gb_connection_disable);
600 /* Caller must have disabled the connection before destroying it. */
601 void gb_connection_destroy(struct gb_connection *connection)
608 mutex_lock(&gb_connection_mutex);
610 spin_lock_irq(&gb_connections_lock);
611 list_del(&connection->bundle_links);
612 list_del(&connection->hd_links);
613 spin_unlock_irq(&gb_connections_lock);
615 destroy_workqueue(connection->wq);
617 id_map = &connection->hd->cport_id_map;
618 ida_simple_remove(id_map, connection->hd_cport_id);
619 connection->hd_cport_id = CPORT_ID_BAD;
621 mutex_unlock(&gb_connection_mutex);
623 gb_connection_put(connection);
625 EXPORT_SYMBOL_GPL(gb_connection_destroy);
627 void gb_connection_latency_tag_enable(struct gb_connection *connection)
629 struct gb_host_device *hd = connection->hd;
632 if (!hd->driver->latency_tag_enable)
635 ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
637 dev_err(&connection->hd->dev,
638 "%s: failed to enable latency tag: %d\n",
639 connection->name, ret);
642 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
644 void gb_connection_latency_tag_disable(struct gb_connection *connection)
646 struct gb_host_device *hd = connection->hd;
649 if (!hd->driver->latency_tag_disable)
652 ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
654 dev_err(&connection->hd->dev,
655 "%s: failed to disable latency tag: %d\n",
656 connection->name, ret);
659 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);