c1cdfcd830a48be1d3cdbb5e8ae5f592bc33c594
[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 gb_connection_hd_cport_flush(struct gb_connection *connection)
306 {
307         struct gb_host_device *hd = connection->hd;
308         int ret;
309
310         if (!hd->driver->cport_flush)
311                 return 0;
312
313         ret = hd->driver->cport_flush(hd, connection->hd_cport_id);
314         if (ret) {
315                 dev_err(&hd->dev, "%s: failed to flush host cport: %d\n",
316                                 connection->name, ret);
317                 return ret;
318         }
319
320         return 0;
321 }
322
323 static int
324 gb_connection_hd_cport_features_enable(struct gb_connection *connection)
325 {
326         struct gb_host_device *hd = connection->hd;
327         int ret;
328
329         if (!hd->driver->cport_features_enable)
330                 return 0;
331
332         ret = hd->driver->cport_features_enable(hd, connection->hd_cport_id);
333         if (ret) {
334                 dev_err(&hd->dev, "%s: failed to enable CPort features: %d\n",
335                         connection->name, ret);
336                 return ret;
337         }
338
339         return 0;
340 }
341
342 static void
343 gb_connection_hd_cport_features_disable(struct gb_connection *connection)
344 {
345         struct gb_host_device *hd = connection->hd;
346
347         if (!hd->driver->cport_features_disable)
348                 return;
349
350         hd->driver->cport_features_disable(hd, connection->hd_cport_id);
351 }
352
353 /*
354  * Request the SVC to create a connection from AP's cport to interface's
355  * cport.
356  */
357 static int
358 gb_connection_svc_connection_create(struct gb_connection *connection)
359 {
360         struct gb_host_device *hd = connection->hd;
361         struct gb_interface *intf;
362         u8 cport_flags;
363         int ret;
364
365         if (gb_connection_is_static(connection))
366                 return 0;
367
368         intf = connection->intf;
369
370         /*
371          * Enable either E2EFC or CSD, unless no flow control is requested.
372          */
373         cport_flags = GB_SVC_CPORT_FLAG_CSV_N;
374         if (gb_connection_flow_control_disabled(connection)) {
375                 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N;
376         } else if (gb_connection_e2efc_enabled(connection)) {
377                 cport_flags |= GB_SVC_CPORT_FLAG_CSD_N |
378                                 GB_SVC_CPORT_FLAG_E2EFC;
379         }
380
381         ret = gb_svc_connection_create(hd->svc,
382                         hd->svc->ap_intf_id,
383                         connection->hd_cport_id,
384                         intf->interface_id,
385                         connection->intf_cport_id,
386                         cport_flags);
387         if (ret) {
388                 dev_err(&connection->hd->dev,
389                         "%s: failed to create svc connection: %d\n",
390                         connection->name, ret);
391                 return ret;
392         }
393
394         return 0;
395 }
396
397 static void
398 gb_connection_svc_connection_destroy(struct gb_connection *connection)
399 {
400         if (gb_connection_is_static(connection))
401                 return;
402
403         gb_svc_connection_destroy(connection->hd->svc,
404                                   connection->hd->svc->ap_intf_id,
405                                   connection->hd_cport_id,
406                                   connection->intf->interface_id,
407                                   connection->intf_cport_id);
408 }
409
410 static void
411 gb_connection_svc_connection_quiescing(struct gb_connection *connection)
412 {
413         struct gb_host_device *hd = connection->hd;
414
415         if (gb_connection_is_static(connection))
416                 return;
417
418         gb_svc_connection_quiescing(hd->svc,
419                                         hd->svc->ap_intf_id,
420                                         connection->hd_cport_id,
421                                         connection->intf->interface_id,
422                                         connection->intf_cport_id);
423 }
424
425 /* Inform Interface about active CPorts */
426 static int gb_connection_control_connected(struct gb_connection *connection)
427 {
428         struct gb_control *control;
429         u16 cport_id = connection->intf_cport_id;
430         int ret;
431
432         if (gb_connection_is_static(connection))
433                 return 0;
434
435         /*
436          * HACK: Suppress connected request for the offloaded camera
437          * connection as it is currently not supported by firmware. Note that
438          * the corresponding non-fatal disconnected event is still sent.
439          */
440         if (gb_connection_is_offloaded(connection) &&
441                         connection->flags & GB_CONNECTION_FLAG_CDSI1) {
442                 return 0;
443         }
444
445         if (gb_connection_is_control(connection))
446                 return 0;
447
448         control = connection->intf->control;
449
450         ret = gb_control_connected_operation(control, cport_id);
451         if (ret) {
452                 dev_err(&connection->bundle->dev,
453                         "failed to connect cport: %d\n", ret);
454                 return ret;
455         }
456
457         return 0;
458 }
459
460 static void
461 gb_connection_control_disconnecting(struct gb_connection *connection)
462 {
463         struct gb_control *control;
464         u16 cport_id = connection->intf_cport_id;
465         int ret;
466
467         if (gb_connection_is_static(connection))
468                 return;
469
470         control = connection->intf->control;
471
472         ret = gb_control_disconnecting_operation(control, cport_id);
473         if (ret) {
474                 dev_err(&connection->hd->dev,
475                                 "%s: failed to send disconnecting: %d\n",
476                                 connection->name, ret);
477         }
478 }
479
480 static void
481 gb_connection_control_disconnected(struct gb_connection *connection)
482 {
483         struct gb_control *control;
484         u16 cport_id = connection->intf_cport_id;
485         int ret;
486
487         if (gb_connection_is_static(connection))
488                 return;
489
490         if (gb_connection_is_control(connection))
491                 return;
492
493         control = connection->intf->control;
494
495         ret = gb_control_disconnected_operation(control, cport_id);
496         if (ret) {
497                 dev_warn(&connection->bundle->dev,
498                          "failed to disconnect cport: %d\n", ret);
499         }
500 }
501
502 static int gb_connection_ping_operation(struct gb_connection *connection)
503 {
504         struct gb_operation *operation;
505         int ret;
506
507         operation = gb_operation_create_core(connection,
508                                                 GB_REQUEST_TYPE_PING,
509                                                 0, 0, 0,
510                                                 GFP_KERNEL);
511         if (!operation)
512                 return -ENOMEM;
513
514         ret = gb_operation_request_send_sync(operation);
515
516         gb_operation_put(operation);
517
518         return ret;
519 }
520
521 static int gb_connection_ping(struct gb_connection *connection)
522 {
523         struct gb_host_device *hd = connection->hd;
524         int ret;
525
526         if (gb_connection_is_static(connection))
527                 return 0;
528
529         if (gb_connection_is_offloaded(connection)) {
530                 if (!hd->driver->cport_ping)
531                         return 0;
532
533                 ret = hd->driver->cport_ping(hd, connection->intf_cport_id);
534         } else {
535                 ret = gb_connection_ping_operation(connection);
536         }
537
538         if (ret) {
539                 dev_err(&hd->dev, "%s: failed to send ping: %d\n",
540                                 connection->name, ret);
541                 return ret;
542         }
543
544         return 0;
545 }
546
547 /*
548  * Cancel all active operations on a connection.
549  *
550  * Locking: Called with connection lock held and state set to DISABLED or
551  * DISCONNECTING.
552  */
553 static void gb_connection_cancel_operations(struct gb_connection *connection,
554                                                 int errno)
555         __must_hold(&connection->lock)
556 {
557         struct gb_operation *operation;
558
559         while (!list_empty(&connection->operations)) {
560                 operation = list_last_entry(&connection->operations,
561                                                 struct gb_operation, links);
562                 gb_operation_get(operation);
563                 spin_unlock_irq(&connection->lock);
564
565                 if (gb_operation_is_incoming(operation))
566                         gb_operation_cancel_incoming(operation, errno);
567                 else
568                         gb_operation_cancel(operation, errno);
569
570                 gb_operation_put(operation);
571
572                 spin_lock_irq(&connection->lock);
573         }
574 }
575
576 /*
577  * Cancel all active incoming operations on a connection.
578  *
579  * Locking: Called with connection lock held and state set to ENABLED_TX.
580  */
581 static void
582 gb_connection_flush_incoming_operations(struct gb_connection *connection,
583                                                 int errno)
584         __must_hold(&connection->lock)
585 {
586         struct gb_operation *operation;
587         bool incoming;
588
589         while (!list_empty(&connection->operations)) {
590                 incoming = false;
591                 list_for_each_entry(operation, &connection->operations,
592                                                                 links) {
593                         if (gb_operation_is_incoming(operation)) {
594                                 gb_operation_get(operation);
595                                 incoming = true;
596                                 break;
597                         }
598                 }
599
600                 if (!incoming)
601                         break;
602
603                 spin_unlock_irq(&connection->lock);
604
605                 /* FIXME: flush, not cancel? */
606                 gb_operation_cancel_incoming(operation, errno);
607                 gb_operation_put(operation);
608
609                 spin_lock_irq(&connection->lock);
610         }
611 }
612
613 /*
614  * _gb_connection_enable() - enable a connection
615  * @connection:         connection to enable
616  * @rx:                 whether to enable incoming requests
617  *
618  * Connection-enable helper for DISABLED->ENABLED, DISABLED->ENABLED_TX, and
619  * ENABLED_TX->ENABLED state transitions.
620  *
621  * Locking: Caller holds connection->mutex.
622  */
623 static int _gb_connection_enable(struct gb_connection *connection, bool rx)
624 {
625         int ret;
626
627         /* Handle ENABLED_TX -> ENABLED transitions. */
628         if (connection->state == GB_CONNECTION_STATE_ENABLED_TX) {
629                 if (!(connection->handler && rx))
630                         return 0;
631
632                 spin_lock_irq(&connection->lock);
633                 connection->state = GB_CONNECTION_STATE_ENABLED;
634                 spin_unlock_irq(&connection->lock);
635
636                 return 0;
637         }
638
639         ret = gb_connection_hd_cport_enable(connection);
640         if (ret)
641                 return ret;
642
643         ret = gb_connection_svc_connection_create(connection);
644         if (ret)
645                 goto err_hd_cport_disable;
646
647         ret = gb_connection_hd_cport_features_enable(connection);
648         if (ret)
649                 goto err_svc_connection_destroy;
650
651         spin_lock_irq(&connection->lock);
652         if (connection->handler && rx)
653                 connection->state = GB_CONNECTION_STATE_ENABLED;
654         else
655                 connection->state = GB_CONNECTION_STATE_ENABLED_TX;
656         spin_unlock_irq(&connection->lock);
657
658         ret = gb_connection_control_connected(connection);
659         if (ret)
660                 goto err_control_disconnecting;
661
662         return 0;
663
664 err_control_disconnecting:
665         gb_connection_control_disconnecting(connection);
666
667         spin_lock_irq(&connection->lock);
668         connection->state = GB_CONNECTION_STATE_DISCONNECTING;
669         gb_connection_cancel_operations(connection, -ESHUTDOWN);
670         spin_unlock_irq(&connection->lock);
671
672         /* Transmit queue should already be empty. */
673         gb_connection_hd_cport_flush(connection);
674
675         gb_connection_ping(connection);
676         gb_connection_hd_cport_features_disable(connection);
677         gb_connection_svc_connection_quiescing(connection);
678         gb_connection_ping(connection);
679         gb_connection_control_disconnected(connection);
680         connection->state = GB_CONNECTION_STATE_DISABLED;
681 err_svc_connection_destroy:
682         gb_connection_svc_connection_destroy(connection);
683 err_hd_cport_disable:
684         gb_connection_hd_cport_disable(connection);
685
686         return ret;
687 }
688
689 int gb_connection_enable(struct gb_connection *connection)
690 {
691         int ret = 0;
692
693         mutex_lock(&connection->mutex);
694
695         if (connection->state == GB_CONNECTION_STATE_ENABLED)
696                 goto out_unlock;
697
698         ret = _gb_connection_enable(connection, true);
699 out_unlock:
700         mutex_unlock(&connection->mutex);
701
702         return ret;
703 }
704 EXPORT_SYMBOL_GPL(gb_connection_enable);
705
706 int gb_connection_enable_tx(struct gb_connection *connection)
707 {
708         int ret = 0;
709
710         mutex_lock(&connection->mutex);
711
712         if (connection->state == GB_CONNECTION_STATE_ENABLED) {
713                 ret = -EINVAL;
714                 goto out_unlock;
715         }
716
717         if (connection->state == GB_CONNECTION_STATE_ENABLED_TX)
718                 goto out_unlock;
719
720         ret = _gb_connection_enable(connection, false);
721 out_unlock:
722         mutex_unlock(&connection->mutex);
723
724         return ret;
725 }
726 EXPORT_SYMBOL_GPL(gb_connection_enable_tx);
727
728 void gb_connection_disable_rx(struct gb_connection *connection)
729 {
730         mutex_lock(&connection->mutex);
731
732         spin_lock_irq(&connection->lock);
733         if (connection->state != GB_CONNECTION_STATE_ENABLED) {
734                 spin_unlock_irq(&connection->lock);
735                 goto out_unlock;
736         }
737         connection->state = GB_CONNECTION_STATE_ENABLED_TX;
738         gb_connection_flush_incoming_operations(connection, -ESHUTDOWN);
739         spin_unlock_irq(&connection->lock);
740
741 out_unlock:
742         mutex_unlock(&connection->mutex);
743 }
744 EXPORT_SYMBOL_GPL(gb_connection_disable_rx);
745
746 void gb_connection_disable(struct gb_connection *connection)
747 {
748         mutex_lock(&connection->mutex);
749
750         if (connection->state == GB_CONNECTION_STATE_DISABLED)
751                 goto out_unlock;
752
753         gb_connection_control_disconnecting(connection);
754
755         spin_lock_irq(&connection->lock);
756         connection->state = GB_CONNECTION_STATE_DISCONNECTING;
757         gb_connection_cancel_operations(connection, -ESHUTDOWN);
758         spin_unlock_irq(&connection->lock);
759
760         gb_connection_hd_cport_flush(connection);
761
762         gb_connection_ping(connection);
763         gb_connection_hd_cport_features_disable(connection);
764         gb_connection_svc_connection_quiescing(connection);
765         gb_connection_ping(connection);
766
767         gb_connection_control_disconnected(connection);
768
769         connection->state = GB_CONNECTION_STATE_DISABLED;
770
771         gb_connection_svc_connection_destroy(connection);
772         gb_connection_hd_cport_disable(connection);
773
774 out_unlock:
775         mutex_unlock(&connection->mutex);
776 }
777 EXPORT_SYMBOL_GPL(gb_connection_disable);
778
779 /* Disable a connection without communicating with the remote end. */
780 void gb_connection_disable_forced(struct gb_connection *connection)
781 {
782         mutex_lock(&connection->mutex);
783
784         if (connection->state == GB_CONNECTION_STATE_DISABLED)
785                 goto out_unlock;
786
787         spin_lock_irq(&connection->lock);
788         connection->state = GB_CONNECTION_STATE_DISABLED;
789         gb_connection_cancel_operations(connection, -ESHUTDOWN);
790         spin_unlock_irq(&connection->lock);
791
792         gb_connection_hd_cport_flush(connection);
793         gb_connection_hd_cport_features_disable(connection);
794         gb_connection_svc_connection_destroy(connection);
795         gb_connection_hd_cport_disable(connection);
796
797 out_unlock:
798         mutex_unlock(&connection->mutex);
799 }
800 EXPORT_SYMBOL_GPL(gb_connection_disable_forced);
801
802 /* Caller must have disabled the connection before destroying it. */
803 void gb_connection_destroy(struct gb_connection *connection)
804 {
805         if (!connection)
806                 return;
807
808         if (WARN_ON(connection->state != GB_CONNECTION_STATE_DISABLED))
809                 gb_connection_disable(connection);
810
811         mutex_lock(&gb_connection_mutex);
812
813         spin_lock_irq(&gb_connections_lock);
814         list_del(&connection->bundle_links);
815         list_del(&connection->hd_links);
816         spin_unlock_irq(&gb_connections_lock);
817
818         destroy_workqueue(connection->wq);
819
820         gb_hd_cport_release(connection->hd, connection->hd_cport_id);
821         connection->hd_cport_id = CPORT_ID_BAD;
822
823         mutex_unlock(&gb_connection_mutex);
824
825         gb_connection_put(connection);
826 }
827 EXPORT_SYMBOL_GPL(gb_connection_destroy);
828
829 void gb_connection_latency_tag_enable(struct gb_connection *connection)
830 {
831         struct gb_host_device *hd = connection->hd;
832         int ret;
833
834         if (!hd->driver->latency_tag_enable)
835                 return;
836
837         ret = hd->driver->latency_tag_enable(hd, connection->hd_cport_id);
838         if (ret) {
839                 dev_err(&connection->hd->dev,
840                         "%s: failed to enable latency tag: %d\n",
841                         connection->name, ret);
842         }
843 }
844 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_enable);
845
846 void gb_connection_latency_tag_disable(struct gb_connection *connection)
847 {
848         struct gb_host_device *hd = connection->hd;
849         int ret;
850
851         if (!hd->driver->latency_tag_disable)
852                 return;
853
854         ret = hd->driver->latency_tag_disable(hd, connection->hd_cport_id);
855         if (ret) {
856                 dev_err(&connection->hd->dev,
857                         "%s: failed to disable latency tag: %d\n",
858                         connection->name, ret);
859         }
860 }
861 EXPORT_SYMBOL_GPL(gb_connection_latency_tag_disable);