greybus: connection: rename an error label
[cascardo/linux.git] / drivers / staging / greybus / connection.c
1 /*
2  * Greybus connections
3  *
4  * Copyright 2014 Google Inc.
5  * Copyright 2014 Linaro Ltd.
6  *
7  * Released under the GPLv2 only.
8  */
9
10 #include <linux/workqueue.h>
11
12 #include "greybus.h"
13
14
15 static void gb_connection_kref_release(struct kref *kref);
16
17
18 static DEFINE_SPINLOCK(gb_connections_lock);
19 static DEFINE_MUTEX(gb_connection_mutex);
20
21
22 /* Caller holds gb_connection_mutex. */
23 static struct gb_connection *
24 gb_connection_intf_find(struct gb_interface *intf, u16 cport_id)
25 {
26         struct gb_host_device *hd = intf->hd;
27         struct gb_connection *connection;
28
29         list_for_each_entry(connection, &hd->connections, hd_links) {
30                 if (connection->intf == intf &&
31                                 connection->intf_cport_id == cport_id)
32                         return connection;
33         }
34
35         return NULL;
36 }
37
38 static void gb_connection_get(struct gb_connection *connection)
39 {
40         kref_get(&connection->kref);
41 }
42
43 static void gb_connection_put(struct gb_connection *connection)
44 {
45         kref_put(&connection->kref, gb_connection_kref_release);
46 }
47
48 /*
49  * Returns a reference-counted pointer to the connection if found.
50  */
51 static struct gb_connection *
52 gb_connection_hd_find(struct gb_host_device *hd, u16 cport_id)
53 {
54         struct gb_connection *connection;
55         unsigned long flags;
56
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);
61                         goto found;
62                 }
63         connection = NULL;
64 found:
65         spin_unlock_irqrestore(&gb_connections_lock, flags);
66
67         return connection;
68 }
69
70 /*
71  * Callback from the host driver to let us know that data has been
72  * received on the bundle.
73  */
74 void greybus_data_rcvd(struct gb_host_device *hd, u16 cport_id,
75                         u8 *data, size_t length)
76 {
77         struct gb_connection *connection;
78
79         connection = gb_connection_hd_find(hd, cport_id);
80         if (!connection) {
81                 dev_err(&hd->dev,
82                         "nonexistent connection (%zu bytes dropped)\n", length);
83                 return;
84         }
85         gb_connection_recv(connection, data, length);
86         gb_connection_put(connection);
87 }
88 EXPORT_SYMBOL_GPL(greybus_data_rcvd);
89
90 static void gb_connection_kref_release(struct kref *kref)
91 {
92         struct gb_connection *connection;
93
94         connection = container_of(kref, struct gb_connection, kref);
95
96         kfree(connection);
97 }
98
99 static void gb_connection_init_name(struct gb_connection *connection)
100 {
101         u16 hd_cport_id = connection->hd_cport_id;
102         u16 cport_id = 0;
103         u8 intf_id = 0;
104
105         if (connection->intf) {
106                 intf_id = connection->intf->interface_id;
107                 cport_id = connection->intf_cport_id;
108         }
109
110         snprintf(connection->name, sizeof(connection->name),
111                         "%u/%u:%u", hd_cport_id, intf_id, cport_id);
112 }
113
114 /*
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)
122  * @flags:              connection flags
123  *
124  * Create a Greybus connection, representing the bidirectional link
125  * between a CPort on a (local) Greybus host device and a CPort on
126  * another Greybus interface.
127  *
128  * A connection also maintains the state of operations sent over the
129  * connection.
130  *
131  * Serialised against concurrent create and destroy using the
132  * gb_connection_mutex.
133  *
134  * Return: A pointer to the new connection if successful, or an ERR_PTR
135  * otherwise.
136  */
137 static struct gb_connection *
138 _gb_connection_create(struct gb_host_device *hd, int hd_cport_id,
139                                 struct gb_interface *intf,
140                                 struct gb_bundle *bundle, int cport_id,
141                                 gb_request_handler_t handler,
142                                 unsigned long flags)
143 {
144         struct gb_connection *connection;
145         int ret;
146
147         mutex_lock(&gb_connection_mutex);
148
149         if (intf && gb_connection_intf_find(intf, cport_id)) {
150                 dev_err(&intf->dev, "cport %u already in use\n", cport_id);
151                 ret = -EBUSY;
152                 goto err_unlock;
153         }
154
155         ret = gb_hd_cport_allocate(hd, hd_cport_id, flags);
156         if (ret < 0) {
157                 dev_err(&hd->dev, "failed to allocate cport: %d\n", ret);
158                 goto err_unlock;
159         }
160         hd_cport_id = ret;
161
162         connection = kzalloc(sizeof(*connection), GFP_KERNEL);
163         if (!connection) {
164                 ret = -ENOMEM;
165                 goto err_hd_cport_release;
166         }
167
168         connection->hd_cport_id = hd_cport_id;
169         connection->intf_cport_id = cport_id;
170         connection->hd = hd;
171         connection->intf = intf;
172         connection->bundle = bundle;
173         connection->handler = handler;
174         connection->flags = flags;
175         if (intf && (intf->quirks & GB_INTERFACE_QUIRK_NO_CPORT_FEATURES))
176                 connection->flags |= GB_CONNECTION_FLAG_NO_FLOWCTRL;
177         connection->state = GB_CONNECTION_STATE_DISABLED;
178
179         atomic_set(&connection->op_cycle, 0);
180         mutex_init(&connection->mutex);
181         spin_lock_init(&connection->lock);
182         INIT_LIST_HEAD(&connection->operations);
183
184         connection->wq = alloc_workqueue("%s:%d", WQ_UNBOUND, 1,
185                                          dev_name(&hd->dev), hd_cport_id);
186         if (!connection->wq) {
187                 ret = -ENOMEM;
188                 goto err_free_connection;
189         }
190
191         kref_init(&connection->kref);
192
193         gb_connection_init_name(connection);
194
195         spin_lock_irq(&gb_connections_lock);
196         list_add(&connection->hd_links, &hd->connections);
197
198         if (bundle)
199                 list_add(&connection->bundle_links, &bundle->connections);
200         else
201                 INIT_LIST_HEAD(&connection->bundle_links);
202
203         spin_unlock_irq(&gb_connections_lock);
204
205         mutex_unlock(&gb_connection_mutex);
206
207         return connection;
208
209 err_free_connection:
210         kfree(connection);
211 err_hd_cport_release:
212         gb_hd_cport_release(hd, hd_cport_id);
213 err_unlock:
214         mutex_unlock(&gb_connection_mutex);
215
216         return ERR_PTR(ret);
217 }
218
219 struct gb_connection *
220 gb_connection_create_static(struct gb_host_device *hd, u16 hd_cport_id,
221                                         gb_request_handler_t handler)
222 {
223         return _gb_connection_create(hd, hd_cport_id, NULL, NULL, 0, handler,
224                                         0);
225 }
226
227 struct gb_connection *
228 gb_connection_create_control(struct gb_interface *intf)
229 {
230         return _gb_connection_create(intf->hd, -1, intf, NULL, 0, NULL,
231                                         GB_CONNECTION_FLAG_CONTROL);
232 }
233
234 struct gb_connection *
235 gb_connection_create(struct gb_bundle *bundle, u16 cport_id,
236                                         gb_request_handler_t handler)
237 {
238         struct gb_interface *intf = bundle->intf;
239
240         return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
241                                         handler, 0);
242 }
243 EXPORT_SYMBOL_GPL(gb_connection_create);
244
245 struct gb_connection *
246 gb_connection_create_flags(struct gb_bundle *bundle, u16 cport_id,
247                                         gb_request_handler_t handler,
248                                         unsigned long flags)
249 {
250         struct gb_interface *intf = bundle->intf;
251
252         return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
253                                         handler, flags);
254 }
255 EXPORT_SYMBOL_GPL(gb_connection_create_flags);
256
257 struct gb_connection *
258 gb_connection_create_offloaded(struct gb_bundle *bundle, u16 cport_id,
259                                         unsigned long flags)
260 {
261         struct gb_interface *intf = bundle->intf;
262
263         flags |= GB_CONNECTION_FLAG_OFFLOADED;
264
265         return _gb_connection_create(intf->hd, -1, intf, bundle, cport_id,
266                                         NULL, flags);
267 }
268 EXPORT_SYMBOL_GPL(gb_connection_create_offloaded);
269
270 static int gb_connection_hd_cport_enable(struct gb_connection *connection)
271 {
272         struct gb_host_device *hd = connection->hd;
273         int ret;
274
275         if (!hd->driver->cport_enable)
276                 return 0;
277
278         ret = hd->driver->cport_enable(hd, connection->hd_cport_id);
279         if (ret) {
280                 dev_err(&hd->dev,
281                                 "%s: failed to enable host cport: %d\n",
282                                 connection->name, ret);
283                 return ret;
284         }
285
286         return 0;
287 }
288
289 static void gb_connection_hd_cport_disable(struct gb_connection *connection)
290 {
291         struct gb_host_device *hd = connection->hd;
292         int ret;
293
294         if (!hd->driver->cport_disable)
295                 return;
296
297         ret = hd->driver->cport_disable(hd, connection->hd_cport_id);
298         if (ret) {
299                 dev_err(&hd->dev,
300                                 "%s: failed to disable host cport: %d\n",
301                                 connection->name, ret);
302         }
303 }
304
305 static int
306 gb_connection_hd_cport_features_enable(struct gb_connection *connection)
307 {
308         struct gb_host_device *hd = connection->hd;
309         int ret;
310
311         if (!hd->driver->cport_features_enable)
312                 return 0;
313
314         ret = hd->driver->cport_features_enable(hd, connection->hd_cport_id);
315         if (ret) {
316                 dev_err(&hd->dev, "%s: failed to enable CPort features: %d\n",
317                         connection->name, ret);
318                 return ret;
319         }
320
321         return 0;
322 }
323
324 static void
325 gb_connection_hd_cport_features_disable(struct gb_connection *connection)
326 {
327         struct gb_host_device *hd = connection->hd;
328
329         if (!hd->driver->cport_features_disable)
330                 return;
331
332         hd->driver->cport_features_disable(hd, connection->hd_cport_id);
333 }
334
335 /*
336  * Request the SVC to create a connection from AP's cport to interface's
337  * cport.
338  */
339 static int
340 gb_connection_svc_connection_create(struct gb_connection *connection)
341 {
342         struct gb_host_device *hd = connection->hd;
343         struct gb_interface *intf;
344         u8 cport_flags;
345         int ret;
346
347         if (gb_connection_is_static(connection))
348                 return gb_connection_hd_cport_features_enable(connection);
349
350         intf = connection->intf;
351
352         /*
353          * Enable either E2EFC or CSD, unless no flow control is requested.
354          */
355         cport_flags = GB_SVC_CPORT_FLAG_CSV_N;
356         if (gb_connection_flow_control_disabled(connection)) {
357                 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N;
358         } else if (gb_connection_e2efc_enabled(connection)) {
359                 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N |
360                                 GB_SVC_CPORT_FLAG_E2EFC;
361         }
362
363         ret = gb_svc_connection_create(hd->svc,
364                         hd->svc->ap_intf_id,
365                         connection->hd_cport_id,
366                         intf->interface_id,
367                         connection->intf_cport_id,
368                         cport_flags);
369         if (ret) {
370                 dev_err(&connection->hd->dev,
371                         "%s: failed to create svc connection: %d\n",
372                         connection->name, ret);
373                 return ret;
374         }
375
376         ret = gb_connection_hd_cport_features_enable(connection);
377         if (ret) {
378                 gb_svc_connection_destroy(hd->svc, hd->svc->ap_intf_id,
379                                           connection->hd_cport_id,
380                                           intf->interface_id,
381                                           connection->intf_cport_id);
382                 return ret;
383         }
384
385         return 0;
386 }
387
388 static void
389 gb_connection_svc_connection_destroy(struct gb_connection *connection)
390 {
391         gb_connection_hd_cport_features_disable(connection);
392
393         if (gb_connection_is_static(connection))
394                 return;
395
396         gb_svc_connection_destroy(connection->hd->svc,
397                                   connection->hd->svc->ap_intf_id,
398                                   connection->hd_cport_id,
399                                   connection->intf->interface_id,
400                                   connection->intf_cport_id);
401 }
402
403 /* Inform Interface about active CPorts */
404 static int gb_connection_control_connected(struct gb_connection *connection)
405 {
406         struct gb_control *control;
407         u16 cport_id = connection->intf_cport_id;
408         int ret;
409
410         if (gb_connection_is_static(connection))
411                 return 0;
412
413         /*
414          * HACK: Suppress connected request for the offloaded camera
415          * connection as it is currently not supported by firmware. Note that
416          * the corresponding non-fatal disconnected event is still sent.
417          */
418         if (gb_connection_is_offloaded(connection) &&
419                         connection->flags & GB_CONNECTION_FLAG_CDSI1) {
420                 return 0;
421         }
422
423         if (gb_connection_is_control(connection))
424                 return 0;
425
426         control = connection->intf->control;
427
428         ret = gb_control_connected_operation(control, cport_id);
429         if (ret) {
430                 dev_err(&connection->bundle->dev,
431                         "failed to connect cport: %d\n", ret);
432                 return ret;
433         }
434
435         return 0;
436 }
437
438 /* Inform Interface about inactive CPorts */
439 static void
440 gb_connection_control_disconnected(struct gb_connection *connection)
441 {
442         struct gb_control *control;
443         u16 cport_id = connection->intf_cport_id;
444         int ret;
445
446         if (gb_connection_is_static(connection))
447                 return;
448
449         if (gb_connection_is_control(connection))
450                 return;
451
452         control = connection->intf->control;
453
454         ret = gb_control_disconnected_operation(control, cport_id);
455         if (ret) {
456                 dev_warn(&connection->bundle->dev,
457                          "failed to disconnect cport: %d\n", ret);
458         }
459 }
460
461 /*
462  * Cancel all active operations on a connection.
463  *
464  * Locking: Called with connection lock held and state set to DISABLED.
465  */
466 static void gb_connection_cancel_operations(struct gb_connection *connection,
467                                                 int errno)
468         __must_hold(&connection->lock)
469 {
470         struct gb_operation *operation;
471
472         while (!list_empty(&connection->operations)) {
473                 operation = list_last_entry(&connection->operations,
474                                                 struct gb_operation, links);
475                 gb_operation_get(operation);
476                 spin_unlock_irq(&connection->lock);
477
478                 if (gb_operation_is_incoming(operation))
479                         gb_operation_cancel_incoming(operation, errno);
480                 else
481                         gb_operation_cancel(operation, errno);
482
483                 gb_operation_put(operation);
484
485                 spin_lock_irq(&connection->lock);
486         }
487 }
488
489 /*
490  * Cancel all active incoming operations on a connection.
491  *
492  * Locking: Called with connection lock held and state set to ENABLED_TX.
493  */
494 static void
495 gb_connection_flush_incoming_operations(struct gb_connection *connection,
496                                                 int errno)
497         __must_hold(&connection->lock)
498 {
499         struct gb_operation *operation;
500         bool incoming;
501
502         while (!list_empty(&connection->operations)) {
503                 incoming = false;
504                 list_for_each_entry(operation, &connection->operations,
505                                                                 links) {
506                         if (gb_operation_is_incoming(operation)) {
507                                 gb_operation_get(operation);
508                                 incoming = true;
509                                 break;
510                         }
511                 }
512
513                 if (!incoming)
514                         break;
515
516                 spin_unlock_irq(&connection->lock);
517
518                 /* FIXME: flush, not cancel? */
519                 gb_operation_cancel_incoming(operation, errno);
520                 gb_operation_put(operation);
521
522                 spin_lock_irq(&connection->lock);
523         }
524 }
525
526 /*
527  * _gb_connection_enable() - enable a connection
528  * @connection:         connection to enable
529  * @rx:                 whether to enable incoming requests
530  *
531  * Connection-enable helper for DISABLED->ENABLED, DISABLED->ENABLED_TX, and
532  * ENABLED_TX->ENABLED state transitions.
533  *
534  * Locking: Caller holds connection->mutex.
535  */
536 static int _gb_connection_enable(struct gb_connection *connection, bool rx)
537 {
538         int ret;
539
540         /* Handle ENABLED_TX -> ENABLED transitions. */
541         if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
542                 if (!(connection->handler && rx))
543                         return 0;
544
545                 spin_lock_irq(&connection->lock);
546                 connection->state = GB_CONNECTION_STATE_ENABLED;
547                 spin_unlock_irq(&connection->lock);
548
549                 return 0;
550         }
551
552         ret = gb_connection_hd_cport_enable(connection);
553         if (ret)
554                 return ret;
555
556         ret = gb_connection_svc_connection_create(connection);
557         if (ret)
558                 goto err_hd_cport_disable;
559
560         spin_lock_irq(&connection->lock);
561         if (connection->handler && rx)
562                 connection->state = GB_CONNECTION_STATE_ENABLED;
563         else
564                 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
565         spin_unlock_irq(&connection->lock);
566
567         ret = gb_connection_control_connected(connection);
568         if (ret)
569                 goto err_flush_operations;
570
571         return 0;
572
573 err_flush_operations:
574         spin_lock_irq(&connection->lock);
575         connection->state = GB_CONNECTION_STATE_DISABLED;
576         gb_connection_cancel_operations(connection, -ESHUTDOWN);
577         spin_unlock_irq(&connection->lock);
578
579         gb_connection_svc_connection_destroy(connection);
580 err_hd_cport_disable:
581         gb_connection_hd_cport_disable(connection);
582
583         return ret;
584 }
585
586 int gb_connection_enable(struct gb_connection *connection)
587 {
588         int ret = 0;
589
590         mutex_lock(&connection->mutex);
591
592         if (connection->state == GB_CONNECTION_STATE_ENABLED)
593                 goto out_unlock;
594
595         ret = _gb_connection_enable(connection, true);
596 out_unlock:
597         mutex_unlock(&connection->mutex);
598
599         return ret;
600 }
601 EXPORT_SYMBOL_GPL(gb_connection_enable);
602
603 int gb_connection_enable_tx(struct gb_connection *connection)
604 {
605         int ret = 0;
606
607         mutex_lock(&connection->mutex);
608
609         if (connection->state == GB_CONNECTION_STATE_ENABLED) {
610                 ret = -EINVAL;
611                 goto out_unlock;
612         }
613
614         if (connection->state == GB_CONNECTION_STATE_ENABLED_TX)
615                 goto out_unlock;
616
617         ret = _gb_connection_enable(connection, false);
618 out_unlock:
619         mutex_unlock(&connection->mutex);
620
621         return ret;
622 }
623 EXPORT_SYMBOL_GPL(gb_connection_enable_tx);
624
625 void gb_connection_disable_rx(struct gb_connection *connection)
626 {
627         mutex_lock(&connection->mutex);
628
629         spin_lock_irq(&connection->lock);
630         if (connection->state != GB_CONNECTION_STATE_ENABLED) {
631                 spin_unlock_irq(&connection->lock);
632                 goto out_unlock;
633         }
634         connection->state = GB_CONNECTION_STATE_ENABLED_TX;
635         gb_connection_flush_incoming_operations(connection, -ESHUTDOWN);
636         spin_unlock_irq(&connection->lock);
637
638 out_unlock:
639         mutex_unlock(&connection->mutex);
640 }
641 EXPORT_SYMBOL_GPL(gb_connection_disable_rx);
642
643 void gb_connection_disable(struct gb_connection *connection)
644 {
645         mutex_lock(&connection->mutex);
646
647         if (connection->state == GB_CONNECTION_STATE_DISABLED)
648                 goto out_unlock;
649
650         gb_connection_control_disconnected(connection);
651
652         spin_lock_irq(&connection->lock);
653         connection->state = GB_CONNECTION_STATE_DISABLED;
654         gb_connection_cancel_operations(connection, -ESHUTDOWN);
655         spin_unlock_irq(&connection->lock);
656
657         gb_connection_svc_connection_destroy(connection);
658         gb_connection_hd_cport_disable(connection);
659
660 out_unlock:
661         mutex_unlock(&connection->mutex);
662 }
663 EXPORT_SYMBOL_GPL(gb_connection_disable);
664
665 /* Disable a connection without communicating with the remote end. */
666 void gb_connection_disable_forced(struct gb_connection *connection)
667 {
668         mutex_lock(&connection->mutex);
669
670         if (connection->state == GB_CONNECTION_STATE_DISABLED)
671                 goto out_unlock;
672
673         spin_lock_irq(&connection->lock);
674         connection->state = GB_CONNECTION_STATE_DISABLED;
675         gb_connection_cancel_operations(connection, -ESHUTDOWN);
676         spin_unlock_irq(&connection->lock);
677
678         gb_connection_svc_connection_destroy(connection);
679         gb_connection_hd_cport_disable(connection);
680
681 out_unlock:
682         mutex_unlock(&connection->mutex);
683 }
684 EXPORT_SYMBOL_GPL(gb_connection_disable_forced);
685
686 /* Caller must have disabled the connection before destroying it. */
687 void gb_connection_destroy(struct gb_connection *connection)
688 {
689         if (!connection)
690                 return;
691
692         if (WARN_ON(connection->state != GB_CONNECTION_STATE_DISABLED))
693                 gb_connection_disable(connection);
694
695         mutex_lock(&gb_connection_mutex);
696
697         spin_lock_irq(&gb_connections_lock);
698         list_del(&connection->bundle_links);
699         list_del(&connection->hd_links);
700         spin_unlock_irq(&gb_connections_lock);
701
702         destroy_workqueue(connection->wq);
703
704         gb_hd_cport_release(connection->hd, connection->hd_cport_id);
705         connection->hd_cport_id = CPORT_ID_BAD;
706
707         mutex_unlock(&gb_connection_mutex);
708
709         gb_connection_put(connection);
710 }
711 EXPORT_SYMBOL_GPL(gb_connection_destroy);
712
713 void gb_connection_latency_tag_enable(struct gb_connection *connection)
714 {
715         struct gb_host_device *hd = connection->hd;
716         int ret;
717
718         if (!hd->driver->latency_tag_enable)
719                 return;
720
721         ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
722         if (ret) {
723                 dev_err(&connection->hd->dev,
724                         "%s: failed to enable latency tag: %d\n",
725                         connection->name, ret);
726         }
727 }
728 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
729
730 void gb_connection_latency_tag_disable(struct gb_connection *connection)
731 {
732         struct gb_host_device *hd = connection->hd;
733         int ret;
734
735         if (!hd->driver->latency_tag_disable)
736                 return;
737
738         ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
739         if (ret) {
740                 dev_err(&connection->hd->dev,
741                         "%s: failed to disable latency tag: %d\n",
742                         connection->name, ret);
743         }
744 }
745 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);