xref: /OK3568_Linux_fs/kernel/drivers/thunderbolt/path.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Thunderbolt driver - path/tunnel functionality
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6*4882a593Smuzhiyun  * Copyright (C) 2019, Intel Corporation
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/slab.h>
10*4882a593Smuzhiyun #include <linux/errno.h>
11*4882a593Smuzhiyun #include <linux/delay.h>
12*4882a593Smuzhiyun #include <linux/ktime.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "tb.h"
15*4882a593Smuzhiyun 
tb_dump_hop(const struct tb_path_hop * hop,const struct tb_regs_hop * regs)16*4882a593Smuzhiyun static void tb_dump_hop(const struct tb_path_hop *hop, const struct tb_regs_hop *regs)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun 	const struct tb_port *port = hop->in_port;
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun 	tb_port_dbg(port, " In HopID: %d => Out port: %d Out HopID: %d\n",
21*4882a593Smuzhiyun 		    hop->in_hop_index, regs->out_port, regs->next_hop);
22*4882a593Smuzhiyun 	tb_port_dbg(port, "  Weight: %d Priority: %d Credits: %d Drop: %d\n",
23*4882a593Smuzhiyun 		    regs->weight, regs->priority,
24*4882a593Smuzhiyun 		    regs->initial_credits, regs->drop_packages);
25*4882a593Smuzhiyun 	tb_port_dbg(port, "   Counter enabled: %d Counter index: %d\n",
26*4882a593Smuzhiyun 		    regs->counter_enable, regs->counter);
27*4882a593Smuzhiyun 	tb_port_dbg(port, "  Flow Control (In/Eg): %d/%d Shared Buffer (In/Eg): %d/%d\n",
28*4882a593Smuzhiyun 		    regs->ingress_fc, regs->egress_fc,
29*4882a593Smuzhiyun 		    regs->ingress_shared_buffer, regs->egress_shared_buffer);
30*4882a593Smuzhiyun 	tb_port_dbg(port, "  Unknown1: %#x Unknown2: %#x Unknown3: %#x\n",
31*4882a593Smuzhiyun 		    regs->unknown1, regs->unknown2, regs->unknown3);
32*4882a593Smuzhiyun }
33*4882a593Smuzhiyun 
tb_path_find_dst_port(struct tb_port * src,int src_hopid,int dst_hopid)34*4882a593Smuzhiyun static struct tb_port *tb_path_find_dst_port(struct tb_port *src, int src_hopid,
35*4882a593Smuzhiyun 					     int dst_hopid)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun 	struct tb_port *port, *out_port = NULL;
38*4882a593Smuzhiyun 	struct tb_regs_hop hop;
39*4882a593Smuzhiyun 	struct tb_switch *sw;
40*4882a593Smuzhiyun 	int i, ret, hopid;
41*4882a593Smuzhiyun 
42*4882a593Smuzhiyun 	hopid = src_hopid;
43*4882a593Smuzhiyun 	port = src;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	for (i = 0; port && i < TB_PATH_MAX_HOPS; i++) {
46*4882a593Smuzhiyun 		sw = port->sw;
47*4882a593Smuzhiyun 
48*4882a593Smuzhiyun 		ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hopid, 2);
49*4882a593Smuzhiyun 		if (ret) {
50*4882a593Smuzhiyun 			tb_port_warn(port, "failed to read path at %d\n", hopid);
51*4882a593Smuzhiyun 			return NULL;
52*4882a593Smuzhiyun 		}
53*4882a593Smuzhiyun 
54*4882a593Smuzhiyun 		if (!hop.enable)
55*4882a593Smuzhiyun 			return NULL;
56*4882a593Smuzhiyun 
57*4882a593Smuzhiyun 		out_port = &sw->ports[hop.out_port];
58*4882a593Smuzhiyun 		hopid = hop.next_hop;
59*4882a593Smuzhiyun 		port = out_port->remote;
60*4882a593Smuzhiyun 	}
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	return out_port && hopid == dst_hopid ? out_port : NULL;
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun 
tb_path_find_src_hopid(struct tb_port * src,const struct tb_port * dst,int dst_hopid)65*4882a593Smuzhiyun static int tb_path_find_src_hopid(struct tb_port *src,
66*4882a593Smuzhiyun 	const struct tb_port *dst, int dst_hopid)
67*4882a593Smuzhiyun {
68*4882a593Smuzhiyun 	struct tb_port *out;
69*4882a593Smuzhiyun 	int i;
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	for (i = TB_PATH_MIN_HOPID; i <= src->config.max_in_hop_id; i++) {
72*4882a593Smuzhiyun 		out = tb_path_find_dst_port(src, i, dst_hopid);
73*4882a593Smuzhiyun 		if (out == dst)
74*4882a593Smuzhiyun 			return i;
75*4882a593Smuzhiyun 	}
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	return 0;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /**
81*4882a593Smuzhiyun  * tb_path_discover() - Discover a path
82*4882a593Smuzhiyun  * @src: First input port of a path
83*4882a593Smuzhiyun  * @src_hopid: Starting HopID of a path (%-1 if don't care)
84*4882a593Smuzhiyun  * @dst: Expected destination port of the path (%NULL if don't care)
85*4882a593Smuzhiyun  * @dst_hopid: HopID to the @dst (%-1 if don't care)
86*4882a593Smuzhiyun  * @last: Last port is filled here if not %NULL
87*4882a593Smuzhiyun  * @name: Name of the path
88*4882a593Smuzhiyun  *
89*4882a593Smuzhiyun  * Follows a path starting from @src and @src_hopid to the last output
90*4882a593Smuzhiyun  * port of the path. Allocates HopIDs for the visited ports. Call
91*4882a593Smuzhiyun  * tb_path_free() to release the path and allocated HopIDs when the path
92*4882a593Smuzhiyun  * is not needed anymore.
93*4882a593Smuzhiyun  *
94*4882a593Smuzhiyun  * Note function discovers also incomplete paths so caller should check
95*4882a593Smuzhiyun  * that the @dst port is the expected one. If it is not, the path can be
96*4882a593Smuzhiyun  * cleaned up by calling tb_path_deactivate() before tb_path_free().
97*4882a593Smuzhiyun  *
98*4882a593Smuzhiyun  * Return: Discovered path on success, %NULL in case of failure
99*4882a593Smuzhiyun  */
tb_path_discover(struct tb_port * src,int src_hopid,struct tb_port * dst,int dst_hopid,struct tb_port ** last,const char * name)100*4882a593Smuzhiyun struct tb_path *tb_path_discover(struct tb_port *src, int src_hopid,
101*4882a593Smuzhiyun 				 struct tb_port *dst, int dst_hopid,
102*4882a593Smuzhiyun 				 struct tb_port **last, const char *name)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun 	struct tb_port *out_port;
105*4882a593Smuzhiyun 	struct tb_regs_hop hop;
106*4882a593Smuzhiyun 	struct tb_path *path;
107*4882a593Smuzhiyun 	struct tb_switch *sw;
108*4882a593Smuzhiyun 	struct tb_port *p;
109*4882a593Smuzhiyun 	size_t num_hops;
110*4882a593Smuzhiyun 	int ret, i, h;
111*4882a593Smuzhiyun 
112*4882a593Smuzhiyun 	if (src_hopid < 0 && dst) {
113*4882a593Smuzhiyun 		/*
114*4882a593Smuzhiyun 		 * For incomplete paths the intermediate HopID can be
115*4882a593Smuzhiyun 		 * different from the one used by the protocol adapter
116*4882a593Smuzhiyun 		 * so in that case find a path that ends on @dst with
117*4882a593Smuzhiyun 		 * matching @dst_hopid. That should give us the correct
118*4882a593Smuzhiyun 		 * HopID for the @src.
119*4882a593Smuzhiyun 		 */
120*4882a593Smuzhiyun 		src_hopid = tb_path_find_src_hopid(src, dst, dst_hopid);
121*4882a593Smuzhiyun 		if (!src_hopid)
122*4882a593Smuzhiyun 			return NULL;
123*4882a593Smuzhiyun 	}
124*4882a593Smuzhiyun 
125*4882a593Smuzhiyun 	p = src;
126*4882a593Smuzhiyun 	h = src_hopid;
127*4882a593Smuzhiyun 	num_hops = 0;
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	for (i = 0; p && i < TB_PATH_MAX_HOPS; i++) {
130*4882a593Smuzhiyun 		sw = p->sw;
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 		ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2);
133*4882a593Smuzhiyun 		if (ret) {
134*4882a593Smuzhiyun 			tb_port_warn(p, "failed to read path at %d\n", h);
135*4882a593Smuzhiyun 			return NULL;
136*4882a593Smuzhiyun 		}
137*4882a593Smuzhiyun 
138*4882a593Smuzhiyun 		/* If the hop is not enabled we got an incomplete path */
139*4882a593Smuzhiyun 		if (!hop.enable)
140*4882a593Smuzhiyun 			break;
141*4882a593Smuzhiyun 
142*4882a593Smuzhiyun 		out_port = &sw->ports[hop.out_port];
143*4882a593Smuzhiyun 		if (last)
144*4882a593Smuzhiyun 			*last = out_port;
145*4882a593Smuzhiyun 
146*4882a593Smuzhiyun 		h = hop.next_hop;
147*4882a593Smuzhiyun 		p = out_port->remote;
148*4882a593Smuzhiyun 		num_hops++;
149*4882a593Smuzhiyun 	}
150*4882a593Smuzhiyun 
151*4882a593Smuzhiyun 	path = kzalloc(sizeof(*path), GFP_KERNEL);
152*4882a593Smuzhiyun 	if (!path)
153*4882a593Smuzhiyun 		return NULL;
154*4882a593Smuzhiyun 
155*4882a593Smuzhiyun 	path->name = name;
156*4882a593Smuzhiyun 	path->tb = src->sw->tb;
157*4882a593Smuzhiyun 	path->path_length = num_hops;
158*4882a593Smuzhiyun 	path->activated = true;
159*4882a593Smuzhiyun 
160*4882a593Smuzhiyun 	path->hops = kcalloc(num_hops, sizeof(*path->hops), GFP_KERNEL);
161*4882a593Smuzhiyun 	if (!path->hops) {
162*4882a593Smuzhiyun 		kfree(path);
163*4882a593Smuzhiyun 		return NULL;
164*4882a593Smuzhiyun 	}
165*4882a593Smuzhiyun 
166*4882a593Smuzhiyun 	p = src;
167*4882a593Smuzhiyun 	h = src_hopid;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	for (i = 0; i < num_hops; i++) {
170*4882a593Smuzhiyun 		int next_hop;
171*4882a593Smuzhiyun 
172*4882a593Smuzhiyun 		sw = p->sw;
173*4882a593Smuzhiyun 
174*4882a593Smuzhiyun 		ret = tb_port_read(p, &hop, TB_CFG_HOPS, 2 * h, 2);
175*4882a593Smuzhiyun 		if (ret) {
176*4882a593Smuzhiyun 			tb_port_warn(p, "failed to read path at %d\n", h);
177*4882a593Smuzhiyun 			goto err;
178*4882a593Smuzhiyun 		}
179*4882a593Smuzhiyun 
180*4882a593Smuzhiyun 		if (tb_port_alloc_in_hopid(p, h, h) < 0)
181*4882a593Smuzhiyun 			goto err;
182*4882a593Smuzhiyun 
183*4882a593Smuzhiyun 		out_port = &sw->ports[hop.out_port];
184*4882a593Smuzhiyun 		next_hop = hop.next_hop;
185*4882a593Smuzhiyun 
186*4882a593Smuzhiyun 		if (tb_port_alloc_out_hopid(out_port, next_hop, next_hop) < 0) {
187*4882a593Smuzhiyun 			tb_port_release_in_hopid(p, h);
188*4882a593Smuzhiyun 			goto err;
189*4882a593Smuzhiyun 		}
190*4882a593Smuzhiyun 
191*4882a593Smuzhiyun 		path->hops[i].in_port = p;
192*4882a593Smuzhiyun 		path->hops[i].in_hop_index = h;
193*4882a593Smuzhiyun 		path->hops[i].in_counter_index = -1;
194*4882a593Smuzhiyun 		path->hops[i].out_port = out_port;
195*4882a593Smuzhiyun 		path->hops[i].next_hop_index = next_hop;
196*4882a593Smuzhiyun 
197*4882a593Smuzhiyun 		h = next_hop;
198*4882a593Smuzhiyun 		p = out_port->remote;
199*4882a593Smuzhiyun 	}
200*4882a593Smuzhiyun 
201*4882a593Smuzhiyun 	return path;
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun err:
204*4882a593Smuzhiyun 	tb_port_warn(src, "failed to discover path starting at HopID %d\n",
205*4882a593Smuzhiyun 		     src_hopid);
206*4882a593Smuzhiyun 	tb_path_free(path);
207*4882a593Smuzhiyun 	return NULL;
208*4882a593Smuzhiyun }
209*4882a593Smuzhiyun 
210*4882a593Smuzhiyun /**
211*4882a593Smuzhiyun  * tb_path_alloc() - allocate a thunderbolt path between two ports
212*4882a593Smuzhiyun  * @tb: Domain pointer
213*4882a593Smuzhiyun  * @src: Source port of the path
214*4882a593Smuzhiyun  * @src_hopid: HopID used for the first ingress port in the path
215*4882a593Smuzhiyun  * @dst: Destination port of the path
216*4882a593Smuzhiyun  * @dst_hopid: HopID used for the last egress port in the path
217*4882a593Smuzhiyun  * @link_nr: Preferred link if there are dual links on the path
218*4882a593Smuzhiyun  * @name: Name of the path
219*4882a593Smuzhiyun  *
220*4882a593Smuzhiyun  * Creates path between two ports starting with given @src_hopid. Reserves
221*4882a593Smuzhiyun  * HopIDs for each port (they can be different from @src_hopid depending on
222*4882a593Smuzhiyun  * how many HopIDs each port already have reserved). If there are dual
223*4882a593Smuzhiyun  * links on the path, prioritizes using @link_nr but takes into account
224*4882a593Smuzhiyun  * that the lanes may be bonded.
225*4882a593Smuzhiyun  *
226*4882a593Smuzhiyun  * Return: Returns a tb_path on success or NULL on failure.
227*4882a593Smuzhiyun  */
tb_path_alloc(struct tb * tb,struct tb_port * src,int src_hopid,struct tb_port * dst,int dst_hopid,int link_nr,const char * name)228*4882a593Smuzhiyun struct tb_path *tb_path_alloc(struct tb *tb, struct tb_port *src, int src_hopid,
229*4882a593Smuzhiyun 			      struct tb_port *dst, int dst_hopid, int link_nr,
230*4882a593Smuzhiyun 			      const char *name)
231*4882a593Smuzhiyun {
232*4882a593Smuzhiyun 	struct tb_port *in_port, *out_port, *first_port, *last_port;
233*4882a593Smuzhiyun 	int in_hopid, out_hopid;
234*4882a593Smuzhiyun 	struct tb_path *path;
235*4882a593Smuzhiyun 	size_t num_hops;
236*4882a593Smuzhiyun 	int i, ret;
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 	path = kzalloc(sizeof(*path), GFP_KERNEL);
239*4882a593Smuzhiyun 	if (!path)
240*4882a593Smuzhiyun 		return NULL;
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	first_port = last_port = NULL;
243*4882a593Smuzhiyun 	i = 0;
244*4882a593Smuzhiyun 	tb_for_each_port_on_path(src, dst, in_port) {
245*4882a593Smuzhiyun 		if (!first_port)
246*4882a593Smuzhiyun 			first_port = in_port;
247*4882a593Smuzhiyun 		last_port = in_port;
248*4882a593Smuzhiyun 		i++;
249*4882a593Smuzhiyun 	}
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	/* Check that src and dst are reachable */
252*4882a593Smuzhiyun 	if (first_port != src || last_port != dst) {
253*4882a593Smuzhiyun 		kfree(path);
254*4882a593Smuzhiyun 		return NULL;
255*4882a593Smuzhiyun 	}
256*4882a593Smuzhiyun 
257*4882a593Smuzhiyun 	/* Each hop takes two ports */
258*4882a593Smuzhiyun 	num_hops = i / 2;
259*4882a593Smuzhiyun 
260*4882a593Smuzhiyun 	path->hops = kcalloc(num_hops, sizeof(*path->hops), GFP_KERNEL);
261*4882a593Smuzhiyun 	if (!path->hops) {
262*4882a593Smuzhiyun 		kfree(path);
263*4882a593Smuzhiyun 		return NULL;
264*4882a593Smuzhiyun 	}
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun 	in_hopid = src_hopid;
267*4882a593Smuzhiyun 	out_port = NULL;
268*4882a593Smuzhiyun 
269*4882a593Smuzhiyun 	for (i = 0; i < num_hops; i++) {
270*4882a593Smuzhiyun 		in_port = tb_next_port_on_path(src, dst, out_port);
271*4882a593Smuzhiyun 		if (!in_port)
272*4882a593Smuzhiyun 			goto err;
273*4882a593Smuzhiyun 
274*4882a593Smuzhiyun 		/* When lanes are bonded primary link must be used */
275*4882a593Smuzhiyun 		if (!in_port->bonded && in_port->dual_link_port &&
276*4882a593Smuzhiyun 		    in_port->link_nr != link_nr)
277*4882a593Smuzhiyun 			in_port = in_port->dual_link_port;
278*4882a593Smuzhiyun 
279*4882a593Smuzhiyun 		ret = tb_port_alloc_in_hopid(in_port, in_hopid, in_hopid);
280*4882a593Smuzhiyun 		if (ret < 0)
281*4882a593Smuzhiyun 			goto err;
282*4882a593Smuzhiyun 		in_hopid = ret;
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 		out_port = tb_next_port_on_path(src, dst, in_port);
285*4882a593Smuzhiyun 		if (!out_port)
286*4882a593Smuzhiyun 			goto err;
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 		/*
289*4882a593Smuzhiyun 		 * Pick up right port when going from non-bonded to
290*4882a593Smuzhiyun 		 * bonded or from bonded to non-bonded.
291*4882a593Smuzhiyun 		 */
292*4882a593Smuzhiyun 		if (out_port->dual_link_port) {
293*4882a593Smuzhiyun 			if (!in_port->bonded && out_port->bonded &&
294*4882a593Smuzhiyun 			    out_port->link_nr) {
295*4882a593Smuzhiyun 				/*
296*4882a593Smuzhiyun 				 * Use primary link when going from
297*4882a593Smuzhiyun 				 * non-bonded to bonded.
298*4882a593Smuzhiyun 				 */
299*4882a593Smuzhiyun 				out_port = out_port->dual_link_port;
300*4882a593Smuzhiyun 			} else if (!out_port->bonded &&
301*4882a593Smuzhiyun 				   out_port->link_nr != link_nr) {
302*4882a593Smuzhiyun 				/*
303*4882a593Smuzhiyun 				 * If out port is not bonded follow
304*4882a593Smuzhiyun 				 * link_nr.
305*4882a593Smuzhiyun 				 */
306*4882a593Smuzhiyun 				out_port = out_port->dual_link_port;
307*4882a593Smuzhiyun 			}
308*4882a593Smuzhiyun 		}
309*4882a593Smuzhiyun 
310*4882a593Smuzhiyun 		if (i == num_hops - 1)
311*4882a593Smuzhiyun 			ret = tb_port_alloc_out_hopid(out_port, dst_hopid,
312*4882a593Smuzhiyun 						      dst_hopid);
313*4882a593Smuzhiyun 		else
314*4882a593Smuzhiyun 			ret = tb_port_alloc_out_hopid(out_port, -1, -1);
315*4882a593Smuzhiyun 
316*4882a593Smuzhiyun 		if (ret < 0)
317*4882a593Smuzhiyun 			goto err;
318*4882a593Smuzhiyun 		out_hopid = ret;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 		path->hops[i].in_hop_index = in_hopid;
321*4882a593Smuzhiyun 		path->hops[i].in_port = in_port;
322*4882a593Smuzhiyun 		path->hops[i].in_counter_index = -1;
323*4882a593Smuzhiyun 		path->hops[i].out_port = out_port;
324*4882a593Smuzhiyun 		path->hops[i].next_hop_index = out_hopid;
325*4882a593Smuzhiyun 
326*4882a593Smuzhiyun 		in_hopid = out_hopid;
327*4882a593Smuzhiyun 	}
328*4882a593Smuzhiyun 
329*4882a593Smuzhiyun 	path->tb = tb;
330*4882a593Smuzhiyun 	path->path_length = num_hops;
331*4882a593Smuzhiyun 	path->name = name;
332*4882a593Smuzhiyun 
333*4882a593Smuzhiyun 	return path;
334*4882a593Smuzhiyun 
335*4882a593Smuzhiyun err:
336*4882a593Smuzhiyun 	tb_path_free(path);
337*4882a593Smuzhiyun 	return NULL;
338*4882a593Smuzhiyun }
339*4882a593Smuzhiyun 
340*4882a593Smuzhiyun /**
341*4882a593Smuzhiyun  * tb_path_free() - free a path
342*4882a593Smuzhiyun  * @path: Path to free
343*4882a593Smuzhiyun  *
344*4882a593Smuzhiyun  * Frees a path. The path does not need to be deactivated.
345*4882a593Smuzhiyun  */
tb_path_free(struct tb_path * path)346*4882a593Smuzhiyun void tb_path_free(struct tb_path *path)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	int i;
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	for (i = 0; i < path->path_length; i++) {
351*4882a593Smuzhiyun 		const struct tb_path_hop *hop = &path->hops[i];
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 		if (hop->in_port)
354*4882a593Smuzhiyun 			tb_port_release_in_hopid(hop->in_port,
355*4882a593Smuzhiyun 						 hop->in_hop_index);
356*4882a593Smuzhiyun 		if (hop->out_port)
357*4882a593Smuzhiyun 			tb_port_release_out_hopid(hop->out_port,
358*4882a593Smuzhiyun 						  hop->next_hop_index);
359*4882a593Smuzhiyun 	}
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	kfree(path->hops);
362*4882a593Smuzhiyun 	kfree(path);
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun 
__tb_path_deallocate_nfc(struct tb_path * path,int first_hop)365*4882a593Smuzhiyun static void __tb_path_deallocate_nfc(struct tb_path *path, int first_hop)
366*4882a593Smuzhiyun {
367*4882a593Smuzhiyun 	int i, res;
368*4882a593Smuzhiyun 	for (i = first_hop; i < path->path_length; i++) {
369*4882a593Smuzhiyun 		res = tb_port_add_nfc_credits(path->hops[i].in_port,
370*4882a593Smuzhiyun 					      -path->nfc_credits);
371*4882a593Smuzhiyun 		if (res)
372*4882a593Smuzhiyun 			tb_port_warn(path->hops[i].in_port,
373*4882a593Smuzhiyun 				     "nfc credits deallocation failed for hop %d\n",
374*4882a593Smuzhiyun 				     i);
375*4882a593Smuzhiyun 	}
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun 
__tb_path_deactivate_hop(struct tb_port * port,int hop_index,bool clear_fc)378*4882a593Smuzhiyun static int __tb_path_deactivate_hop(struct tb_port *port, int hop_index,
379*4882a593Smuzhiyun 				    bool clear_fc)
380*4882a593Smuzhiyun {
381*4882a593Smuzhiyun 	struct tb_regs_hop hop;
382*4882a593Smuzhiyun 	ktime_t timeout;
383*4882a593Smuzhiyun 	int ret;
384*4882a593Smuzhiyun 
385*4882a593Smuzhiyun 	/* Disable the path */
386*4882a593Smuzhiyun 	ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
387*4882a593Smuzhiyun 	if (ret)
388*4882a593Smuzhiyun 		return ret;
389*4882a593Smuzhiyun 
390*4882a593Smuzhiyun 	/* Already disabled */
391*4882a593Smuzhiyun 	if (!hop.enable)
392*4882a593Smuzhiyun 		return 0;
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	hop.enable = 0;
395*4882a593Smuzhiyun 
396*4882a593Smuzhiyun 	ret = tb_port_write(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
397*4882a593Smuzhiyun 	if (ret)
398*4882a593Smuzhiyun 		return ret;
399*4882a593Smuzhiyun 
400*4882a593Smuzhiyun 	/* Wait until it is drained */
401*4882a593Smuzhiyun 	timeout = ktime_add_ms(ktime_get(), 500);
402*4882a593Smuzhiyun 	do {
403*4882a593Smuzhiyun 		ret = tb_port_read(port, &hop, TB_CFG_HOPS, 2 * hop_index, 2);
404*4882a593Smuzhiyun 		if (ret)
405*4882a593Smuzhiyun 			return ret;
406*4882a593Smuzhiyun 
407*4882a593Smuzhiyun 		if (!hop.pending) {
408*4882a593Smuzhiyun 			if (clear_fc) {
409*4882a593Smuzhiyun 				/* Clear flow control */
410*4882a593Smuzhiyun 				hop.ingress_fc = 0;
411*4882a593Smuzhiyun 				hop.egress_fc = 0;
412*4882a593Smuzhiyun 				hop.ingress_shared_buffer = 0;
413*4882a593Smuzhiyun 				hop.egress_shared_buffer = 0;
414*4882a593Smuzhiyun 
415*4882a593Smuzhiyun 				return tb_port_write(port, &hop, TB_CFG_HOPS,
416*4882a593Smuzhiyun 						     2 * hop_index, 2);
417*4882a593Smuzhiyun 			}
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 			return 0;
420*4882a593Smuzhiyun 		}
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 		usleep_range(10, 20);
423*4882a593Smuzhiyun 	} while (ktime_before(ktime_get(), timeout));
424*4882a593Smuzhiyun 
425*4882a593Smuzhiyun 	return -ETIMEDOUT;
426*4882a593Smuzhiyun }
427*4882a593Smuzhiyun 
__tb_path_deactivate_hops(struct tb_path * path,int first_hop)428*4882a593Smuzhiyun static void __tb_path_deactivate_hops(struct tb_path *path, int first_hop)
429*4882a593Smuzhiyun {
430*4882a593Smuzhiyun 	int i, res;
431*4882a593Smuzhiyun 
432*4882a593Smuzhiyun 	for (i = first_hop; i < path->path_length; i++) {
433*4882a593Smuzhiyun 		res = __tb_path_deactivate_hop(path->hops[i].in_port,
434*4882a593Smuzhiyun 					       path->hops[i].in_hop_index,
435*4882a593Smuzhiyun 					       path->clear_fc);
436*4882a593Smuzhiyun 		if (res && res != -ENODEV)
437*4882a593Smuzhiyun 			tb_port_warn(path->hops[i].in_port,
438*4882a593Smuzhiyun 				     "hop deactivation failed for hop %d, index %d\n",
439*4882a593Smuzhiyun 				     i, path->hops[i].in_hop_index);
440*4882a593Smuzhiyun 	}
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun 
tb_path_deactivate(struct tb_path * path)443*4882a593Smuzhiyun void tb_path_deactivate(struct tb_path *path)
444*4882a593Smuzhiyun {
445*4882a593Smuzhiyun 	if (!path->activated) {
446*4882a593Smuzhiyun 		tb_WARN(path->tb, "trying to deactivate an inactive path\n");
447*4882a593Smuzhiyun 		return;
448*4882a593Smuzhiyun 	}
449*4882a593Smuzhiyun 	tb_dbg(path->tb,
450*4882a593Smuzhiyun 	       "deactivating %s path from %llx:%x to %llx:%x\n",
451*4882a593Smuzhiyun 	       path->name, tb_route(path->hops[0].in_port->sw),
452*4882a593Smuzhiyun 	       path->hops[0].in_port->port,
453*4882a593Smuzhiyun 	       tb_route(path->hops[path->path_length - 1].out_port->sw),
454*4882a593Smuzhiyun 	       path->hops[path->path_length - 1].out_port->port);
455*4882a593Smuzhiyun 	__tb_path_deactivate_hops(path, 0);
456*4882a593Smuzhiyun 	__tb_path_deallocate_nfc(path, 0);
457*4882a593Smuzhiyun 	path->activated = false;
458*4882a593Smuzhiyun }
459*4882a593Smuzhiyun 
460*4882a593Smuzhiyun /**
461*4882a593Smuzhiyun  * tb_path_activate() - activate a path
462*4882a593Smuzhiyun  *
463*4882a593Smuzhiyun  * Activate a path starting with the last hop and iterating backwards. The
464*4882a593Smuzhiyun  * caller must fill path->hops before calling tb_path_activate().
465*4882a593Smuzhiyun  *
466*4882a593Smuzhiyun  * Return: Returns 0 on success or an error code on failure.
467*4882a593Smuzhiyun  */
tb_path_activate(struct tb_path * path)468*4882a593Smuzhiyun int tb_path_activate(struct tb_path *path)
469*4882a593Smuzhiyun {
470*4882a593Smuzhiyun 	int i, res;
471*4882a593Smuzhiyun 	enum tb_path_port out_mask, in_mask;
472*4882a593Smuzhiyun 	if (path->activated) {
473*4882a593Smuzhiyun 		tb_WARN(path->tb, "trying to activate already activated path\n");
474*4882a593Smuzhiyun 		return -EINVAL;
475*4882a593Smuzhiyun 	}
476*4882a593Smuzhiyun 
477*4882a593Smuzhiyun 	tb_dbg(path->tb,
478*4882a593Smuzhiyun 	       "activating %s path from %llx:%x to %llx:%x\n",
479*4882a593Smuzhiyun 	       path->name, tb_route(path->hops[0].in_port->sw),
480*4882a593Smuzhiyun 	       path->hops[0].in_port->port,
481*4882a593Smuzhiyun 	       tb_route(path->hops[path->path_length - 1].out_port->sw),
482*4882a593Smuzhiyun 	       path->hops[path->path_length - 1].out_port->port);
483*4882a593Smuzhiyun 
484*4882a593Smuzhiyun 	/* Clear counters. */
485*4882a593Smuzhiyun 	for (i = path->path_length - 1; i >= 0; i--) {
486*4882a593Smuzhiyun 		if (path->hops[i].in_counter_index == -1)
487*4882a593Smuzhiyun 			continue;
488*4882a593Smuzhiyun 		res = tb_port_clear_counter(path->hops[i].in_port,
489*4882a593Smuzhiyun 					    path->hops[i].in_counter_index);
490*4882a593Smuzhiyun 		if (res)
491*4882a593Smuzhiyun 			goto err;
492*4882a593Smuzhiyun 	}
493*4882a593Smuzhiyun 
494*4882a593Smuzhiyun 	/* Add non flow controlled credits. */
495*4882a593Smuzhiyun 	for (i = path->path_length - 1; i >= 0; i--) {
496*4882a593Smuzhiyun 		res = tb_port_add_nfc_credits(path->hops[i].in_port,
497*4882a593Smuzhiyun 					      path->nfc_credits);
498*4882a593Smuzhiyun 		if (res) {
499*4882a593Smuzhiyun 			__tb_path_deallocate_nfc(path, i);
500*4882a593Smuzhiyun 			goto err;
501*4882a593Smuzhiyun 		}
502*4882a593Smuzhiyun 	}
503*4882a593Smuzhiyun 
504*4882a593Smuzhiyun 	/* Activate hops. */
505*4882a593Smuzhiyun 	for (i = path->path_length - 1; i >= 0; i--) {
506*4882a593Smuzhiyun 		struct tb_regs_hop hop = { 0 };
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun 		/* If it is left active deactivate it first */
509*4882a593Smuzhiyun 		__tb_path_deactivate_hop(path->hops[i].in_port,
510*4882a593Smuzhiyun 				path->hops[i].in_hop_index, path->clear_fc);
511*4882a593Smuzhiyun 
512*4882a593Smuzhiyun 		/* dword 0 */
513*4882a593Smuzhiyun 		hop.next_hop = path->hops[i].next_hop_index;
514*4882a593Smuzhiyun 		hop.out_port = path->hops[i].out_port->port;
515*4882a593Smuzhiyun 		hop.initial_credits = path->hops[i].initial_credits;
516*4882a593Smuzhiyun 		hop.unknown1 = 0;
517*4882a593Smuzhiyun 		hop.enable = 1;
518*4882a593Smuzhiyun 
519*4882a593Smuzhiyun 		/* dword 1 */
520*4882a593Smuzhiyun 		out_mask = (i == path->path_length - 1) ?
521*4882a593Smuzhiyun 				TB_PATH_DESTINATION : TB_PATH_INTERNAL;
522*4882a593Smuzhiyun 		in_mask = (i == 0) ? TB_PATH_SOURCE : TB_PATH_INTERNAL;
523*4882a593Smuzhiyun 		hop.weight = path->weight;
524*4882a593Smuzhiyun 		hop.unknown2 = 0;
525*4882a593Smuzhiyun 		hop.priority = path->priority;
526*4882a593Smuzhiyun 		hop.drop_packages = path->drop_packages;
527*4882a593Smuzhiyun 		hop.counter = path->hops[i].in_counter_index;
528*4882a593Smuzhiyun 		hop.counter_enable = path->hops[i].in_counter_index != -1;
529*4882a593Smuzhiyun 		hop.ingress_fc = path->ingress_fc_enable & in_mask;
530*4882a593Smuzhiyun 		hop.egress_fc = path->egress_fc_enable & out_mask;
531*4882a593Smuzhiyun 		hop.ingress_shared_buffer = path->ingress_shared_buffer
532*4882a593Smuzhiyun 					    & in_mask;
533*4882a593Smuzhiyun 		hop.egress_shared_buffer = path->egress_shared_buffer
534*4882a593Smuzhiyun 					    & out_mask;
535*4882a593Smuzhiyun 		hop.unknown3 = 0;
536*4882a593Smuzhiyun 
537*4882a593Smuzhiyun 		tb_port_dbg(path->hops[i].in_port, "Writing hop %d\n", i);
538*4882a593Smuzhiyun 		tb_dump_hop(&path->hops[i], &hop);
539*4882a593Smuzhiyun 		res = tb_port_write(path->hops[i].in_port, &hop, TB_CFG_HOPS,
540*4882a593Smuzhiyun 				    2 * path->hops[i].in_hop_index, 2);
541*4882a593Smuzhiyun 		if (res) {
542*4882a593Smuzhiyun 			__tb_path_deactivate_hops(path, i);
543*4882a593Smuzhiyun 			__tb_path_deallocate_nfc(path, 0);
544*4882a593Smuzhiyun 			goto err;
545*4882a593Smuzhiyun 		}
546*4882a593Smuzhiyun 	}
547*4882a593Smuzhiyun 	path->activated = true;
548*4882a593Smuzhiyun 	tb_dbg(path->tb, "path activation complete\n");
549*4882a593Smuzhiyun 	return 0;
550*4882a593Smuzhiyun err:
551*4882a593Smuzhiyun 	tb_WARN(path->tb, "path activation failed\n");
552*4882a593Smuzhiyun 	return res;
553*4882a593Smuzhiyun }
554*4882a593Smuzhiyun 
555*4882a593Smuzhiyun /**
556*4882a593Smuzhiyun  * tb_path_is_invalid() - check whether any ports on the path are invalid
557*4882a593Smuzhiyun  *
558*4882a593Smuzhiyun  * Return: Returns true if the path is invalid, false otherwise.
559*4882a593Smuzhiyun  */
tb_path_is_invalid(struct tb_path * path)560*4882a593Smuzhiyun bool tb_path_is_invalid(struct tb_path *path)
561*4882a593Smuzhiyun {
562*4882a593Smuzhiyun 	int i = 0;
563*4882a593Smuzhiyun 	for (i = 0; i < path->path_length; i++) {
564*4882a593Smuzhiyun 		if (path->hops[i].in_port->sw->is_unplugged)
565*4882a593Smuzhiyun 			return true;
566*4882a593Smuzhiyun 		if (path->hops[i].out_port->sw->is_unplugged)
567*4882a593Smuzhiyun 			return true;
568*4882a593Smuzhiyun 	}
569*4882a593Smuzhiyun 	return false;
570*4882a593Smuzhiyun }
571*4882a593Smuzhiyun 
572*4882a593Smuzhiyun /**
573*4882a593Smuzhiyun  * tb_path_port_on_path() - Does the path go through certain port
574*4882a593Smuzhiyun  * @path: Path to check
575*4882a593Smuzhiyun  * @port: Switch to check
576*4882a593Smuzhiyun  *
577*4882a593Smuzhiyun  * Goes over all hops on path and checks if @port is any of them.
578*4882a593Smuzhiyun  * Direction does not matter.
579*4882a593Smuzhiyun  */
tb_path_port_on_path(const struct tb_path * path,const struct tb_port * port)580*4882a593Smuzhiyun bool tb_path_port_on_path(const struct tb_path *path, const struct tb_port *port)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun 	int i;
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	for (i = 0; i < path->path_length; i++) {
585*4882a593Smuzhiyun 		if (path->hops[i].in_port == port ||
586*4882a593Smuzhiyun 		    path->hops[i].out_port == port)
587*4882a593Smuzhiyun 			return true;
588*4882a593Smuzhiyun 	}
589*4882a593Smuzhiyun 
590*4882a593Smuzhiyun 	return false;
591*4882a593Smuzhiyun }
592