4 * Copyright 2014 Google Inc.
5 * Copyright 2014 Linaro Ltd.
7 * Released under the GPLv2 only.
10 #include <linux/workqueue.h>
14 #define GB_CONNECTION_TS_KFIFO_ELEMENTS 2
15 #define GB_CONNECTION_TS_KFIFO_LEN \
16 (GB_CONNECTION_TS_KFIFO_ELEMENTS * sizeof(struct timeval))
18 static DEFINE_SPINLOCK(gb_connections_lock);
20 /* This is only used at initialization time; no locking is required. */
21 static struct gb_connection *
22 gb_connection_intf_find(struct gb_interface *intf, u16 cport_id)
24 struct greybus_host_device *hd = intf->hd;
25 struct gb_connection *connection;
27 list_for_each_entry(connection, &hd->connections, hd_links)
28 if (connection->bundle->intf == intf &&
29 connection->intf_cport_id == cport_id)
34 static struct gb_connection *
35 gb_connection_hd_find(struct greybus_host_device *hd, u16 cport_id)
37 struct gb_connection *connection;
40 spin_lock_irqsave(&gb_connections_lock, flags);
41 list_for_each_entry(connection, &hd->connections, hd_links)
42 if (connection->hd_cport_id == cport_id)
46 spin_unlock_irqrestore(&gb_connections_lock, flags);
52 * Callback from the host driver to let us know that data has been
53 * received on the bundle.
55 void greybus_data_rcvd(struct greybus_host_device *hd, u16 cport_id,
56 u8 *data, size_t length)
58 struct gb_connection *connection;
60 connection = gb_connection_hd_find(hd, cport_id);
63 "nonexistent connection (%zu bytes dropped)\n", length);
66 gb_connection_recv(connection, data, length);
68 EXPORT_SYMBOL_GPL(greybus_data_rcvd);
70 void gb_connection_push_timestamp(struct gb_connection *connection)
75 kfifo_in_locked(&connection->ts_kfifo, (void *)&tv,
76 sizeof(struct timeval), &connection->lock);
78 EXPORT_SYMBOL_GPL(gb_connection_push_timestamp);
80 int gb_connection_pop_timestamp(struct gb_connection *connection,
85 if (!kfifo_len(&connection->ts_kfifo))
87 retval = kfifo_out_locked(&connection->ts_kfifo, (void *)tv,
88 sizeof(*tv), &connection->lock);
91 EXPORT_SYMBOL_GPL(gb_connection_pop_timestamp);
93 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
96 struct gb_connection *connection = to_gb_connection(dev);
97 enum gb_connection_state state;
99 spin_lock_irq(&connection->lock);
100 state = connection->state;
101 spin_unlock_irq(&connection->lock);
103 return sprintf(buf, "%d\n", state);
105 static DEVICE_ATTR_RO(state);
108 protocol_id_show(struct device *dev, struct device_attribute *attr, char *buf)
110 struct gb_connection *connection = to_gb_connection(dev);
112 if (connection->protocol)
113 return sprintf(buf, "%d\n", connection->protocol->id);
117 static DEVICE_ATTR_RO(protocol_id);
119 static struct attribute *connection_attrs[] = {
120 &dev_attr_state.attr,
121 &dev_attr_protocol_id.attr,
125 ATTRIBUTE_GROUPS(connection);
127 static void gb_connection_release(struct device *dev)
129 struct gb_connection *connection = to_gb_connection(dev);
131 destroy_workqueue(connection->wq);
132 kfifo_free(&connection->ts_kfifo);
136 struct device_type greybus_connection_type = {
137 .name = "greybus_connection",
138 .release = gb_connection_release,
142 int svc_update_connection(struct gb_interface *intf,
143 struct gb_connection *connection)
145 struct gb_bundle *bundle;
147 bundle = gb_bundle_create(intf, GB_SVC_BUNDLE_ID, GREYBUS_CLASS_SVC);
151 device_del(&connection->dev);
152 connection->bundle = bundle;
153 connection->dev.parent = &bundle->dev;
154 dev_set_name(&connection->dev, "%s:%d", dev_name(&bundle->dev),
157 WARN_ON(device_add(&connection->dev));
159 spin_lock_irq(&gb_connections_lock);
160 list_add(&connection->bundle_links, &bundle->connections);
161 spin_unlock_irq(&gb_connections_lock);
167 * Set up a Greybus connection, representing the bidirectional link
168 * between a CPort on a (local) Greybus host device and a CPort on
169 * another Greybus module.
171 * A connection also maintains the state of operations sent over the
174 * Returns a pointer to the new connection if successful, or a null
177 struct gb_connection *
178 gb_connection_create_range(struct greybus_host_device *hd,
179 struct gb_bundle *bundle, struct device *parent,
180 u16 cport_id, u8 protocol_id, u32 ida_start,
183 struct gb_connection *connection;
184 struct ida *id_map = &hd->cport_id_map;
191 * If a manifest tries to reuse a cport, reject it. We
192 * initialize connections serially so we don't need to worry
193 * about holding the connection lock.
195 if (bundle && gb_connection_intf_find(bundle->intf, cport_id)) {
196 pr_err("duplicate interface cport id 0x%04hx\n", cport_id);
200 hd_cport_id = ida_simple_get(id_map, ida_start, ida_end, GFP_KERNEL);
204 connection = kzalloc(sizeof(*connection), GFP_KERNEL);
208 connection->hd_cport_id = hd_cport_id;
209 connection->intf_cport_id = cport_id;
212 connection->protocol_id = protocol_id;
213 connection->major = major;
214 connection->minor = minor;
216 connection->bundle = bundle;
217 connection->state = GB_CONNECTION_STATE_DISABLED;
219 atomic_set(&connection->op_cycle, 0);
220 spin_lock_init(&connection->lock);
221 INIT_LIST_HEAD(&connection->operations);
223 connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
224 dev_name(parent), cport_id);
226 goto err_free_connection;
228 if (kfifo_alloc(&connection->ts_kfifo, GB_CONNECTION_TS_KFIFO_LEN,
232 connection->dev.parent = parent;
233 connection->dev.bus = &greybus_bus_type;
234 connection->dev.type = &greybus_connection_type;
235 connection->dev.groups = connection_groups;
236 device_initialize(&connection->dev);
237 dev_set_name(&connection->dev, "%s:%d",
238 dev_name(parent), cport_id);
240 retval = device_add(&connection->dev);
242 connection->hd_cport_id = CPORT_ID_BAD;
243 put_device(&connection->dev);
245 pr_err("failed to add connection device for cport 0x%04hx\n",
251 spin_lock_irq(&gb_connections_lock);
252 list_add(&connection->hd_links, &hd->connections);
255 list_add(&connection->bundle_links, &bundle->connections);
257 INIT_LIST_HEAD(&connection->bundle_links);
259 spin_unlock_irq(&gb_connections_lock);
261 if (hd_cport_id != GB_SVC_CPORT_ID) {
262 gb_svc_connection_create(hd->svc,
263 hd->endo->ap_intf_id, hd_cport_id,
264 bundle->intf->interface_id, cport_id);
267 gb_connection_bind_protocol(connection);
268 if (!connection->protocol)
269 dev_warn(&connection->dev,
270 "protocol 0x%02hhx handler not found\n", protocol_id);
275 destroy_workqueue(connection->wq);
279 ida_simple_remove(id_map, hd_cport_id);
284 struct gb_connection *gb_connection_create(struct gb_bundle *bundle,
285 u16 cport_id, u8 protocol_id)
287 return gb_connection_create_range(bundle->intf->hd, bundle,
288 &bundle->dev, cport_id, protocol_id,
289 0, bundle->intf->hd->num_cports - 1);
293 * Cancel all active operations on a connection.
295 * Should only be called during connection tear down.
297 static void gb_connection_cancel_operations(struct gb_connection *connection,
300 struct gb_operation *operation;
302 spin_lock_irq(&connection->lock);
303 while (!list_empty(&connection->operations)) {
304 operation = list_last_entry(&connection->operations,
305 struct gb_operation, links);
306 gb_operation_get(operation);
307 spin_unlock_irq(&connection->lock);
309 if (gb_operation_is_incoming(operation))
310 gb_operation_cancel_incoming(operation, errno);
312 gb_operation_cancel(operation, errno);
314 gb_operation_put(operation);
316 spin_lock_irq(&connection->lock);
318 spin_unlock_irq(&connection->lock);
322 * Tear down a previously set up connection.
324 void gb_connection_destroy(struct gb_connection *connection)
328 if (WARN_ON(!connection))
331 spin_lock_irq(&gb_connections_lock);
332 list_del(&connection->bundle_links);
333 list_del(&connection->hd_links);
334 spin_unlock_irq(&gb_connections_lock);
336 if (connection->protocol)
337 gb_protocol_put(connection->protocol);
338 connection->protocol = NULL;
340 id_map = &connection->hd->cport_id_map;
341 ida_simple_remove(id_map, connection->hd_cport_id);
342 connection->hd_cport_id = CPORT_ID_BAD;
344 device_unregister(&connection->dev);
347 static void gb_connection_disconnected(struct gb_connection *connection)
349 struct gb_control *control;
350 int cport_id = connection->intf_cport_id;
354 * Inform Interface about In-active CPorts. We don't need to do this
355 * operation for control cport.
357 if ((cport_id == GB_CONTROL_CPORT_ID) ||
358 (connection->hd_cport_id == GB_SVC_CPORT_ID))
361 control = connection->bundle->intf->control;
363 ret = gb_control_disconnected_operation(control, cport_id);
365 dev_warn(&connection->dev,
366 "Failed to disconnect CPort-%d (%d)\n", cport_id, ret);
369 static int gb_connection_init(struct gb_connection *connection)
371 int cport_id = connection->intf_cport_id;
374 if (!connection->protocol) {
375 dev_warn(&connection->dev, "init without protocol.\n");
380 * Inform Interface about Active CPorts. We don't need to do this
381 * operation for control cport.
383 if (cport_id != GB_CONTROL_CPORT_ID &&
384 connection->hd_cport_id != GB_SVC_CPORT_ID) {
385 struct gb_control *control = connection->bundle->intf->control;
387 ret = gb_control_connected_operation(control, cport_id);
389 dev_err(&connection->dev,
390 "Failed to connect CPort-%d (%d)\n",
396 /* Need to enable the connection to initialize it */
397 spin_lock_irq(&connection->lock);
398 connection->state = GB_CONNECTION_STATE_ENABLED;
399 spin_unlock_irq(&connection->lock);
402 * Request protocol version supported by the module. We don't need to do
403 * this for SVC as that is initiated by the SVC.
405 if (connection->hd_cport_id != GB_SVC_CPORT_ID) {
406 ret = gb_protocol_get_version(connection);
408 dev_err(&connection->dev,
409 "Failed to get version CPort-%d (%d)\n",
415 ret = connection->protocol->connection_init(connection);
420 spin_lock_irq(&connection->lock);
421 connection->state = GB_CONNECTION_STATE_ERROR;
422 spin_unlock_irq(&connection->lock);
424 gb_connection_disconnected(connection);
428 void gb_connection_exit(struct gb_connection *connection)
430 if (!connection->protocol) {
431 dev_warn(&connection->dev, "exit without protocol.\n");
435 spin_lock_irq(&connection->lock);
436 if (connection->state != GB_CONNECTION_STATE_ENABLED) {
437 spin_unlock_irq(&connection->lock);
440 connection->state = GB_CONNECTION_STATE_DESTROYING;
441 spin_unlock_irq(&connection->lock);
443 gb_connection_cancel_operations(connection, -ESHUTDOWN);
445 connection->protocol->connection_exit(connection);
446 gb_connection_disconnected(connection);
449 void gb_hd_connections_exit(struct greybus_host_device *hd)
451 struct gb_connection *connection;
453 list_for_each_entry(connection, &hd->connections, hd_links) {
454 gb_connection_exit(connection);
455 gb_connection_destroy(connection);
459 void gb_connection_bind_protocol(struct gb_connection *connection)
461 struct gb_protocol *protocol;
463 /* If we already have a protocol bound here, just return */
464 if (connection->protocol)
467 protocol = gb_protocol_get(connection->protocol_id,
472 connection->protocol = protocol;
475 * If we have a valid device_id for the interface block, then we have an
476 * active device, so bring up the connection at the same time.
478 if ((!connection->bundle &&
479 connection->hd_cport_id == GB_SVC_CPORT_ID) ||
480 connection->bundle->intf->device_id != GB_DEVICE_ID_BAD)
481 gb_connection_init(connection);