xref: /freebsd/sys/contrib/dev/iwlwifi/fw/notif-wait.c (revision bfcc09ddd422c95a1a2e4e794b63ee54c4902398)
1*bfcc09ddSBjoern A. Zeeb // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2*bfcc09ddSBjoern A. Zeeb /*
3*bfcc09ddSBjoern A. Zeeb  * Copyright (C) 2005-2014, 2021 Intel Corporation
4*bfcc09ddSBjoern A. Zeeb  * Copyright (C) 2015-2017 Intel Deutschland GmbH
5*bfcc09ddSBjoern A. Zeeb  */
6*bfcc09ddSBjoern A. Zeeb #include <linux/sched.h>
7*bfcc09ddSBjoern A. Zeeb #include <linux/export.h>
8*bfcc09ddSBjoern A. Zeeb 
9*bfcc09ddSBjoern A. Zeeb #include "iwl-drv.h"
10*bfcc09ddSBjoern A. Zeeb #include "notif-wait.h"
11*bfcc09ddSBjoern A. Zeeb 
12*bfcc09ddSBjoern A. Zeeb 
iwl_notification_wait_init(struct iwl_notif_wait_data * notif_wait)13*bfcc09ddSBjoern A. Zeeb void iwl_notification_wait_init(struct iwl_notif_wait_data *notif_wait)
14*bfcc09ddSBjoern A. Zeeb {
15*bfcc09ddSBjoern A. Zeeb 	spin_lock_init(&notif_wait->notif_wait_lock);
16*bfcc09ddSBjoern A. Zeeb 	INIT_LIST_HEAD(&notif_wait->notif_waits);
17*bfcc09ddSBjoern A. Zeeb 	init_waitqueue_head(&notif_wait->notif_waitq);
18*bfcc09ddSBjoern A. Zeeb }
19*bfcc09ddSBjoern A. Zeeb IWL_EXPORT_SYMBOL(iwl_notification_wait_init);
20*bfcc09ddSBjoern A. Zeeb 
iwl_notification_wait(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt)21*bfcc09ddSBjoern A. Zeeb bool iwl_notification_wait(struct iwl_notif_wait_data *notif_wait,
22*bfcc09ddSBjoern A. Zeeb 			   struct iwl_rx_packet *pkt)
23*bfcc09ddSBjoern A. Zeeb {
24*bfcc09ddSBjoern A. Zeeb 	bool triggered = false;
25*bfcc09ddSBjoern A. Zeeb 
26*bfcc09ddSBjoern A. Zeeb 	if (!list_empty(&notif_wait->notif_waits)) {
27*bfcc09ddSBjoern A. Zeeb 		struct iwl_notification_wait *w;
28*bfcc09ddSBjoern A. Zeeb 
29*bfcc09ddSBjoern A. Zeeb 		spin_lock_bh(&notif_wait->notif_wait_lock);
30*bfcc09ddSBjoern A. Zeeb 		list_for_each_entry(w, &notif_wait->notif_waits, list) {
31*bfcc09ddSBjoern A. Zeeb 			int i;
32*bfcc09ddSBjoern A. Zeeb 			bool found = false;
33*bfcc09ddSBjoern A. Zeeb 
34*bfcc09ddSBjoern A. Zeeb 			/*
35*bfcc09ddSBjoern A. Zeeb 			 * If it already finished (triggered) or has been
36*bfcc09ddSBjoern A. Zeeb 			 * aborted then don't evaluate it again to avoid races,
37*bfcc09ddSBjoern A. Zeeb 			 * Otherwise the function could be called again even
38*bfcc09ddSBjoern A. Zeeb 			 * though it returned true before
39*bfcc09ddSBjoern A. Zeeb 			 */
40*bfcc09ddSBjoern A. Zeeb 			if (w->triggered || w->aborted)
41*bfcc09ddSBjoern A. Zeeb 				continue;
42*bfcc09ddSBjoern A. Zeeb 
43*bfcc09ddSBjoern A. Zeeb 			for (i = 0; i < w->n_cmds; i++) {
44*bfcc09ddSBjoern A. Zeeb 				u16 rec_id = WIDE_ID(pkt->hdr.group_id,
45*bfcc09ddSBjoern A. Zeeb 						     pkt->hdr.cmd);
46*bfcc09ddSBjoern A. Zeeb 
47*bfcc09ddSBjoern A. Zeeb 				if (w->cmds[i] == rec_id ||
48*bfcc09ddSBjoern A. Zeeb 				    (!iwl_cmd_groupid(w->cmds[i]) &&
49*bfcc09ddSBjoern A. Zeeb 				     DEF_ID(w->cmds[i]) == rec_id)) {
50*bfcc09ddSBjoern A. Zeeb 					found = true;
51*bfcc09ddSBjoern A. Zeeb 					break;
52*bfcc09ddSBjoern A. Zeeb 				}
53*bfcc09ddSBjoern A. Zeeb 			}
54*bfcc09ddSBjoern A. Zeeb 			if (!found)
55*bfcc09ddSBjoern A. Zeeb 				continue;
56*bfcc09ddSBjoern A. Zeeb 
57*bfcc09ddSBjoern A. Zeeb 			if (!w->fn || w->fn(notif_wait, pkt, w->fn_data)) {
58*bfcc09ddSBjoern A. Zeeb 				w->triggered = true;
59*bfcc09ddSBjoern A. Zeeb 				triggered = true;
60*bfcc09ddSBjoern A. Zeeb 			}
61*bfcc09ddSBjoern A. Zeeb 		}
62*bfcc09ddSBjoern A. Zeeb 		spin_unlock_bh(&notif_wait->notif_wait_lock);
63*bfcc09ddSBjoern A. Zeeb 	}
64*bfcc09ddSBjoern A. Zeeb 
65*bfcc09ddSBjoern A. Zeeb 	return triggered;
66*bfcc09ddSBjoern A. Zeeb }
67*bfcc09ddSBjoern A. Zeeb IWL_EXPORT_SYMBOL(iwl_notification_wait);
68*bfcc09ddSBjoern A. Zeeb 
iwl_abort_notification_waits(struct iwl_notif_wait_data * notif_wait)69*bfcc09ddSBjoern A. Zeeb void iwl_abort_notification_waits(struct iwl_notif_wait_data *notif_wait)
70*bfcc09ddSBjoern A. Zeeb {
71*bfcc09ddSBjoern A. Zeeb 	struct iwl_notification_wait *wait_entry;
72*bfcc09ddSBjoern A. Zeeb 
73*bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&notif_wait->notif_wait_lock);
74*bfcc09ddSBjoern A. Zeeb 	list_for_each_entry(wait_entry, &notif_wait->notif_waits, list)
75*bfcc09ddSBjoern A. Zeeb 		wait_entry->aborted = true;
76*bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&notif_wait->notif_wait_lock);
77*bfcc09ddSBjoern A. Zeeb 
78*bfcc09ddSBjoern A. Zeeb 	wake_up_all(&notif_wait->notif_waitq);
79*bfcc09ddSBjoern A. Zeeb }
80*bfcc09ddSBjoern A. Zeeb IWL_EXPORT_SYMBOL(iwl_abort_notification_waits);
81*bfcc09ddSBjoern A. Zeeb 
82*bfcc09ddSBjoern A. Zeeb void
iwl_init_notification_wait(struct iwl_notif_wait_data * notif_wait,struct iwl_notification_wait * wait_entry,const u16 * cmds,int n_cmds,bool (* fn)(struct iwl_notif_wait_data * notif_wait,struct iwl_rx_packet * pkt,void * data),void * fn_data)83*bfcc09ddSBjoern A. Zeeb iwl_init_notification_wait(struct iwl_notif_wait_data *notif_wait,
84*bfcc09ddSBjoern A. Zeeb 			   struct iwl_notification_wait *wait_entry,
85*bfcc09ddSBjoern A. Zeeb 			   const u16 *cmds, int n_cmds,
86*bfcc09ddSBjoern A. Zeeb 			   bool (*fn)(struct iwl_notif_wait_data *notif_wait,
87*bfcc09ddSBjoern A. Zeeb 				      struct iwl_rx_packet *pkt, void *data),
88*bfcc09ddSBjoern A. Zeeb 			   void *fn_data)
89*bfcc09ddSBjoern A. Zeeb {
90*bfcc09ddSBjoern A. Zeeb 	if (WARN_ON(n_cmds > MAX_NOTIF_CMDS))
91*bfcc09ddSBjoern A. Zeeb 		n_cmds = MAX_NOTIF_CMDS;
92*bfcc09ddSBjoern A. Zeeb 
93*bfcc09ddSBjoern A. Zeeb 	wait_entry->fn = fn;
94*bfcc09ddSBjoern A. Zeeb 	wait_entry->fn_data = fn_data;
95*bfcc09ddSBjoern A. Zeeb 	wait_entry->n_cmds = n_cmds;
96*bfcc09ddSBjoern A. Zeeb 	memcpy(wait_entry->cmds, cmds, n_cmds * sizeof(u16));
97*bfcc09ddSBjoern A. Zeeb 	wait_entry->triggered = false;
98*bfcc09ddSBjoern A. Zeeb 	wait_entry->aborted = false;
99*bfcc09ddSBjoern A. Zeeb 
100*bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&notif_wait->notif_wait_lock);
101*bfcc09ddSBjoern A. Zeeb 	list_add(&wait_entry->list, &notif_wait->notif_waits);
102*bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&notif_wait->notif_wait_lock);
103*bfcc09ddSBjoern A. Zeeb }
104*bfcc09ddSBjoern A. Zeeb IWL_EXPORT_SYMBOL(iwl_init_notification_wait);
105*bfcc09ddSBjoern A. Zeeb 
iwl_remove_notification(struct iwl_notif_wait_data * notif_wait,struct iwl_notification_wait * wait_entry)106*bfcc09ddSBjoern A. Zeeb void iwl_remove_notification(struct iwl_notif_wait_data *notif_wait,
107*bfcc09ddSBjoern A. Zeeb 			     struct iwl_notification_wait *wait_entry)
108*bfcc09ddSBjoern A. Zeeb {
109*bfcc09ddSBjoern A. Zeeb 	spin_lock_bh(&notif_wait->notif_wait_lock);
110*bfcc09ddSBjoern A. Zeeb 	list_del(&wait_entry->list);
111*bfcc09ddSBjoern A. Zeeb 	spin_unlock_bh(&notif_wait->notif_wait_lock);
112*bfcc09ddSBjoern A. Zeeb }
113*bfcc09ddSBjoern A. Zeeb IWL_EXPORT_SYMBOL(iwl_remove_notification);
114*bfcc09ddSBjoern A. Zeeb 
iwl_wait_notification(struct iwl_notif_wait_data * notif_wait,struct iwl_notification_wait * wait_entry,unsigned long timeout)115*bfcc09ddSBjoern A. Zeeb int iwl_wait_notification(struct iwl_notif_wait_data *notif_wait,
116*bfcc09ddSBjoern A. Zeeb 			  struct iwl_notification_wait *wait_entry,
117*bfcc09ddSBjoern A. Zeeb 			  unsigned long timeout)
118*bfcc09ddSBjoern A. Zeeb {
119*bfcc09ddSBjoern A. Zeeb 	int ret;
120*bfcc09ddSBjoern A. Zeeb 
121*bfcc09ddSBjoern A. Zeeb 	ret = wait_event_timeout(notif_wait->notif_waitq,
122*bfcc09ddSBjoern A. Zeeb 				 wait_entry->triggered || wait_entry->aborted,
123*bfcc09ddSBjoern A. Zeeb 				 timeout);
124*bfcc09ddSBjoern A. Zeeb 
125*bfcc09ddSBjoern A. Zeeb 	iwl_remove_notification(notif_wait, wait_entry);
126*bfcc09ddSBjoern A. Zeeb 
127*bfcc09ddSBjoern A. Zeeb 	if (wait_entry->aborted)
128*bfcc09ddSBjoern A. Zeeb 		return -EIO;
129*bfcc09ddSBjoern A. Zeeb 
130*bfcc09ddSBjoern A. Zeeb 	/* return value is always >= 0 */
131*bfcc09ddSBjoern A. Zeeb 	if (ret <= 0)
132*bfcc09ddSBjoern A. Zeeb 		return -ETIMEDOUT;
133*bfcc09ddSBjoern A. Zeeb 	return 0;
134*bfcc09ddSBjoern A. Zeeb }
135*bfcc09ddSBjoern A. Zeeb IWL_EXPORT_SYMBOL(iwl_wait_notification);
136