1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * SSH request transport layer.
4  *
5  * Copyright (C) 2019-2020 Maximilian Luz <[email protected]>
6  */
7 
8 #include <asm/unaligned.h>
9 #include <linux/atomic.h>
10 #include <linux/completion.h>
11 #include <linux/ktime.h>
12 #include <linux/limits.h>
13 #include <linux/list.h>
14 #include <linux/slab.h>
15 #include <linux/spinlock.h>
16 #include <linux/types.h>
17 #include <linux/workqueue.h>
18 
19 #include <linux/surface_aggregator/serial_hub.h>
20 #include <linux/surface_aggregator/controller.h>
21 
22 #include "ssh_packet_layer.h"
23 #include "ssh_request_layer.h"
24 
25 #include "trace.h"
26 
27 /*
28  * SSH_RTL_REQUEST_TIMEOUT - Request timeout.
29  *
30  * Timeout as ktime_t delta for request responses. If we have not received a
31  * response in this time-frame after finishing the underlying packet
32  * transmission, the request will be completed with %-ETIMEDOUT as status
33  * code.
34  */
35 #define SSH_RTL_REQUEST_TIMEOUT			ms_to_ktime(3000)
36 
37 /*
38  * SSH_RTL_REQUEST_TIMEOUT_RESOLUTION - Request timeout granularity.
39  *
40  * Time-resolution for timeouts. Should be larger than one jiffy to avoid
41  * direct re-scheduling of reaper work_struct.
42  */
43 #define SSH_RTL_REQUEST_TIMEOUT_RESOLUTION	ms_to_ktime(max(2000 / HZ, 50))
44 
45 /*
46  * SSH_RTL_MAX_PENDING - Maximum number of pending requests.
47  *
48  * Maximum number of requests concurrently waiting to be completed (i.e.
49  * waiting for the corresponding packet transmission to finish if they don't
50  * have a response or waiting for a response if they have one).
51  */
52 #define SSH_RTL_MAX_PENDING		3
53 
54 /*
55  * SSH_RTL_TX_BATCH - Maximum number of requests processed per work execution.
56  * Used to prevent livelocking of the workqueue. Value chosen via educated
57  * guess, may be adjusted.
58  */
59 #define SSH_RTL_TX_BATCH		10
60 
61 static u16 ssh_request_get_rqid(struct ssh_request *rqst)
62 {
63 	return get_unaligned_le16(rqst->packet.data.ptr
64 				  + SSH_MSGOFFSET_COMMAND(rqid));
65 }
66 
67 static u32 ssh_request_get_rqid_safe(struct ssh_request *rqst)
68 {
69 	if (!rqst->packet.data.ptr)
70 		return U32_MAX;
71 
72 	return ssh_request_get_rqid(rqst);
73 }
74 
75 static void ssh_rtl_queue_remove(struct ssh_request *rqst)
76 {
77 	struct ssh_rtl *rtl = ssh_request_rtl(rqst);
78 
79 	spin_lock(&rtl->queue.lock);
80 
81 	if (!test_and_clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &rqst->state)) {
82 		spin_unlock(&rtl->queue.lock);
83 		return;
84 	}
85 
86 	list_del(&rqst->node);
87 
88 	spin_unlock(&rtl->queue.lock);
89 	ssh_request_put(rqst);
90 }
91 
92 static bool ssh_rtl_queue_empty(struct ssh_rtl *rtl)
93 {
94 	bool empty;
95 
96 	spin_lock(&rtl->queue.lock);
97 	empty = list_empty(&rtl->queue.head);
98 	spin_unlock(&rtl->queue.lock);
99 
100 	return empty;
101 }
102 
103 static void ssh_rtl_pending_remove(struct ssh_request *rqst)
104 {
105 	struct ssh_rtl *rtl = ssh_request_rtl(rqst);
106 
107 	spin_lock(&rtl->pending.lock);
108 
109 	if (!test_and_clear_bit(SSH_REQUEST_SF_PENDING_BIT, &rqst->state)) {
110 		spin_unlock(&rtl->pending.lock);
111 		return;
112 	}
113 
114 	atomic_dec(&rtl->pending.count);
115 	list_del(&rqst->node);
116 
117 	spin_unlock(&rtl->pending.lock);
118 
119 	ssh_request_put(rqst);
120 }
121 
122 static int ssh_rtl_tx_pending_push(struct ssh_request *rqst)
123 {
124 	struct ssh_rtl *rtl = ssh_request_rtl(rqst);
125 
126 	spin_lock(&rtl->pending.lock);
127 
128 	if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state)) {
129 		spin_unlock(&rtl->pending.lock);
130 		return -EINVAL;
131 	}
132 
133 	if (test_and_set_bit(SSH_REQUEST_SF_PENDING_BIT, &rqst->state)) {
134 		spin_unlock(&rtl->pending.lock);
135 		return -EALREADY;
136 	}
137 
138 	atomic_inc(&rtl->pending.count);
139 	list_add_tail(&ssh_request_get(rqst)->node, &rtl->pending.head);
140 
141 	spin_unlock(&rtl->pending.lock);
142 	return 0;
143 }
144 
145 static void ssh_rtl_complete_with_status(struct ssh_request *rqst, int status)
146 {
147 	struct ssh_rtl *rtl = ssh_request_rtl(rqst);
148 
149 	trace_ssam_request_complete(rqst, status);
150 
151 	/* rtl/ptl may not be set if we're canceling before submitting. */
152 	rtl_dbg_cond(rtl, "rtl: completing request (rqid: %#06x, status: %d)\n",
153 		     ssh_request_get_rqid_safe(rqst), status);
154 
155 	rqst->ops->complete(rqst, NULL, NULL, status);
156 }
157 
158 static void ssh_rtl_complete_with_rsp(struct ssh_request *rqst,
159 				      const struct ssh_command *cmd,
160 				      const struct ssam_span *data)
161 {
162 	struct ssh_rtl *rtl = ssh_request_rtl(rqst);
163 
164 	trace_ssam_request_complete(rqst, 0);
165 
166 	rtl_dbg(rtl, "rtl: completing request with response (rqid: %#06x)\n",
167 		ssh_request_get_rqid(rqst));
168 
169 	rqst->ops->complete(rqst, cmd, data, 0);
170 }
171 
172 static bool ssh_rtl_tx_can_process(struct ssh_request *rqst)
173 {
174 	struct ssh_rtl *rtl = ssh_request_rtl(rqst);
175 
176 	if (test_bit(SSH_REQUEST_TY_FLUSH_BIT, &rqst->state))
177 		return !atomic_read(&rtl->pending.count);
178 
179 	return atomic_read(&rtl->pending.count) < SSH_RTL_MAX_PENDING;
180 }
181 
182 static struct ssh_request *ssh_rtl_tx_next(struct ssh_rtl *rtl)
183 {
184 	struct ssh_request *rqst = ERR_PTR(-ENOENT);
185 	struct ssh_request *p, *n;
186 
187 	spin_lock(&rtl->queue.lock);
188 
189 	/* Find first non-locked request and remove it. */
190 	list_for_each_entry_safe(p, n, &rtl->queue.head, node) {
191 		if (unlikely(test_bit(SSH_REQUEST_SF_LOCKED_BIT, &p->state)))
192 			continue;
193 
194 		if (!ssh_rtl_tx_can_process(p)) {
195 			rqst = ERR_PTR(-EBUSY);
196 			break;
197 		}
198 
199 		/* Remove from queue and mark as transmitting. */
200 		set_bit(SSH_REQUEST_SF_TRANSMITTING_BIT, &p->state);
201 		/* Ensure state never gets zero. */
202 		smp_mb__before_atomic();
203 		clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &p->state);
204 
205 		list_del(&p->node);
206 
207 		rqst = p;
208 		break;
209 	}
210 
211 	spin_unlock(&rtl->queue.lock);
212 	return rqst;
213 }
214 
215 static int ssh_rtl_tx_try_process_one(struct ssh_rtl *rtl)
216 {
217 	struct ssh_request *rqst;
218 	int status;
219 
220 	/* Get and prepare next request for transmit. */
221 	rqst = ssh_rtl_tx_next(rtl);
222 	if (IS_ERR(rqst))
223 		return PTR_ERR(rqst);
224 
225 	/* Add it to/mark it as pending. */
226 	status = ssh_rtl_tx_pending_push(rqst);
227 	if (status) {
228 		ssh_request_put(rqst);
229 		return -EAGAIN;
230 	}
231 
232 	/* Submit packet. */
233 	status = ssh_ptl_submit(&rtl->ptl, &rqst->packet);
234 	if (status == -ESHUTDOWN) {
235 		/*
236 		 * Packet has been refused due to the packet layer shutting
237 		 * down. Complete it here.
238 		 */
239 		set_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state);
240 		/*
241 		 * Note: A barrier is not required here, as there are only two
242 		 * references in the system at this point: The one that we have,
243 		 * and the other one that belongs to the pending set. Due to the
244 		 * request being marked as "transmitting", our process is the
245 		 * only one allowed to remove the pending node and change the
246 		 * state. Normally, the task would fall to the packet callback,
247 		 * but as this is a path where submission failed, this callback
248 		 * will never be executed.
249 		 */
250 
251 		ssh_rtl_pending_remove(rqst);
252 		ssh_rtl_complete_with_status(rqst, -ESHUTDOWN);
253 
254 		ssh_request_put(rqst);
255 		return -ESHUTDOWN;
256 
257 	} else if (status) {
258 		/*
259 		 * If submitting the packet failed and the packet layer isn't
260 		 * shutting down, the packet has either been submitted/queued
261 		 * before (-EALREADY, which cannot happen as we have
262 		 * guaranteed that requests cannot be re-submitted), or the
263 		 * packet was marked as locked (-EINVAL). To mark the packet
264 		 * locked at this stage, the request, and thus the packets
265 		 * itself, had to have been canceled. Simply drop the
266 		 * reference. Cancellation itself will remove it from the set
267 		 * of pending requests.
268 		 */
269 
270 		WARN_ON(status != -EINVAL);
271 
272 		ssh_request_put(rqst);
273 		return -EAGAIN;
274 	}
275 
276 	ssh_request_put(rqst);
277 	return 0;
278 }
279 
280 static bool ssh_rtl_tx_schedule(struct ssh_rtl *rtl)
281 {
282 	if (atomic_read(&rtl->pending.count) >= SSH_RTL_MAX_PENDING)
283 		return false;
284 
285 	if (ssh_rtl_queue_empty(rtl))
286 		return false;
287 
288 	return schedule_work(&rtl->tx.work);
289 }
290 
291 static void ssh_rtl_tx_work_fn(struct work_struct *work)
292 {
293 	struct ssh_rtl *rtl = to_ssh_rtl(work, tx.work);
294 	unsigned int iterations = SSH_RTL_TX_BATCH;
295 	int status;
296 
297 	/*
298 	 * Try to be nice and not block/live-lock the workqueue: Run a maximum
299 	 * of 10 tries, then re-submit if necessary. This should not be
300 	 * necessary for normal execution, but guarantee it anyway.
301 	 */
302 	do {
303 		status = ssh_rtl_tx_try_process_one(rtl);
304 		if (status == -ENOENT || status == -EBUSY)
305 			return;		/* No more requests to process. */
306 
307 		if (status == -ESHUTDOWN) {
308 			/*
309 			 * Packet system shutting down. No new packets can be
310 			 * transmitted. Return silently, the party initiating
311 			 * the shutdown should handle the rest.
312 			 */
313 			return;
314 		}
315 
316 		WARN_ON(status != 0 && status != -EAGAIN);
317 	} while (--iterations);
318 
319 	/* Out of tries, reschedule. */
320 	ssh_rtl_tx_schedule(rtl);
321 }
322 
323 /**
324  * ssh_rtl_submit() - Submit a request to the transport layer.
325  * @rtl:  The request transport layer.
326  * @rqst: The request to submit.
327  *
328  * Submits a request to the transport layer. A single request may not be
329  * submitted multiple times without reinitializing it.
330  *
331  * Return: Returns zero on success, %-EINVAL if the request type is invalid or
332  * the request has been canceled prior to submission, %-EALREADY if the
333  * request has already been submitted, or %-ESHUTDOWN in case the request
334  * transport layer has been shut down.
335  */
336 int ssh_rtl_submit(struct ssh_rtl *rtl, struct ssh_request *rqst)
337 {
338 	trace_ssam_request_submit(rqst);
339 
340 	/*
341 	 * Ensure that requests expecting a response are sequenced. If this
342 	 * invariant ever changes, see the comment in ssh_rtl_complete() on what
343 	 * is required to be changed in the code.
344 	 */
345 	if (test_bit(SSH_REQUEST_TY_HAS_RESPONSE_BIT, &rqst->state))
346 		if (!test_bit(SSH_PACKET_TY_SEQUENCED_BIT, &rqst->packet.state))
347 			return -EINVAL;
348 
349 	spin_lock(&rtl->queue.lock);
350 
351 	/*
352 	 * Try to set ptl and check if this request has already been submitted.
353 	 *
354 	 * Must be inside lock as we might run into a lost update problem
355 	 * otherwise: If this were outside of the lock, cancellation in
356 	 * ssh_rtl_cancel_nonpending() may run after we've set the ptl
357 	 * reference but before we enter the lock. In that case, we'd detect
358 	 * that the request is being added to the queue and would try to remove
359 	 * it from that, but removal might fail because it hasn't actually been
360 	 * added yet. By putting this cmpxchg in the critical section, we
361 	 * ensure that the queuing detection only triggers when we are already
362 	 * in the critical section and the remove process will wait until the
363 	 * push operation has been completed (via lock) due to that. Only then,
364 	 * we can safely try to remove it.
365 	 */
366 	if (cmpxchg(&rqst->packet.ptl, NULL, &rtl->ptl)) {
367 		spin_unlock(&rtl->queue.lock);
368 		return -EALREADY;
369 	}
370 
371 	/*
372 	 * Ensure that we set ptl reference before we continue modifying state.
373 	 * This is required for non-pending cancellation. This barrier is paired
374 	 * with the one in ssh_rtl_cancel_nonpending().
375 	 *
376 	 * By setting the ptl reference before we test for "locked", we can
377 	 * check if the "locked" test may have already run. See comments in
378 	 * ssh_rtl_cancel_nonpending() for more detail.
379 	 */
380 	smp_mb__after_atomic();
381 
382 	if (test_bit(SSH_RTL_SF_SHUTDOWN_BIT, &rtl->state)) {
383 		spin_unlock(&rtl->queue.lock);
384 		return -ESHUTDOWN;
385 	}
386 
387 	if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state)) {
388 		spin_unlock(&rtl->queue.lock);
389 		return -EINVAL;
390 	}
391 
392 	set_bit(SSH_REQUEST_SF_QUEUED_BIT, &rqst->state);
393 	list_add_tail(&ssh_request_get(rqst)->node, &rtl->queue.head);
394 
395 	spin_unlock(&rtl->queue.lock);
396 
397 	ssh_rtl_tx_schedule(rtl);
398 	return 0;
399 }
400 
401 static void ssh_rtl_timeout_reaper_mod(struct ssh_rtl *rtl, ktime_t now,
402 				       ktime_t expires)
403 {
404 	unsigned long delta = msecs_to_jiffies(ktime_ms_delta(expires, now));
405 	ktime_t aexp = ktime_add(expires, SSH_RTL_REQUEST_TIMEOUT_RESOLUTION);
406 
407 	spin_lock(&rtl->rtx_timeout.lock);
408 
409 	/* Re-adjust / schedule reaper only if it is above resolution delta. */
410 	if (ktime_before(aexp, rtl->rtx_timeout.expires)) {
411 		rtl->rtx_timeout.expires = expires;
412 		mod_delayed_work(system_wq, &rtl->rtx_timeout.reaper, delta);
413 	}
414 
415 	spin_unlock(&rtl->rtx_timeout.lock);
416 }
417 
418 static void ssh_rtl_timeout_start(struct ssh_request *rqst)
419 {
420 	struct ssh_rtl *rtl = ssh_request_rtl(rqst);
421 	ktime_t timestamp = ktime_get_coarse_boottime();
422 	ktime_t timeout = rtl->rtx_timeout.timeout;
423 
424 	if (test_bit(SSH_REQUEST_SF_LOCKED_BIT, &rqst->state))
425 		return;
426 
427 	/*
428 	 * Note: The timestamp gets set only once. This happens on the packet
429 	 * callback. All other access to it is read-only.
430 	 */
431 	WRITE_ONCE(rqst->timestamp, timestamp);
432 	/*
433 	 * Ensure timestamp is set before starting the reaper. Paired with
434 	 * implicit barrier following check on ssh_request_get_expiration() in
435 	 * ssh_rtl_timeout_reap.
436 	 */
437 	smp_mb__after_atomic();
438 
439 	ssh_rtl_timeout_reaper_mod(rtl, timestamp, timestamp + timeout);
440 }
441 
442 static void ssh_rtl_complete(struct ssh_rtl *rtl,
443 			     const struct ssh_command *command,
444 			     const struct ssam_span *command_data)
445 {
446 	struct ssh_request *r = NULL;
447 	struct ssh_request *p, *n;
448 	u16 rqid = get_unaligned_le16(&command->rqid);
449 
450 	trace_ssam_rx_response_received(command, command_data->len);
451 
452 	/*
453 	 * Get request from pending based on request ID and mark it as response
454 	 * received and locked.
455 	 */
456 	spin_lock(&rtl->pending.lock);
457 	list_for_each_entry_safe(p, n, &rtl->pending.head, node) {
458 		/* We generally expect requests to be processed in order. */
459 		if (unlikely(ssh_request_get_rqid(p) != rqid))
460 			continue;
461 
462 		/*
463 		 * Mark as "response received" and "locked" as we're going to
464 		 * complete it.
465 		 */
466 		set_bit(SSH_REQUEST_SF_LOCKED_BIT, &p->state);
467 		set_bit(SSH_REQUEST_SF_RSPRCVD_BIT, &p->state);
468 		/* Ensure state never gets zero. */
469 		smp_mb__before_atomic();
470 		clear_bit(SSH_REQUEST_SF_PENDING_BIT, &p->state);
471 
472 		atomic_dec(&rtl->pending.count);
473 		list_del(&p->node);
474 
475 		r = p;
476 		break;
477 	}
478 	spin_unlock(&rtl->pending.lock);
479 
480 	if (!r) {
481 		rtl_warn(rtl, "rtl: dropping unexpected command message (rqid = %#06x)\n",
482 			 rqid);
483 		return;
484 	}
485 
486 	/* If the request hasn't been completed yet, we will do this now. */
487 	if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state)) {
488 		ssh_request_put(r);
489 		ssh_rtl_tx_schedule(rtl);
490 		return;
491 	}
492 
493 	/*
494 	 * Make sure the request has been transmitted. In case of a sequenced
495 	 * request, we are guaranteed that the completion callback will run on
496 	 * the receiver thread directly when the ACK for the packet has been
497 	 * received. Similarly, this function is guaranteed to run on the
498 	 * receiver thread. Thus we are guaranteed that if the packet has been
499 	 * successfully transmitted and received an ACK, the transmitted flag
500 	 * has been set and is visible here.
501 	 *
502 	 * We are currently not handling unsequenced packets here, as those
503 	 * should never expect a response as ensured in ssh_rtl_submit. If this
504 	 * ever changes, one would have to test for
505 	 *
506 	 *	(r->state & (transmitting | transmitted))
507 	 *
508 	 * on unsequenced packets to determine if they could have been
509 	 * transmitted. There are no synchronization guarantees as in the
510 	 * sequenced case, since, in this case, the callback function will not
511 	 * run on the same thread. Thus an exact determination is impossible.
512 	 */
513 	if (!test_bit(SSH_REQUEST_SF_TRANSMITTED_BIT, &r->state)) {
514 		rtl_err(rtl, "rtl: received response before ACK for request (rqid = %#06x)\n",
515 			rqid);
516 
517 		/*
518 		 * NB: Timeout has already been canceled, request already been
519 		 * removed from pending and marked as locked and completed. As
520 		 * we receive a "false" response, the packet might still be
521 		 * queued though.
522 		 */
523 		ssh_rtl_queue_remove(r);
524 
525 		ssh_rtl_complete_with_status(r, -EREMOTEIO);
526 		ssh_request_put(r);
527 
528 		ssh_rtl_tx_schedule(rtl);
529 		return;
530 	}
531 
532 	/*
533 	 * NB: Timeout has already been canceled, request already been
534 	 * removed from pending and marked as locked and completed. The request
535 	 * can also not be queued any more, as it has been marked as
536 	 * transmitting and later transmitted. Thus no need to remove it from
537 	 * anywhere.
538 	 */
539 
540 	ssh_rtl_complete_with_rsp(r, command, command_data);
541 	ssh_request_put(r);
542 
543 	ssh_rtl_tx_schedule(rtl);
544 }
545 
546 static bool ssh_rtl_cancel_nonpending(struct ssh_request *r)
547 {
548 	struct ssh_rtl *rtl;
549 	unsigned long flags, fixed;
550 	bool remove;
551 
552 	/*
553 	 * Handle unsubmitted request: Try to mark the packet as locked,
554 	 * expecting the state to be zero (i.e. unsubmitted). Note that, if
555 	 * setting the state worked, we might still be adding the packet to the
556 	 * queue in a currently executing submit call. In that case, however,
557 	 * ptl reference must have been set previously, as locked is checked
558 	 * after setting ptl. Furthermore, when the ptl reference is set, the
559 	 * submission process is guaranteed to have entered the critical
560 	 * section. Thus only if we successfully locked this request and ptl is
561 	 * NULL, we have successfully removed the request, i.e. we are
562 	 * guaranteed that, due to the "locked" check in ssh_rtl_submit(), the
563 	 * packet will never be added. Otherwise, we need to try and grab it
564 	 * from the queue, where we are now guaranteed that the packet is or has
565 	 * been due to the critical section.
566 	 *
567 	 * Note that if the cmpxchg() fails, we are guaranteed that ptl has
568 	 * been set and is non-NULL, as states can only be nonzero after this
569 	 * has been set. Also note that we need to fetch the static (type)
570 	 * flags to ensure that they don't cause the cmpxchg() to fail.
571 	 */
572 	fixed = READ_ONCE(r->state) & SSH_REQUEST_FLAGS_TY_MASK;
573 	flags = cmpxchg(&r->state, fixed, SSH_REQUEST_SF_LOCKED_BIT);
574 
575 	/*
576 	 * Force correct ordering with regards to state and ptl reference access
577 	 * to safe-guard cancellation to concurrent submission against a
578 	 * lost-update problem. First try to exchange state, then also check
579 	 * ptl if that worked. This barrier is paired with the
580 	 * one in ssh_rtl_submit().
581 	 */
582 	smp_mb__after_atomic();
583 
584 	if (flags == fixed && !READ_ONCE(r->packet.ptl)) {
585 		if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
586 			return true;
587 
588 		ssh_rtl_complete_with_status(r, -ECANCELED);
589 		return true;
590 	}
591 
592 	rtl = ssh_request_rtl(r);
593 	spin_lock(&rtl->queue.lock);
594 
595 	/*
596 	 * Note: 1) Requests cannot be re-submitted. 2) If a request is
597 	 * queued, it cannot be "transmitting"/"pending" yet. Thus, if we
598 	 * successfully remove the request here, we have removed all its
599 	 * occurrences in the system.
600 	 */
601 
602 	remove = test_and_clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &r->state);
603 	if (!remove) {
604 		spin_unlock(&rtl->queue.lock);
605 		return false;
606 	}
607 
608 	set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
609 	list_del(&r->node);
610 
611 	spin_unlock(&rtl->queue.lock);
612 
613 	ssh_request_put(r);	/* Drop reference obtained from queue. */
614 
615 	if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
616 		return true;
617 
618 	ssh_rtl_complete_with_status(r, -ECANCELED);
619 	return true;
620 }
621 
622 static bool ssh_rtl_cancel_pending(struct ssh_request *r)
623 {
624 	/* If the packet is already locked, it's going to be removed shortly. */
625 	if (test_and_set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state))
626 		return true;
627 
628 	/*
629 	 * Now that we have locked the packet, we have guaranteed that it can't
630 	 * be added to the system any more. If ptl is NULL, the locked
631 	 * check in ssh_rtl_submit() has not been run and any submission,
632 	 * currently in progress or called later, won't add the packet. Thus we
633 	 * can directly complete it.
634 	 *
635 	 * The implicit memory barrier of test_and_set_bit() should be enough
636 	 * to ensure that the correct order (first lock, then check ptl) is
637 	 * ensured. This is paired with the barrier in ssh_rtl_submit().
638 	 */
639 	if (!READ_ONCE(r->packet.ptl)) {
640 		if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
641 			return true;
642 
643 		ssh_rtl_complete_with_status(r, -ECANCELED);
644 		return true;
645 	}
646 
647 	/*
648 	 * Try to cancel the packet. If the packet has not been completed yet,
649 	 * this will subsequently (and synchronously) call the completion
650 	 * callback of the packet, which will complete the request.
651 	 */
652 	ssh_ptl_cancel(&r->packet);
653 
654 	/*
655 	 * If the packet has been completed with success, i.e. has not been
656 	 * canceled by the above call, the request may not have been completed
657 	 * yet (may be waiting for a response). Check if we need to do this
658 	 * here.
659 	 */
660 	if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
661 		return true;
662 
663 	ssh_rtl_queue_remove(r);
664 	ssh_rtl_pending_remove(r);
665 	ssh_rtl_complete_with_status(r, -ECANCELED);
666 
667 	return true;
668 }
669 
670 /**
671  * ssh_rtl_cancel() - Cancel request.
672  * @rqst:    The request to cancel.
673  * @pending: Whether to also cancel pending requests.
674  *
675  * Cancels the given request. If @pending is %false, this will not cancel
676  * pending requests, i.e. requests that have already been submitted to the
677  * packet layer but not been completed yet. If @pending is %true, this will
678  * cancel the given request regardless of the state it is in.
679  *
680  * If the request has been canceled by calling this function, both completion
681  * and release callbacks of the request will be executed in a reasonable
682  * time-frame. This may happen during execution of this function, however,
683  * there is no guarantee for this. For example, a request currently
684  * transmitting will be canceled/completed only after transmission has
685  * completed, and the respective callbacks will be executed on the transmitter
686  * thread, which may happen during, but also some time after execution of the
687  * cancel function.
688  *
689  * Return: Returns %true if the given request has been canceled or completed,
690  * either by this function or prior to calling this function, %false
691  * otherwise. If @pending is %true, this function will always return %true.
692  */
693 bool ssh_rtl_cancel(struct ssh_request *rqst, bool pending)
694 {
695 	struct ssh_rtl *rtl;
696 	bool canceled;
697 
698 	if (test_and_set_bit(SSH_REQUEST_SF_CANCELED_BIT, &rqst->state))
699 		return true;
700 
701 	trace_ssam_request_cancel(rqst);
702 
703 	if (pending)
704 		canceled = ssh_rtl_cancel_pending(rqst);
705 	else
706 		canceled = ssh_rtl_cancel_nonpending(rqst);
707 
708 	/* Note: rtl may be NULL if request has not been submitted yet. */
709 	rtl = ssh_request_rtl(rqst);
710 	if (canceled && rtl)
711 		ssh_rtl_tx_schedule(rtl);
712 
713 	return canceled;
714 }
715 
716 static void ssh_rtl_packet_callback(struct ssh_packet *p, int status)
717 {
718 	struct ssh_request *r = to_ssh_request(p);
719 
720 	if (unlikely(status)) {
721 		set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
722 
723 		if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
724 			return;
725 
726 		/*
727 		 * The packet may get canceled even though it has not been
728 		 * submitted yet. The request may still be queued. Check the
729 		 * queue and remove it if necessary. As the timeout would have
730 		 * been started in this function on success, there's no need
731 		 * to cancel it here.
732 		 */
733 		ssh_rtl_queue_remove(r);
734 		ssh_rtl_pending_remove(r);
735 		ssh_rtl_complete_with_status(r, status);
736 
737 		ssh_rtl_tx_schedule(ssh_request_rtl(r));
738 		return;
739 	}
740 
741 	/* Update state: Mark as transmitted and clear transmitting. */
742 	set_bit(SSH_REQUEST_SF_TRANSMITTED_BIT, &r->state);
743 	/* Ensure state never gets zero. */
744 	smp_mb__before_atomic();
745 	clear_bit(SSH_REQUEST_SF_TRANSMITTING_BIT, &r->state);
746 
747 	/* If we expect a response, we just need to start the timeout. */
748 	if (test_bit(SSH_REQUEST_TY_HAS_RESPONSE_BIT, &r->state)) {
749 		/*
750 		 * Note: This is the only place where the timestamp gets set,
751 		 * all other access to it is read-only.
752 		 */
753 		ssh_rtl_timeout_start(r);
754 		return;
755 	}
756 
757 	/*
758 	 * If we don't expect a response, lock, remove, and complete the
759 	 * request. Note that, at this point, the request is guaranteed to have
760 	 * left the queue and no timeout has been started. Thus we only need to
761 	 * remove it from pending. If the request has already been completed (it
762 	 * may have been canceled) return.
763 	 */
764 
765 	set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
766 	if (test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
767 		return;
768 
769 	ssh_rtl_pending_remove(r);
770 	ssh_rtl_complete_with_status(r, 0);
771 
772 	ssh_rtl_tx_schedule(ssh_request_rtl(r));
773 }
774 
775 static ktime_t ssh_request_get_expiration(struct ssh_request *r, ktime_t timeout)
776 {
777 	ktime_t timestamp = READ_ONCE(r->timestamp);
778 
779 	if (timestamp != KTIME_MAX)
780 		return ktime_add(timestamp, timeout);
781 	else
782 		return KTIME_MAX;
783 }
784 
785 static void ssh_rtl_timeout_reap(struct work_struct *work)
786 {
787 	struct ssh_rtl *rtl = to_ssh_rtl(work, rtx_timeout.reaper.work);
788 	struct ssh_request *r, *n;
789 	LIST_HEAD(claimed);
790 	ktime_t now = ktime_get_coarse_boottime();
791 	ktime_t timeout = rtl->rtx_timeout.timeout;
792 	ktime_t next = KTIME_MAX;
793 
794 	trace_ssam_rtl_timeout_reap(atomic_read(&rtl->pending.count));
795 
796 	/*
797 	 * Mark reaper as "not pending". This is done before checking any
798 	 * requests to avoid lost-update type problems.
799 	 */
800 	spin_lock(&rtl->rtx_timeout.lock);
801 	rtl->rtx_timeout.expires = KTIME_MAX;
802 	spin_unlock(&rtl->rtx_timeout.lock);
803 
804 	spin_lock(&rtl->pending.lock);
805 	list_for_each_entry_safe(r, n, &rtl->pending.head, node) {
806 		ktime_t expires = ssh_request_get_expiration(r, timeout);
807 
808 		/*
809 		 * Check if the timeout hasn't expired yet. Find out next
810 		 * expiration date to be handled after this run.
811 		 */
812 		if (ktime_after(expires, now)) {
813 			next = ktime_before(expires, next) ? expires : next;
814 			continue;
815 		}
816 
817 		/* Avoid further transitions if locked. */
818 		if (test_and_set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state))
819 			continue;
820 
821 		/*
822 		 * We have now marked the packet as locked. Thus it cannot be
823 		 * added to the pending or queued lists again after we've
824 		 * removed it here. We can therefore re-use the node of this
825 		 * packet temporarily.
826 		 */
827 
828 		clear_bit(SSH_REQUEST_SF_PENDING_BIT, &r->state);
829 
830 		atomic_dec(&rtl->pending.count);
831 		list_del(&r->node);
832 
833 		list_add_tail(&r->node, &claimed);
834 	}
835 	spin_unlock(&rtl->pending.lock);
836 
837 	/* Cancel and complete the request. */
838 	list_for_each_entry_safe(r, n, &claimed, node) {
839 		trace_ssam_request_timeout(r);
840 
841 		/*
842 		 * At this point we've removed the packet from pending. This
843 		 * means that we've obtained the last (only) reference of the
844 		 * system to it. Thus we can just complete it.
845 		 */
846 		if (!test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
847 			ssh_rtl_complete_with_status(r, -ETIMEDOUT);
848 
849 		/*
850 		 * Drop the reference we've obtained by removing it from the
851 		 * pending set.
852 		 */
853 		list_del(&r->node);
854 		ssh_request_put(r);
855 	}
856 
857 	/* Ensure that the reaper doesn't run again immediately. */
858 	next = max(next, ktime_add(now, SSH_RTL_REQUEST_TIMEOUT_RESOLUTION));
859 	if (next != KTIME_MAX)
860 		ssh_rtl_timeout_reaper_mod(rtl, now, next);
861 
862 	ssh_rtl_tx_schedule(rtl);
863 }
864 
865 static void ssh_rtl_rx_event(struct ssh_rtl *rtl, const struct ssh_command *cmd,
866 			     const struct ssam_span *data)
867 {
868 	trace_ssam_rx_event_received(cmd, data->len);
869 
870 	rtl_dbg(rtl, "rtl: handling event (rqid: %#06x)\n",
871 		get_unaligned_le16(&cmd->rqid));
872 
873 	rtl->ops.handle_event(rtl, cmd, data);
874 }
875 
876 static void ssh_rtl_rx_command(struct ssh_ptl *p, const struct ssam_span *data)
877 {
878 	struct ssh_rtl *rtl = to_ssh_rtl(p, ptl);
879 	struct device *dev = &p->serdev->dev;
880 	struct ssh_command *command;
881 	struct ssam_span command_data;
882 
883 	if (sshp_parse_command(dev, data, &command, &command_data))
884 		return;
885 
886 	if (ssh_rqid_is_event(get_unaligned_le16(&command->rqid)))
887 		ssh_rtl_rx_event(rtl, command, &command_data);
888 	else
889 		ssh_rtl_complete(rtl, command, &command_data);
890 }
891 
892 static void ssh_rtl_rx_data(struct ssh_ptl *p, const struct ssam_span *data)
893 {
894 	if (!data->len) {
895 		ptl_err(p, "rtl: rx: no data frame payload\n");
896 		return;
897 	}
898 
899 	switch (data->ptr[0]) {
900 	case SSH_PLD_TYPE_CMD:
901 		ssh_rtl_rx_command(p, data);
902 		break;
903 
904 	default:
905 		ptl_err(p, "rtl: rx: unknown frame payload type (type: %#04x)\n",
906 			data->ptr[0]);
907 		break;
908 	}
909 }
910 
911 static void ssh_rtl_packet_release(struct ssh_packet *p)
912 {
913 	struct ssh_request *rqst;
914 
915 	rqst = to_ssh_request(p);
916 	rqst->ops->release(rqst);
917 }
918 
919 static const struct ssh_packet_ops ssh_rtl_packet_ops = {
920 	.complete = ssh_rtl_packet_callback,
921 	.release = ssh_rtl_packet_release,
922 };
923 
924 /**
925  * ssh_request_init() - Initialize SSH request.
926  * @rqst:  The request to initialize.
927  * @flags: Request flags, determining the type of the request.
928  * @ops:   Request operations.
929  *
930  * Initializes the given SSH request and underlying packet. Sets the message
931  * buffer pointer to %NULL and the message buffer length to zero. This buffer
932  * has to be set separately via ssh_request_set_data() before submission and
933  * must contain a valid SSH request message.
934  *
935  * Return: Returns zero on success or %-EINVAL if the given flags are invalid.
936  */
937 int ssh_request_init(struct ssh_request *rqst, enum ssam_request_flags flags,
938 		     const struct ssh_request_ops *ops)
939 {
940 	unsigned long type = BIT(SSH_PACKET_TY_BLOCKING_BIT);
941 
942 	/* Unsequenced requests cannot have a response. */
943 	if (flags & SSAM_REQUEST_UNSEQUENCED && flags & SSAM_REQUEST_HAS_RESPONSE)
944 		return -EINVAL;
945 
946 	if (!(flags & SSAM_REQUEST_UNSEQUENCED))
947 		type |= BIT(SSH_PACKET_TY_SEQUENCED_BIT);
948 
949 	ssh_packet_init(&rqst->packet, type, SSH_PACKET_PRIORITY(DATA, 0),
950 			&ssh_rtl_packet_ops);
951 
952 	INIT_LIST_HEAD(&rqst->node);
953 
954 	rqst->state = 0;
955 	if (flags & SSAM_REQUEST_HAS_RESPONSE)
956 		rqst->state |= BIT(SSH_REQUEST_TY_HAS_RESPONSE_BIT);
957 
958 	rqst->timestamp = KTIME_MAX;
959 	rqst->ops = ops;
960 
961 	return 0;
962 }
963 
964 /**
965  * ssh_rtl_init() - Initialize request transport layer.
966  * @rtl:    The request transport layer to initialize.
967  * @serdev: The underlying serial device, i.e. the lower-level transport.
968  * @ops:    Request transport layer operations.
969  *
970  * Initializes the given request transport layer and associated packet
971  * transport layer. Transmitter and receiver threads must be started
972  * separately via ssh_rtl_tx_start() and ssh_rtl_rx_start(), after the
973  * request-layer has been initialized and the lower-level serial device layer
974  * has been set up.
975  *
976  * Return: Returns zero on success and a nonzero error code on failure.
977  */
978 int ssh_rtl_init(struct ssh_rtl *rtl, struct serdev_device *serdev,
979 		 const struct ssh_rtl_ops *ops)
980 {
981 	struct ssh_ptl_ops ptl_ops;
982 	int status;
983 
984 	ptl_ops.data_received = ssh_rtl_rx_data;
985 
986 	status = ssh_ptl_init(&rtl->ptl, serdev, &ptl_ops);
987 	if (status)
988 		return status;
989 
990 	spin_lock_init(&rtl->queue.lock);
991 	INIT_LIST_HEAD(&rtl->queue.head);
992 
993 	spin_lock_init(&rtl->pending.lock);
994 	INIT_LIST_HEAD(&rtl->pending.head);
995 	atomic_set_release(&rtl->pending.count, 0);
996 
997 	INIT_WORK(&rtl->tx.work, ssh_rtl_tx_work_fn);
998 
999 	spin_lock_init(&rtl->rtx_timeout.lock);
1000 	rtl->rtx_timeout.timeout = SSH_RTL_REQUEST_TIMEOUT;
1001 	rtl->rtx_timeout.expires = KTIME_MAX;
1002 	INIT_DELAYED_WORK(&rtl->rtx_timeout.reaper, ssh_rtl_timeout_reap);
1003 
1004 	rtl->ops = *ops;
1005 
1006 	return 0;
1007 }
1008 
1009 /**
1010  * ssh_rtl_destroy() - Deinitialize request transport layer.
1011  * @rtl: The request transport layer to deinitialize.
1012  *
1013  * Deinitializes the given request transport layer and frees resources
1014  * associated with it. If receiver and/or transmitter threads have been
1015  * started, the layer must first be shut down via ssh_rtl_shutdown() before
1016  * this function can be called.
1017  */
1018 void ssh_rtl_destroy(struct ssh_rtl *rtl)
1019 {
1020 	ssh_ptl_destroy(&rtl->ptl);
1021 }
1022 
1023 /**
1024  * ssh_rtl_tx_start() - Start request transmitter and receiver.
1025  * @rtl: The request transport layer.
1026  *
1027  * Return: Returns zero on success, a negative error code on failure.
1028  */
1029 int ssh_rtl_start(struct ssh_rtl *rtl)
1030 {
1031 	int status;
1032 
1033 	status = ssh_ptl_tx_start(&rtl->ptl);
1034 	if (status)
1035 		return status;
1036 
1037 	ssh_rtl_tx_schedule(rtl);
1038 
1039 	status = ssh_ptl_rx_start(&rtl->ptl);
1040 	if (status) {
1041 		ssh_rtl_flush(rtl, msecs_to_jiffies(5000));
1042 		ssh_ptl_tx_stop(&rtl->ptl);
1043 		return status;
1044 	}
1045 
1046 	return 0;
1047 }
1048 
1049 struct ssh_flush_request {
1050 	struct ssh_request base;
1051 	struct completion completion;
1052 	int status;
1053 };
1054 
1055 static void ssh_rtl_flush_request_complete(struct ssh_request *r,
1056 					   const struct ssh_command *cmd,
1057 					   const struct ssam_span *data,
1058 					   int status)
1059 {
1060 	struct ssh_flush_request *rqst;
1061 
1062 	rqst = container_of(r, struct ssh_flush_request, base);
1063 	rqst->status = status;
1064 }
1065 
1066 static void ssh_rtl_flush_request_release(struct ssh_request *r)
1067 {
1068 	struct ssh_flush_request *rqst;
1069 
1070 	rqst = container_of(r, struct ssh_flush_request, base);
1071 	complete_all(&rqst->completion);
1072 }
1073 
1074 static const struct ssh_request_ops ssh_rtl_flush_request_ops = {
1075 	.complete = ssh_rtl_flush_request_complete,
1076 	.release = ssh_rtl_flush_request_release,
1077 };
1078 
1079 /**
1080  * ssh_rtl_flush() - Flush the request transport layer.
1081  * @rtl:     request transport layer
1082  * @timeout: timeout for the flush operation in jiffies
1083  *
1084  * Queue a special flush request and wait for its completion. This request
1085  * will be completed after all other currently queued and pending requests
1086  * have been completed. Instead of a normal data packet, this request submits
1087  * a special flush packet, meaning that upon completion, also the underlying
1088  * packet transport layer has been flushed.
1089  *
1090  * Flushing the request layer guarantees that all previously submitted
1091  * requests have been fully completed before this call returns. Additionally,
1092  * flushing blocks execution of all later submitted requests until the flush
1093  * has been completed.
1094  *
1095  * If the caller ensures that no new requests are submitted after a call to
1096  * this function, the request transport layer is guaranteed to have no
1097  * remaining requests when this call returns. The same guarantee does not hold
1098  * for the packet layer, on which control packets may still be queued after
1099  * this call.
1100  *
1101  * Return: Returns zero on success, %-ETIMEDOUT if the flush timed out and has
1102  * been canceled as a result of the timeout, or %-ESHUTDOWN if the packet
1103  * and/or request transport layer has been shut down before this call. May
1104  * also return %-EINTR if the underlying packet transmission has been
1105  * interrupted.
1106  */
1107 int ssh_rtl_flush(struct ssh_rtl *rtl, unsigned long timeout)
1108 {
1109 	const unsigned int init_flags = SSAM_REQUEST_UNSEQUENCED;
1110 	struct ssh_flush_request rqst;
1111 	int status;
1112 
1113 	ssh_request_init(&rqst.base, init_flags, &ssh_rtl_flush_request_ops);
1114 	rqst.base.packet.state |= BIT(SSH_PACKET_TY_FLUSH_BIT);
1115 	rqst.base.packet.priority = SSH_PACKET_PRIORITY(FLUSH, 0);
1116 	rqst.base.state |= BIT(SSH_REQUEST_TY_FLUSH_BIT);
1117 
1118 	init_completion(&rqst.completion);
1119 
1120 	status = ssh_rtl_submit(rtl, &rqst.base);
1121 	if (status)
1122 		return status;
1123 
1124 	ssh_request_put(&rqst.base);
1125 
1126 	if (!wait_for_completion_timeout(&rqst.completion, timeout)) {
1127 		ssh_rtl_cancel(&rqst.base, true);
1128 		wait_for_completion(&rqst.completion);
1129 	}
1130 
1131 	WARN_ON(rqst.status != 0 && rqst.status != -ECANCELED &&
1132 		rqst.status != -ESHUTDOWN && rqst.status != -EINTR);
1133 
1134 	return rqst.status == -ECANCELED ? -ETIMEDOUT : rqst.status;
1135 }
1136 
1137 /**
1138  * ssh_rtl_shutdown() - Shut down request transport layer.
1139  * @rtl: The request transport layer.
1140  *
1141  * Shuts down the request transport layer, removing and canceling all queued
1142  * and pending requests. Requests canceled by this operation will be completed
1143  * with %-ESHUTDOWN as status. Receiver and transmitter threads will be
1144  * stopped, the lower-level packet layer will be shutdown.
1145  *
1146  * As a result of this function, the transport layer will be marked as shut
1147  * down. Submission of requests after the transport layer has been shut down
1148  * will fail with %-ESHUTDOWN.
1149  */
1150 void ssh_rtl_shutdown(struct ssh_rtl *rtl)
1151 {
1152 	struct ssh_request *r, *n;
1153 	LIST_HEAD(claimed);
1154 	int pending;
1155 
1156 	set_bit(SSH_RTL_SF_SHUTDOWN_BIT, &rtl->state);
1157 	/*
1158 	 * Ensure that the layer gets marked as shut-down before actually
1159 	 * stopping it. In combination with the check in ssh_rtl_submit(),
1160 	 * this guarantees that no new requests can be added and all already
1161 	 * queued requests are properly canceled.
1162 	 */
1163 	smp_mb__after_atomic();
1164 
1165 	/* Remove requests from queue. */
1166 	spin_lock(&rtl->queue.lock);
1167 	list_for_each_entry_safe(r, n, &rtl->queue.head, node) {
1168 		set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
1169 		/* Ensure state never gets zero. */
1170 		smp_mb__before_atomic();
1171 		clear_bit(SSH_REQUEST_SF_QUEUED_BIT, &r->state);
1172 
1173 		list_del(&r->node);
1174 		list_add_tail(&r->node, &claimed);
1175 	}
1176 	spin_unlock(&rtl->queue.lock);
1177 
1178 	/*
1179 	 * We have now guaranteed that the queue is empty and no more new
1180 	 * requests can be submitted (i.e. it will stay empty). This means that
1181 	 * calling ssh_rtl_tx_schedule() will not schedule tx.work any more. So
1182 	 * we can simply call cancel_work_sync() on tx.work here and when that
1183 	 * returns, we've locked it down. This also means that after this call,
1184 	 * we don't submit any more packets to the underlying packet layer, so
1185 	 * we can also shut that down.
1186 	 */
1187 
1188 	cancel_work_sync(&rtl->tx.work);
1189 	ssh_ptl_shutdown(&rtl->ptl);
1190 	cancel_delayed_work_sync(&rtl->rtx_timeout.reaper);
1191 
1192 	/*
1193 	 * Shutting down the packet layer should also have canceled all
1194 	 * requests. Thus the pending set should be empty. Attempt to handle
1195 	 * this gracefully anyways, even though this should be dead code.
1196 	 */
1197 
1198 	pending = atomic_read(&rtl->pending.count);
1199 	if (WARN_ON(pending)) {
1200 		spin_lock(&rtl->pending.lock);
1201 		list_for_each_entry_safe(r, n, &rtl->pending.head, node) {
1202 			set_bit(SSH_REQUEST_SF_LOCKED_BIT, &r->state);
1203 			/* Ensure state never gets zero. */
1204 			smp_mb__before_atomic();
1205 			clear_bit(SSH_REQUEST_SF_PENDING_BIT, &r->state);
1206 
1207 			list_del(&r->node);
1208 			list_add_tail(&r->node, &claimed);
1209 		}
1210 		spin_unlock(&rtl->pending.lock);
1211 	}
1212 
1213 	/* Finally, cancel and complete the requests we claimed before. */
1214 	list_for_each_entry_safe(r, n, &claimed, node) {
1215 		/*
1216 		 * We need test_and_set() because we still might compete with
1217 		 * cancellation.
1218 		 */
1219 		if (!test_and_set_bit(SSH_REQUEST_SF_COMPLETED_BIT, &r->state))
1220 			ssh_rtl_complete_with_status(r, -ESHUTDOWN);
1221 
1222 		/*
1223 		 * Drop the reference we've obtained by removing it from the
1224 		 * lists.
1225 		 */
1226 		list_del(&r->node);
1227 		ssh_request_put(r);
1228 	}
1229 }
1230