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 * @protocol_id: protocol id
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 NULL otherwise.
135 static struct gb_connection *
136 gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
137 struct gb_interface *intf,
138 struct gb_bundle *bundle, int cport_id,
141 struct gb_connection *connection;
142 struct ida *id_map = &hd->cport_id_map;
143 int ida_start, ida_end;
145 if (hd_cport_id < 0) {
147 ida_end = hd->num_cports;
148 } else if (hd_cport_id < hd->num_cports) {
149 ida_start = hd_cport_id;
150 ida_end = hd_cport_id + 1;
152 dev_err(&hd->dev, "cport %d not available\n", hd_cport_id);
156 mutex_lock(&gb_connection_mutex);
158 if (intf && gb_connection_intf_find(intf, cport_id)) {
159 dev_err(&intf->dev, "cport %u already in use\n", cport_id);
163 hd_cport_id = ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
167 connection = kzalloc(sizeof(*connection), GFP_KERNEL);
171 connection->hd_cport_id = hd_cport_id;
172 connection->intf_cport_id = cport_id;
174 connection->intf = intf;
176 connection->protocol_id = protocol_id;
178 connection->bundle = bundle;
179 connection->state = GB_CONNECTION_STATE_DISABLED;
181 atomic_set(&connection->op_cycle, 0);
182 mutex_init(&connection->mutex);
183 spin_lock_init(&connection->lock);
184 INIT_LIST_HEAD(&connection->operations);
186 connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
187 dev_name(&hd->dev), hd_cport_id);
189 goto err_free_connection;
191 kref_init(&connection->kref);
193 gb_connection_init_name(connection);
195 spin_lock_irq(&gb_connections_lock);
196 list_add(&connection->hd_links, &hd->connections);
199 list_add(&connection->bundle_links, &bundle->connections);
201 INIT_LIST_HEAD(&connection->bundle_links);
203 spin_unlock_irq(&gb_connections_lock);
205 mutex_unlock(&gb_connection_mutex);
212 ida_simple_remove(id_map, hd_cport_id);
214 mutex_unlock(&gb_connection_mutex);
219 struct gb_connection *
220 gb_connection_create_static(struct gb_host_device *hd,
221 u16 hd_cport_id, u8 protocol_id)
223 return gb_connection_create(hd, hd_cport_id, NULL, NULL, 0,
227 struct gb_connection *
228 gb_connection_create_dynamic(struct gb_interface *intf,
229 struct gb_bundle *bundle,
230 u16 cport_id, u8 protocol_id)
232 return gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
235 EXPORT_SYMBOL_GPL(gb_connection_create_dynamic);
237 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
239 struct gb_host_device *hd = connection->hd;
242 if (!hd->driver->cport_enable)
245 ret = hd->driver->cport_enable(hd, connection->hd_cport_id);
248 "failed to enable host cport: %d\n", ret);
255 static void gb_connection_hd_cport_disable(struct gb_connection *connection)
257 struct gb_host_device *hd = connection->hd;
259 if (!hd->driver->cport_disable)
262 hd->driver->cport_disable(hd, connection->hd_cport_id);
266 * Request the SVC to create a connection from AP's cport to interface's
270 gb_connection_svc_connection_create(struct gb_connection *connection)
272 struct gb_host_device *hd = connection->hd;
273 struct gb_interface *intf;
276 if (gb_connection_is_static(connection))
279 intf = connection->intf;
280 ret = gb_svc_connection_create(hd->svc,
282 connection->hd_cport_id,
284 connection->intf_cport_id,
285 intf->boot_over_unipro);
287 dev_err(&connection->hd->dev,
288 "%s: failed to create svc connection: %d\n",
289 connection->name, ret);
297 gb_connection_svc_connection_destroy(struct gb_connection *connection)
299 if (gb_connection_is_static(connection))
302 gb_svc_connection_destroy(connection->hd->svc,
303 connection->hd->svc->ap_intf_id,
304 connection->hd_cport_id,
305 connection->intf->interface_id,
306 connection->intf_cport_id);
309 /* Inform Interface about active CPorts */
310 static int gb_connection_control_connected(struct gb_connection *connection)
312 struct gb_control *control;
313 u16 cport_id = connection->intf_cport_id;
316 if (gb_connection_is_static(connection))
319 control = connection->intf->control;
321 if (connection == control->connection)
324 ret = gb_control_connected_operation(control, cport_id);
326 dev_err(&connection->bundle->dev,
327 "failed to connect cport: %d\n", ret);
334 /* Inform Interface about inactive CPorts */
336 gb_connection_control_disconnected(struct gb_connection *connection)
338 struct gb_control *control;
339 u16 cport_id = connection->intf_cport_id;
342 if (gb_connection_is_static(connection))
345 control = connection->intf->control;
347 if (connection == control->connection)
350 ret = gb_control_disconnected_operation(control, cport_id);
352 dev_warn(&connection->bundle->dev,
353 "failed to disconnect cport: %d\n", ret);
358 * Cancel all active operations on a connection.
360 * Locking: Called with connection lock held and state set to DISABLED.
362 static void gb_connection_cancel_operations(struct gb_connection *connection,
365 struct gb_operation *operation;
367 while (!list_empty(&connection->operations)) {
368 operation = list_last_entry(&connection->operations,
369 struct gb_operation, links);
370 gb_operation_get(operation);
371 spin_unlock_irq(&connection->lock);
373 if (gb_operation_is_incoming(operation))
374 gb_operation_cancel_incoming(operation, errno);
376 gb_operation_cancel(operation, errno);
378 gb_operation_put(operation);
380 spin_lock_irq(&connection->lock);
385 * Cancel all active incoming operations on a connection.
387 * Locking: Called with connection lock held and state set to ENABLED_TX.
390 gb_connection_flush_incoming_operations(struct gb_connection *connection,
393 struct gb_operation *operation;
396 while (!list_empty(&connection->operations)) {
398 list_for_each_entry(operation, &connection->operations,
400 if (gb_operation_is_incoming(operation)) {
401 gb_operation_get(operation);
410 spin_unlock_irq(&connection->lock);
412 /* FIXME: flush, not cancel? */
413 gb_operation_cancel_incoming(operation, errno);
414 gb_operation_put(operation);
416 spin_lock_irq(&connection->lock);
420 int gb_connection_enable(struct gb_connection *connection,
421 gb_request_handler_t handler)
425 mutex_lock(&connection->mutex);
427 if (connection->state == GB_CONNECTION_STATE_ENABLED)
430 if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
434 spin_lock_irq(&connection->lock);
435 connection->handler = handler;
436 connection->state = GB_CONNECTION_STATE_ENABLED;
437 spin_unlock_irq(&connection->lock);
442 ret = gb_connection_hd_cport_enable(connection);
446 ret = gb_connection_svc_connection_create(connection);
448 goto err_hd_cport_disable;
450 spin_lock_irq(&connection->lock);
451 connection->handler = handler;
453 connection->state = GB_CONNECTION_STATE_ENABLED;
455 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
456 spin_unlock_irq(&connection->lock);
458 ret = gb_connection_control_connected(connection);
460 goto err_svc_destroy;
463 mutex_unlock(&connection->mutex);
468 spin_lock_irq(&connection->lock);
469 connection->state = GB_CONNECTION_STATE_DISABLED;
470 gb_connection_cancel_operations(connection, -ESHUTDOWN);
471 connection->handler = NULL;
472 spin_unlock_irq(&connection->lock);
474 gb_connection_svc_connection_destroy(connection);
475 err_hd_cport_disable:
476 gb_connection_hd_cport_disable(connection);
478 mutex_unlock(&connection->mutex);
482 EXPORT_SYMBOL_GPL(gb_connection_enable);
484 void gb_connection_disable_rx(struct gb_connection *connection)
486 mutex_lock(&connection->mutex);
488 spin_lock_irq(&connection->lock);
489 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
490 spin_unlock_irq(&connection->lock);
493 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
494 gb_connection_flush_incoming_operations(connection, -ESHUTDOWN);
495 connection->handler = NULL;
496 spin_unlock_irq(&connection->lock);
499 mutex_unlock(&connection->mutex);
502 void gb_connection_disable(struct gb_connection *connection)
504 mutex_lock(&connection->mutex);
506 if (connection->state == GB_CONNECTION_STATE_DISABLED)
509 gb_connection_control_disconnected(connection);
511 spin_lock_irq(&connection->lock);
512 connection->state = GB_CONNECTION_STATE_DISABLED;
513 gb_connection_cancel_operations(connection, -ESHUTDOWN);
514 connection->handler = NULL;
515 spin_unlock_irq(&connection->lock);
517 gb_connection_svc_connection_destroy(connection);
518 gb_connection_hd_cport_disable(connection);
521 mutex_unlock(&connection->mutex);
523 EXPORT_SYMBOL_GPL(gb_connection_disable);
525 /* Caller must have disabled the connection before destroying it. */
526 void gb_connection_destroy(struct gb_connection *connection)
533 mutex_lock(&gb_connection_mutex);
535 spin_lock_irq(&gb_connections_lock);
536 list_del(&connection->bundle_links);
537 list_del(&connection->hd_links);
538 spin_unlock_irq(&gb_connections_lock);
540 destroy_workqueue(connection->wq);
542 id_map = &connection->hd->cport_id_map;
543 ida_simple_remove(id_map, connection->hd_cport_id);
544 connection->hd_cport_id = CPORT_ID_BAD;
546 mutex_unlock(&gb_connection_mutex);
548 gb_connection_put(connection);
550 EXPORT_SYMBOL_GPL(gb_connection_destroy);
552 void gb_connection_latency_tag_enable(struct gb_connection *connection)
554 struct gb_host_device *hd = connection->hd;
557 if (!hd->driver->latency_tag_enable)
560 ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
562 dev_err(&connection->hd->dev,
563 "%s: failed to enable latency tag: %d\n",
564 connection->name, ret);
567 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
569 void gb_connection_latency_tag_disable(struct gb_connection *connection)
571 struct gb_host_device *hd = connection->hd;
574 if (!hd->driver->latency_tag_disable)
577 ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
579 dev_err(&connection->hd->dev,
580 "%s: failed to disable latency tag: %d\n",
581 connection->name, ret);
584 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);