]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/thunderbolt/tunnel.c
thunderbolt: Add support for lane bonding
[linux.git] / drivers / thunderbolt / tunnel.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Thunderbolt driver - Tunneling support
4  *
5  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6  * Copyright (C) 2019, Intel Corporation
7  */
8
9 #include <linux/slab.h>
10 #include <linux/list.h>
11
12 #include "tunnel.h"
13 #include "tb.h"
14
15 /* PCIe adapters use always HopID of 8 for both directions */
16 #define TB_PCI_HOPID                    8
17
18 #define TB_PCI_PATH_DOWN                0
19 #define TB_PCI_PATH_UP                  1
20
21 /* DP adapters use HopID 8 for AUX and 9 for Video */
22 #define TB_DP_AUX_TX_HOPID              8
23 #define TB_DP_AUX_RX_HOPID              8
24 #define TB_DP_VIDEO_HOPID               9
25
26 #define TB_DP_VIDEO_PATH_OUT            0
27 #define TB_DP_AUX_PATH_OUT              1
28 #define TB_DP_AUX_PATH_IN               2
29
30 #define TB_DMA_PATH_OUT                 0
31 #define TB_DMA_PATH_IN                  1
32
33 static const char * const tb_tunnel_names[] = { "PCI", "DP", "DMA" };
34
35 #define __TB_TUNNEL_PRINT(level, tunnel, fmt, arg...)                   \
36         do {                                                            \
37                 struct tb_tunnel *__tunnel = (tunnel);                  \
38                 level(__tunnel->tb, "%llx:%x <-> %llx:%x (%s): " fmt,   \
39                       tb_route(__tunnel->src_port->sw),                 \
40                       __tunnel->src_port->port,                         \
41                       tb_route(__tunnel->dst_port->sw),                 \
42                       __tunnel->dst_port->port,                         \
43                       tb_tunnel_names[__tunnel->type],                  \
44                       ## arg);                                          \
45         } while (0)
46
47 #define tb_tunnel_WARN(tunnel, fmt, arg...) \
48         __TB_TUNNEL_PRINT(tb_WARN, tunnel, fmt, ##arg)
49 #define tb_tunnel_warn(tunnel, fmt, arg...) \
50         __TB_TUNNEL_PRINT(tb_warn, tunnel, fmt, ##arg)
51 #define tb_tunnel_info(tunnel, fmt, arg...) \
52         __TB_TUNNEL_PRINT(tb_info, tunnel, fmt, ##arg)
53 #define tb_tunnel_dbg(tunnel, fmt, arg...) \
54         __TB_TUNNEL_PRINT(tb_dbg, tunnel, fmt, ##arg)
55
56 static struct tb_tunnel *tb_tunnel_alloc(struct tb *tb, size_t npaths,
57                                          enum tb_tunnel_type type)
58 {
59         struct tb_tunnel *tunnel;
60
61         tunnel = kzalloc(sizeof(*tunnel), GFP_KERNEL);
62         if (!tunnel)
63                 return NULL;
64
65         tunnel->paths = kcalloc(npaths, sizeof(tunnel->paths[0]), GFP_KERNEL);
66         if (!tunnel->paths) {
67                 tb_tunnel_free(tunnel);
68                 return NULL;
69         }
70
71         INIT_LIST_HEAD(&tunnel->list);
72         tunnel->tb = tb;
73         tunnel->npaths = npaths;
74         tunnel->type = type;
75
76         return tunnel;
77 }
78
79 static int tb_pci_activate(struct tb_tunnel *tunnel, bool activate)
80 {
81         int res;
82
83         res = tb_pci_port_enable(tunnel->src_port, activate);
84         if (res)
85                 return res;
86
87         if (tb_port_is_pcie_up(tunnel->dst_port))
88                 return tb_pci_port_enable(tunnel->dst_port, activate);
89
90         return 0;
91 }
92
93 static int tb_initial_credits(const struct tb_switch *sw)
94 {
95         /* If the path is complete sw is not NULL */
96         if (sw) {
97                 /* More credits for faster link */
98                 switch (sw->link_speed * sw->link_width) {
99                 case 40:
100                         return 32;
101                 case 20:
102                         return 24;
103                 }
104         }
105
106         return 16;
107 }
108
109 static void tb_pci_init_path(struct tb_path *path)
110 {
111         path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
112         path->egress_shared_buffer = TB_PATH_NONE;
113         path->ingress_fc_enable = TB_PATH_ALL;
114         path->ingress_shared_buffer = TB_PATH_NONE;
115         path->priority = 3;
116         path->weight = 1;
117         path->drop_packages = 0;
118         path->nfc_credits = 0;
119         path->hops[0].initial_credits = 7;
120         path->hops[1].initial_credits =
121                 tb_initial_credits(path->hops[1].in_port->sw);
122 }
123
124 /**
125  * tb_tunnel_discover_pci() - Discover existing PCIe tunnels
126  * @tb: Pointer to the domain structure
127  * @down: PCIe downstream adapter
128  *
129  * If @down adapter is active, follows the tunnel to the PCIe upstream
130  * adapter and back. Returns the discovered tunnel or %NULL if there was
131  * no tunnel.
132  */
133 struct tb_tunnel *tb_tunnel_discover_pci(struct tb *tb, struct tb_port *down)
134 {
135         struct tb_tunnel *tunnel;
136         struct tb_path *path;
137
138         if (!tb_pci_port_is_enabled(down))
139                 return NULL;
140
141         tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
142         if (!tunnel)
143                 return NULL;
144
145         tunnel->activate = tb_pci_activate;
146         tunnel->src_port = down;
147
148         /*
149          * Discover both paths even if they are not complete. We will
150          * clean them up by calling tb_tunnel_deactivate() below in that
151          * case.
152          */
153         path = tb_path_discover(down, TB_PCI_HOPID, NULL, -1,
154                                 &tunnel->dst_port, "PCIe Up");
155         if (!path) {
156                 /* Just disable the downstream port */
157                 tb_pci_port_enable(down, false);
158                 goto err_free;
159         }
160         tunnel->paths[TB_PCI_PATH_UP] = path;
161         tb_pci_init_path(tunnel->paths[TB_PCI_PATH_UP]);
162
163         path = tb_path_discover(tunnel->dst_port, -1, down, TB_PCI_HOPID, NULL,
164                                 "PCIe Down");
165         if (!path)
166                 goto err_deactivate;
167         tunnel->paths[TB_PCI_PATH_DOWN] = path;
168         tb_pci_init_path(tunnel->paths[TB_PCI_PATH_DOWN]);
169
170         /* Validate that the tunnel is complete */
171         if (!tb_port_is_pcie_up(tunnel->dst_port)) {
172                 tb_port_warn(tunnel->dst_port,
173                              "path does not end on a PCIe adapter, cleaning up\n");
174                 goto err_deactivate;
175         }
176
177         if (down != tunnel->src_port) {
178                 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
179                 goto err_deactivate;
180         }
181
182         if (!tb_pci_port_is_enabled(tunnel->dst_port)) {
183                 tb_tunnel_warn(tunnel,
184                                "tunnel is not fully activated, cleaning up\n");
185                 goto err_deactivate;
186         }
187
188         tb_tunnel_dbg(tunnel, "discovered\n");
189         return tunnel;
190
191 err_deactivate:
192         tb_tunnel_deactivate(tunnel);
193 err_free:
194         tb_tunnel_free(tunnel);
195
196         return NULL;
197 }
198
199 /**
200  * tb_tunnel_alloc_pci() - allocate a pci tunnel
201  * @tb: Pointer to the domain structure
202  * @up: PCIe upstream adapter port
203  * @down: PCIe downstream adapter port
204  *
205  * Allocate a PCI tunnel. The ports must be of type TB_TYPE_PCIE_UP and
206  * TB_TYPE_PCIE_DOWN.
207  *
208  * Return: Returns a tb_tunnel on success or NULL on failure.
209  */
210 struct tb_tunnel *tb_tunnel_alloc_pci(struct tb *tb, struct tb_port *up,
211                                       struct tb_port *down)
212 {
213         struct tb_tunnel *tunnel;
214         struct tb_path *path;
215
216         tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_PCI);
217         if (!tunnel)
218                 return NULL;
219
220         tunnel->activate = tb_pci_activate;
221         tunnel->src_port = down;
222         tunnel->dst_port = up;
223
224         path = tb_path_alloc(tb, down, TB_PCI_HOPID, up, TB_PCI_HOPID, 0,
225                              "PCIe Down");
226         if (!path) {
227                 tb_tunnel_free(tunnel);
228                 return NULL;
229         }
230         tb_pci_init_path(path);
231         tunnel->paths[TB_PCI_PATH_DOWN] = path;
232
233         path = tb_path_alloc(tb, up, TB_PCI_HOPID, down, TB_PCI_HOPID, 0,
234                              "PCIe Up");
235         if (!path) {
236                 tb_tunnel_free(tunnel);
237                 return NULL;
238         }
239         tb_pci_init_path(path);
240         tunnel->paths[TB_PCI_PATH_UP] = path;
241
242         return tunnel;
243 }
244
245 static int tb_dp_xchg_caps(struct tb_tunnel *tunnel)
246 {
247         struct tb_port *out = tunnel->dst_port;
248         struct tb_port *in = tunnel->src_port;
249         u32 in_dp_cap, out_dp_cap;
250         int ret;
251
252         /*
253          * Copy DP_LOCAL_CAP register to DP_REMOTE_CAP register for
254          * newer generation hardware.
255          */
256         if (in->sw->generation < 2 || out->sw->generation < 2)
257                 return 0;
258
259         /* Read both DP_LOCAL_CAP registers */
260         ret = tb_port_read(in, &in_dp_cap, TB_CFG_PORT,
261                            in->cap_adap + DP_LOCAL_CAP, 1);
262         if (ret)
263                 return ret;
264
265         ret = tb_port_read(out, &out_dp_cap, TB_CFG_PORT,
266                            out->cap_adap + DP_LOCAL_CAP, 1);
267         if (ret)
268                 return ret;
269
270         /* Write IN local caps to OUT remote caps */
271         ret = tb_port_write(out, &in_dp_cap, TB_CFG_PORT,
272                             out->cap_adap + DP_REMOTE_CAP, 1);
273         if (ret)
274                 return ret;
275
276         return tb_port_write(in, &out_dp_cap, TB_CFG_PORT,
277                              in->cap_adap + DP_REMOTE_CAP, 1);
278 }
279
280 static int tb_dp_activate(struct tb_tunnel *tunnel, bool active)
281 {
282         int ret;
283
284         if (active) {
285                 struct tb_path **paths;
286                 int last;
287
288                 paths = tunnel->paths;
289                 last = paths[TB_DP_VIDEO_PATH_OUT]->path_length - 1;
290
291                 tb_dp_port_set_hops(tunnel->src_port,
292                         paths[TB_DP_VIDEO_PATH_OUT]->hops[0].in_hop_index,
293                         paths[TB_DP_AUX_PATH_OUT]->hops[0].in_hop_index,
294                         paths[TB_DP_AUX_PATH_IN]->hops[last].next_hop_index);
295
296                 tb_dp_port_set_hops(tunnel->dst_port,
297                         paths[TB_DP_VIDEO_PATH_OUT]->hops[last].next_hop_index,
298                         paths[TB_DP_AUX_PATH_IN]->hops[0].in_hop_index,
299                         paths[TB_DP_AUX_PATH_OUT]->hops[last].next_hop_index);
300         } else {
301                 tb_dp_port_hpd_clear(tunnel->src_port);
302                 tb_dp_port_set_hops(tunnel->src_port, 0, 0, 0);
303                 if (tb_port_is_dpout(tunnel->dst_port))
304                         tb_dp_port_set_hops(tunnel->dst_port, 0, 0, 0);
305         }
306
307         ret = tb_dp_port_enable(tunnel->src_port, active);
308         if (ret)
309                 return ret;
310
311         if (tb_port_is_dpout(tunnel->dst_port))
312                 return tb_dp_port_enable(tunnel->dst_port, active);
313
314         return 0;
315 }
316
317 static void tb_dp_init_aux_path(struct tb_path *path)
318 {
319         int i;
320
321         path->egress_fc_enable = TB_PATH_SOURCE | TB_PATH_INTERNAL;
322         path->egress_shared_buffer = TB_PATH_NONE;
323         path->ingress_fc_enable = TB_PATH_ALL;
324         path->ingress_shared_buffer = TB_PATH_NONE;
325         path->priority = 2;
326         path->weight = 1;
327
328         for (i = 0; i < path->path_length; i++)
329                 path->hops[i].initial_credits = 1;
330 }
331
332 static void tb_dp_init_video_path(struct tb_path *path, bool discover)
333 {
334         u32 nfc_credits = path->hops[0].in_port->config.nfc_credits;
335
336         path->egress_fc_enable = TB_PATH_NONE;
337         path->egress_shared_buffer = TB_PATH_NONE;
338         path->ingress_fc_enable = TB_PATH_NONE;
339         path->ingress_shared_buffer = TB_PATH_NONE;
340         path->priority = 1;
341         path->weight = 1;
342
343         if (discover) {
344                 path->nfc_credits = nfc_credits & ADP_CS_4_NFC_BUFFERS_MASK;
345         } else {
346                 u32 max_credits;
347
348                 max_credits = (nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >>
349                         ADP_CS_4_TOTAL_BUFFERS_SHIFT;
350                 /* Leave some credits for AUX path */
351                 path->nfc_credits = min(max_credits - 2, 12U);
352         }
353 }
354
355 /**
356  * tb_tunnel_discover_dp() - Discover existing Display Port tunnels
357  * @tb: Pointer to the domain structure
358  * @in: DP in adapter
359  *
360  * If @in adapter is active, follows the tunnel to the DP out adapter
361  * and back. Returns the discovered tunnel or %NULL if there was no
362  * tunnel.
363  *
364  * Return: DP tunnel or %NULL if no tunnel found.
365  */
366 struct tb_tunnel *tb_tunnel_discover_dp(struct tb *tb, struct tb_port *in)
367 {
368         struct tb_tunnel *tunnel;
369         struct tb_port *port;
370         struct tb_path *path;
371
372         if (!tb_dp_port_is_enabled(in))
373                 return NULL;
374
375         tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
376         if (!tunnel)
377                 return NULL;
378
379         tunnel->init = tb_dp_xchg_caps;
380         tunnel->activate = tb_dp_activate;
381         tunnel->src_port = in;
382
383         path = tb_path_discover(in, TB_DP_VIDEO_HOPID, NULL, -1,
384                                 &tunnel->dst_port, "Video");
385         if (!path) {
386                 /* Just disable the DP IN port */
387                 tb_dp_port_enable(in, false);
388                 goto err_free;
389         }
390         tunnel->paths[TB_DP_VIDEO_PATH_OUT] = path;
391         tb_dp_init_video_path(tunnel->paths[TB_DP_VIDEO_PATH_OUT], true);
392
393         path = tb_path_discover(in, TB_DP_AUX_TX_HOPID, NULL, -1, NULL, "AUX TX");
394         if (!path)
395                 goto err_deactivate;
396         tunnel->paths[TB_DP_AUX_PATH_OUT] = path;
397         tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_OUT]);
398
399         path = tb_path_discover(tunnel->dst_port, -1, in, TB_DP_AUX_RX_HOPID,
400                                 &port, "AUX RX");
401         if (!path)
402                 goto err_deactivate;
403         tunnel->paths[TB_DP_AUX_PATH_IN] = path;
404         tb_dp_init_aux_path(tunnel->paths[TB_DP_AUX_PATH_IN]);
405
406         /* Validate that the tunnel is complete */
407         if (!tb_port_is_dpout(tunnel->dst_port)) {
408                 tb_port_warn(in, "path does not end on a DP adapter, cleaning up\n");
409                 goto err_deactivate;
410         }
411
412         if (!tb_dp_port_is_enabled(tunnel->dst_port))
413                 goto err_deactivate;
414
415         if (!tb_dp_port_hpd_is_active(tunnel->dst_port))
416                 goto err_deactivate;
417
418         if (port != tunnel->src_port) {
419                 tb_tunnel_warn(tunnel, "path is not complete, cleaning up\n");
420                 goto err_deactivate;
421         }
422
423         tb_tunnel_dbg(tunnel, "discovered\n");
424         return tunnel;
425
426 err_deactivate:
427         tb_tunnel_deactivate(tunnel);
428 err_free:
429         tb_tunnel_free(tunnel);
430
431         return NULL;
432 }
433
434 /**
435  * tb_tunnel_alloc_dp() - allocate a Display Port tunnel
436  * @tb: Pointer to the domain structure
437  * @in: DP in adapter port
438  * @out: DP out adapter port
439  *
440  * Allocates a tunnel between @in and @out that is capable of tunneling
441  * Display Port traffic.
442  *
443  * Return: Returns a tb_tunnel on success or NULL on failure.
444  */
445 struct tb_tunnel *tb_tunnel_alloc_dp(struct tb *tb, struct tb_port *in,
446                                      struct tb_port *out)
447 {
448         struct tb_tunnel *tunnel;
449         struct tb_path **paths;
450         struct tb_path *path;
451
452         if (WARN_ON(!in->cap_adap || !out->cap_adap))
453                 return NULL;
454
455         tunnel = tb_tunnel_alloc(tb, 3, TB_TUNNEL_DP);
456         if (!tunnel)
457                 return NULL;
458
459         tunnel->init = tb_dp_xchg_caps;
460         tunnel->activate = tb_dp_activate;
461         tunnel->src_port = in;
462         tunnel->dst_port = out;
463
464         paths = tunnel->paths;
465
466         path = tb_path_alloc(tb, in, TB_DP_VIDEO_HOPID, out, TB_DP_VIDEO_HOPID,
467                              1, "Video");
468         if (!path)
469                 goto err_free;
470         tb_dp_init_video_path(path, false);
471         paths[TB_DP_VIDEO_PATH_OUT] = path;
472
473         path = tb_path_alloc(tb, in, TB_DP_AUX_TX_HOPID, out,
474                              TB_DP_AUX_TX_HOPID, 1, "AUX TX");
475         if (!path)
476                 goto err_free;
477         tb_dp_init_aux_path(path);
478         paths[TB_DP_AUX_PATH_OUT] = path;
479
480         path = tb_path_alloc(tb, out, TB_DP_AUX_RX_HOPID, in,
481                              TB_DP_AUX_RX_HOPID, 1, "AUX RX");
482         if (!path)
483                 goto err_free;
484         tb_dp_init_aux_path(path);
485         paths[TB_DP_AUX_PATH_IN] = path;
486
487         return tunnel;
488
489 err_free:
490         tb_tunnel_free(tunnel);
491         return NULL;
492 }
493
494 static u32 tb_dma_credits(struct tb_port *nhi)
495 {
496         u32 max_credits;
497
498         max_credits = (nhi->config.nfc_credits & ADP_CS_4_TOTAL_BUFFERS_MASK) >>
499                 ADP_CS_4_TOTAL_BUFFERS_SHIFT;
500         return min(max_credits, 13U);
501 }
502
503 static int tb_dma_activate(struct tb_tunnel *tunnel, bool active)
504 {
505         struct tb_port *nhi = tunnel->src_port;
506         u32 credits;
507
508         credits = active ? tb_dma_credits(nhi) : 0;
509         return tb_port_set_initial_credits(nhi, credits);
510 }
511
512 static void tb_dma_init_path(struct tb_path *path, unsigned int isb,
513                              unsigned int efc, u32 credits)
514 {
515         int i;
516
517         path->egress_fc_enable = efc;
518         path->ingress_fc_enable = TB_PATH_ALL;
519         path->egress_shared_buffer = TB_PATH_NONE;
520         path->ingress_shared_buffer = isb;
521         path->priority = 5;
522         path->weight = 1;
523         path->clear_fc = true;
524
525         for (i = 0; i < path->path_length; i++)
526                 path->hops[i].initial_credits = credits;
527 }
528
529 /**
530  * tb_tunnel_alloc_dma() - allocate a DMA tunnel
531  * @tb: Pointer to the domain structure
532  * @nhi: Host controller port
533  * @dst: Destination null port which the other domain is connected to
534  * @transmit_ring: NHI ring number used to send packets towards the
535  *                 other domain
536  * @transmit_path: HopID used for transmitting packets
537  * @receive_ring: NHI ring number used to receive packets from the
538  *                other domain
539  * @reveive_path: HopID used for receiving packets
540  *
541  * Return: Returns a tb_tunnel on success or NULL on failure.
542  */
543 struct tb_tunnel *tb_tunnel_alloc_dma(struct tb *tb, struct tb_port *nhi,
544                                       struct tb_port *dst, int transmit_ring,
545                                       int transmit_path, int receive_ring,
546                                       int receive_path)
547 {
548         struct tb_tunnel *tunnel;
549         struct tb_path *path;
550         u32 credits;
551
552         tunnel = tb_tunnel_alloc(tb, 2, TB_TUNNEL_DMA);
553         if (!tunnel)
554                 return NULL;
555
556         tunnel->activate = tb_dma_activate;
557         tunnel->src_port = nhi;
558         tunnel->dst_port = dst;
559
560         credits = tb_dma_credits(nhi);
561
562         path = tb_path_alloc(tb, dst, receive_path, nhi, receive_ring, 0, "DMA RX");
563         if (!path) {
564                 tb_tunnel_free(tunnel);
565                 return NULL;
566         }
567         tb_dma_init_path(path, TB_PATH_NONE, TB_PATH_SOURCE | TB_PATH_INTERNAL,
568                          credits);
569         tunnel->paths[TB_DMA_PATH_IN] = path;
570
571         path = tb_path_alloc(tb, nhi, transmit_ring, dst, transmit_path, 0, "DMA TX");
572         if (!path) {
573                 tb_tunnel_free(tunnel);
574                 return NULL;
575         }
576         tb_dma_init_path(path, TB_PATH_SOURCE, TB_PATH_ALL, credits);
577         tunnel->paths[TB_DMA_PATH_OUT] = path;
578
579         return tunnel;
580 }
581
582 /**
583  * tb_tunnel_free() - free a tunnel
584  * @tunnel: Tunnel to be freed
585  *
586  * Frees a tunnel. The tunnel does not need to be deactivated.
587  */
588 void tb_tunnel_free(struct tb_tunnel *tunnel)
589 {
590         int i;
591
592         if (!tunnel)
593                 return;
594
595         for (i = 0; i < tunnel->npaths; i++) {
596                 if (tunnel->paths[i])
597                         tb_path_free(tunnel->paths[i]);
598         }
599
600         kfree(tunnel->paths);
601         kfree(tunnel);
602 }
603
604 /**
605  * tb_tunnel_is_invalid - check whether an activated path is still valid
606  * @tunnel: Tunnel to check
607  */
608 bool tb_tunnel_is_invalid(struct tb_tunnel *tunnel)
609 {
610         int i;
611
612         for (i = 0; i < tunnel->npaths; i++) {
613                 WARN_ON(!tunnel->paths[i]->activated);
614                 if (tb_path_is_invalid(tunnel->paths[i]))
615                         return true;
616         }
617
618         return false;
619 }
620
621 /**
622  * tb_tunnel_restart() - activate a tunnel after a hardware reset
623  * @tunnel: Tunnel to restart
624  *
625  * Return: 0 on success and negative errno in case if failure
626  */
627 int tb_tunnel_restart(struct tb_tunnel *tunnel)
628 {
629         int res, i;
630
631         tb_tunnel_dbg(tunnel, "activating\n");
632
633         /*
634          * Make sure all paths are properly disabled before enabling
635          * them again.
636          */
637         for (i = 0; i < tunnel->npaths; i++) {
638                 if (tunnel->paths[i]->activated) {
639                         tb_path_deactivate(tunnel->paths[i]);
640                         tunnel->paths[i]->activated = false;
641                 }
642         }
643
644         if (tunnel->init) {
645                 res = tunnel->init(tunnel);
646                 if (res)
647                         return res;
648         }
649
650         for (i = 0; i < tunnel->npaths; i++) {
651                 res = tb_path_activate(tunnel->paths[i]);
652                 if (res)
653                         goto err;
654         }
655
656         if (tunnel->activate) {
657                 res = tunnel->activate(tunnel, true);
658                 if (res)
659                         goto err;
660         }
661
662         return 0;
663
664 err:
665         tb_tunnel_warn(tunnel, "activation failed\n");
666         tb_tunnel_deactivate(tunnel);
667         return res;
668 }
669
670 /**
671  * tb_tunnel_activate() - activate a tunnel
672  * @tunnel: Tunnel to activate
673  *
674  * Return: Returns 0 on success or an error code on failure.
675  */
676 int tb_tunnel_activate(struct tb_tunnel *tunnel)
677 {
678         int i;
679
680         for (i = 0; i < tunnel->npaths; i++) {
681                 if (tunnel->paths[i]->activated) {
682                         tb_tunnel_WARN(tunnel,
683                                        "trying to activate an already activated tunnel\n");
684                         return -EINVAL;
685                 }
686         }
687
688         return tb_tunnel_restart(tunnel);
689 }
690
691 /**
692  * tb_tunnel_deactivate() - deactivate a tunnel
693  * @tunnel: Tunnel to deactivate
694  */
695 void tb_tunnel_deactivate(struct tb_tunnel *tunnel)
696 {
697         int i;
698
699         tb_tunnel_dbg(tunnel, "deactivating\n");
700
701         if (tunnel->activate)
702                 tunnel->activate(tunnel, false);
703
704         for (i = 0; i < tunnel->npaths; i++) {
705                 if (tunnel->paths[i] && tunnel->paths[i]->activated)
706                         tb_path_deactivate(tunnel->paths[i]);
707         }
708 }