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
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 * Serialised against concurrent create and destroy using the
130 * gb_connection_mutex.
132 * Return: A pointer to the new connection if successful, or NULL otherwise.
134 static struct gb_connection *
135 gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
136 struct gb_interface *intf,
137 struct gb_bundle *bundle, int cport_id)
139 struct gb_connection *connection;
140 struct ida *id_map = &hd->cport_id_map;
141 int ida_start, ida_end;
143 if (hd_cport_id < 0) {
145 ida_end = hd->num_cports;
146 } else if (hd_cport_id < hd->num_cports) {
147 ida_start = hd_cport_id;
148 ida_end = hd_cport_id + 1;
150 dev_err(&hd->dev, "cport %d not available\n", hd_cport_id);
154 mutex_lock(&gb_connection_mutex);
156 if (intf && gb_connection_intf_find(intf, cport_id)) {
157 dev_err(&intf->dev, "cport %u already in use\n", cport_id);
161 hd_cport_id = ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
165 connection = kzalloc(sizeof(*connection), GFP_KERNEL);
169 connection->hd_cport_id = hd_cport_id;
170 connection->intf_cport_id = cport_id;
172 connection->intf = intf;
174 connection->bundle = bundle;
175 connection->state = GB_CONNECTION_STATE_DISABLED;
177 atomic_set(&connection->op_cycle, 0);
178 mutex_init(&connection->mutex);
179 spin_lock_init(&connection->lock);
180 INIT_LIST_HEAD(&connection->operations);
182 connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
183 dev_name(&hd->dev), hd_cport_id);
185 goto err_free_connection;
187 kref_init(&connection->kref);
189 gb_connection_init_name(connection);
191 spin_lock_irq(&gb_connections_lock);
192 list_add(&connection->hd_links, &hd->connections);
195 list_add(&connection->bundle_links, &bundle->connections);
197 INIT_LIST_HEAD(&connection->bundle_links);
199 spin_unlock_irq(&gb_connections_lock);
201 mutex_unlock(&gb_connection_mutex);
208 ida_simple_remove(id_map, hd_cport_id);
210 mutex_unlock(&gb_connection_mutex);
215 struct gb_connection *
216 gb_connection_create_static(struct gb_host_device *hd, u16 hd_cport_id)
218 return gb_connection_create(hd, hd_cport_id, NULL, NULL, 0);
221 struct gb_connection *
222 gb_connection_create_control(struct gb_interface *intf)
224 return gb_connection_create(intf->hd, -1, intf, NULL, 0);
227 struct gb_connection *
228 gb_connection_create_dynamic(struct gb_interface *intf,
229 struct gb_bundle *bundle,
232 return gb_connection_create(intf->hd, -1, intf, bundle, cport_id);
234 EXPORT_SYMBOL_GPL(gb_connection_create_dynamic);
236 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
238 struct gb_host_device *hd = connection->hd;
241 if (!hd->driver->cport_enable)
244 ret = hd->driver->cport_enable(hd, connection->hd_cport_id);
247 "failed to enable host cport: %d\n", ret);
254 static void gb_connection_hd_cport_disable(struct gb_connection *connection)
256 struct gb_host_device *hd = connection->hd;
258 if (!hd->driver->cport_disable)
261 hd->driver->cport_disable(hd, connection->hd_cport_id);
265 * Request the SVC to create a connection from AP's cport to interface's
269 gb_connection_svc_connection_create(struct gb_connection *connection)
271 struct gb_host_device *hd = connection->hd;
272 struct gb_interface *intf;
275 if (gb_connection_is_static(connection))
278 intf = connection->intf;
279 ret = gb_svc_connection_create(hd->svc,
281 connection->hd_cport_id,
283 connection->intf_cport_id,
284 intf->boot_over_unipro);
286 dev_err(&connection->hd->dev,
287 "%s: failed to create svc connection: %d\n",
288 connection->name, ret);
296 gb_connection_svc_connection_destroy(struct gb_connection *connection)
298 if (gb_connection_is_static(connection))
301 gb_svc_connection_destroy(connection->hd->svc,
302 connection->hd->svc->ap_intf_id,
303 connection->hd_cport_id,
304 connection->intf->interface_id,
305 connection->intf_cport_id);
308 /* Inform Interface about active CPorts */
309 static int gb_connection_control_connected(struct gb_connection *connection)
311 struct gb_control *control;
312 u16 cport_id = connection->intf_cport_id;
315 if (gb_connection_is_static(connection))
318 control = connection->intf->control;
320 if (connection == control->connection)
323 ret = gb_control_connected_operation(control, cport_id);
325 dev_err(&connection->bundle->dev,
326 "failed to connect cport: %d\n", ret);
333 /* Inform Interface about inactive CPorts */
335 gb_connection_control_disconnected(struct gb_connection *connection)
337 struct gb_control *control;
338 u16 cport_id = connection->intf_cport_id;
341 if (gb_connection_is_static(connection))
344 control = connection->intf->control;
346 if (connection == control->connection)
349 ret = gb_control_disconnected_operation(control, cport_id);
351 dev_warn(&connection->bundle->dev,
352 "failed to disconnect cport: %d\n", ret);
357 * Cancel all active operations on a connection.
359 * Locking: Called with connection lock held and state set to DISABLED.
361 static void gb_connection_cancel_operations(struct gb_connection *connection,
364 struct gb_operation *operation;
366 while (!list_empty(&connection->operations)) {
367 operation = list_last_entry(&connection->operations,
368 struct gb_operation, links);
369 gb_operation_get(operation);
370 spin_unlock_irq(&connection->lock);
372 if (gb_operation_is_incoming(operation))
373 gb_operation_cancel_incoming(operation, errno);
375 gb_operation_cancel(operation, errno);
377 gb_operation_put(operation);
379 spin_lock_irq(&connection->lock);
384 * Cancel all active incoming operations on a connection.
386 * Locking: Called with connection lock held and state set to ENABLED_TX.
389 gb_connection_flush_incoming_operations(struct gb_connection *connection,
392 struct gb_operation *operation;
395 while (!list_empty(&connection->operations)) {
397 list_for_each_entry(operation, &connection->operations,
399 if (gb_operation_is_incoming(operation)) {
400 gb_operation_get(operation);
409 spin_unlock_irq(&connection->lock);
411 /* FIXME: flush, not cancel? */
412 gb_operation_cancel_incoming(operation, errno);
413 gb_operation_put(operation);
415 spin_lock_irq(&connection->lock);
419 int gb_connection_enable(struct gb_connection *connection,
420 gb_request_handler_t handler)
424 mutex_lock(&connection->mutex);
426 if (connection->state == GB_CONNECTION_STATE_ENABLED)
429 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
433 spin_lock_irq(&connection->lock);
434 connection->handler = handler;
435 connection->state = GB_CONNECTION_STATE_ENABLED;
436 spin_unlock_irq(&connection->lock);
441 ret = gb_connection_hd_cport_enable(connection);
445 ret = gb_connection_svc_connection_create(connection);
447 goto err_hd_cport_disable;
449 spin_lock_irq(&connection->lock);
450 connection->handler = handler;
452 connection->state = GB_CONNECTION_STATE_ENABLED;
454 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
455 spin_unlock_irq(&connection->lock);
457 ret = gb_connection_control_connected(connection);
459 goto err_svc_destroy;
462 mutex_unlock(&connection->mutex);
467 spin_lock_irq(&connection->lock);
468 connection->state = GB_CONNECTION_STATE_DISABLED;
469 gb_connection_cancel_operations(connection, -ESHUTDOWN);
470 connection->handler = NULL;
471 spin_unlock_irq(&connection->lock);
473 gb_connection_svc_connection_destroy(connection);
474 err_hd_cport_disable:
475 gb_connection_hd_cport_disable(connection);
477 mutex_unlock(&connection->mutex);
481 EXPORT_SYMBOL_GPL(gb_connection_enable);
483 void gb_connection_disable_rx(struct gb_connection *connection)
485 mutex_lock(&connection->mutex);
487 spin_lock_irq(&connection->lock);
488 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
489 spin_unlock_irq(&connection->lock);
492 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
493 gb_connection_flush_incoming_operations(connection, -ESHUTDOWN);
494 connection->handler = NULL;
495 spin_unlock_irq(&connection->lock);
498 mutex_unlock(&connection->mutex);
501 void gb_connection_disable(struct gb_connection *connection)
503 mutex_lock(&connection->mutex);
505 if (connection->state == GB_CONNECTION_STATE_DISABLED)
508 gb_connection_control_disconnected(connection);
510 spin_lock_irq(&connection->lock);
511 connection->state = GB_CONNECTION_STATE_DISABLED;
512 gb_connection_cancel_operations(connection, -ESHUTDOWN);
513 connection->handler = NULL;
514 spin_unlock_irq(&connection->lock);
516 gb_connection_svc_connection_destroy(connection);
517 gb_connection_hd_cport_disable(connection);
520 mutex_unlock(&connection->mutex);
522 EXPORT_SYMBOL_GPL(gb_connection_disable);
524 /* Caller must have disabled the connection before destroying it. */
525 void gb_connection_destroy(struct gb_connection *connection)
532 mutex_lock(&gb_connection_mutex);
534 spin_lock_irq(&gb_connections_lock);
535 list_del(&connection->bundle_links);
536 list_del(&connection->hd_links);
537 spin_unlock_irq(&gb_connections_lock);
539 destroy_workqueue(connection->wq);
541 id_map = &connection->hd->cport_id_map;
542 ida_simple_remove(id_map, connection->hd_cport_id);
543 connection->hd_cport_id = CPORT_ID_BAD;
545 mutex_unlock(&gb_connection_mutex);
547 gb_connection_put(connection);
549 EXPORT_SYMBOL_GPL(gb_connection_destroy);
551 void gb_connection_latency_tag_enable(struct gb_connection *connection)
553 struct gb_host_device *hd = connection->hd;
556 if (!hd->driver->latency_tag_enable)
559 ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
561 dev_err(&connection->hd->dev,
562 "%s: failed to enable latency tag: %d\n",
563 connection->name, ret);
566 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
568 void gb_connection_latency_tag_disable(struct gb_connection *connection)
570 struct gb_host_device *hd = connection->hd;
573 if (!hd->driver->latency_tag_disable)
576 ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
578 dev_err(&connection->hd->dev,
579 "%s: failed to disable latency tag: %d\n",
580 connection->name, ret);
583 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);