1
2
3
4
5
6
7
8
9
10#include <linux/spinlock.h>
11#include <linux/jiffies.h>
12#include <linux/string.h>
13#include <linux/errno.h>
14#include <linux/slab.h>
15
16#include "zfcp_diag.h"
17#include "zfcp_ext.h"
18#include "zfcp_def.h"
19
20static DECLARE_WAIT_QUEUE_HEAD(__zfcp_diag_publish_wait);
21
22
23
24
25
26
27
28
29
30
31
32
33
34int zfcp_diag_adapter_setup(struct zfcp_adapter *const adapter)
35{
36 struct zfcp_diag_adapter *diag;
37 struct zfcp_diag_header *hdr;
38
39 diag = kzalloc(sizeof(*diag), GFP_KERNEL);
40 if (diag == NULL)
41 return -ENOMEM;
42
43 diag->max_age = (5 * 1000);
44
45
46 hdr = &diag->port_data.header;
47
48 spin_lock_init(&hdr->access_lock);
49 hdr->buffer = &diag->port_data.data;
50 hdr->buffer_size = sizeof(diag->port_data.data);
51
52 hdr->timestamp = jiffies - msecs_to_jiffies(diag->max_age);
53
54
55 hdr = &diag->config_data.header;
56
57 spin_lock_init(&hdr->access_lock);
58 hdr->buffer = &diag->config_data.data;
59 hdr->buffer_size = sizeof(diag->config_data.data);
60
61 hdr->timestamp = jiffies - msecs_to_jiffies(diag->max_age);
62
63 adapter->diagnostics = diag;
64 return 0;
65}
66
67
68
69
70
71
72
73
74void zfcp_diag_adapter_free(struct zfcp_adapter *const adapter)
75{
76 kfree(adapter->diagnostics);
77 adapter->diagnostics = NULL;
78}
79
80
81
82
83
84
85
86void zfcp_diag_update_xdata(struct zfcp_diag_header *const hdr,
87 const void *const data, const bool incomplete)
88{
89 const unsigned long capture_timestamp = jiffies;
90 unsigned long flags;
91
92 spin_lock_irqsave(&hdr->access_lock, flags);
93
94
95 if (!time_after_eq(capture_timestamp, hdr->timestamp))
96 goto out;
97
98 hdr->timestamp = capture_timestamp;
99 hdr->incomplete = incomplete;
100 memcpy(hdr->buffer, data, hdr->buffer_size);
101out:
102 spin_unlock_irqrestore(&hdr->access_lock, flags);
103}
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122int zfcp_diag_update_port_data_buffer(struct zfcp_adapter *const adapter)
123{
124 int rc;
125
126 rc = zfcp_fsf_exchange_port_data_sync(adapter->qdio, NULL);
127 if (rc == -EAGAIN)
128 rc = 0;
129
130
131
132 return rc;
133}
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152int zfcp_diag_update_config_data_buffer(struct zfcp_adapter *const adapter)
153{
154 int rc;
155
156 rc = zfcp_fsf_exchange_config_data_sync(adapter->qdio, NULL);
157 if (rc == -EAGAIN)
158 rc = 0;
159
160
161
162 return rc;
163}
164
165static int __zfcp_diag_update_buffer(struct zfcp_adapter *const adapter,
166 struct zfcp_diag_header *const hdr,
167 zfcp_diag_update_buffer_func buffer_update,
168 unsigned long *const flags)
169 __must_hold(hdr->access_lock)
170{
171 int rc;
172
173 if (hdr->updating == 1) {
174 rc = wait_event_interruptible_lock_irq(__zfcp_diag_publish_wait,
175 hdr->updating == 0,
176 hdr->access_lock);
177 rc = (rc == 0 ? -EAGAIN : -EINTR);
178 } else {
179 hdr->updating = 1;
180 spin_unlock_irqrestore(&hdr->access_lock, *flags);
181
182
183 rc = buffer_update(adapter);
184
185 spin_lock_irqsave(&hdr->access_lock, *flags);
186 hdr->updating = 0;
187
188
189
190
191
192 wake_up_interruptible_all(&__zfcp_diag_publish_wait);
193 }
194
195 return rc;
196}
197
198static bool
199__zfcp_diag_test_buffer_age_isfresh(const struct zfcp_diag_adapter *const diag,
200 const struct zfcp_diag_header *const hdr)
201 __must_hold(hdr->access_lock)
202{
203 const unsigned long now = jiffies;
204
205
206
207
208
209 if (!time_after_eq(now, hdr->timestamp))
210 return false;
211
212 if (jiffies_to_msecs(now - hdr->timestamp) >= diag->max_age)
213 return false;
214
215 return true;
216}
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242int zfcp_diag_update_buffer_limited(struct zfcp_adapter *const adapter,
243 struct zfcp_diag_header *const hdr,
244 zfcp_diag_update_buffer_func buffer_update)
245{
246 unsigned long flags;
247 int rc;
248
249 spin_lock_irqsave(&hdr->access_lock, flags);
250
251 for (rc = 0;
252 !__zfcp_diag_test_buffer_age_isfresh(adapter->diagnostics, hdr);
253 rc = 0) {
254 rc = __zfcp_diag_update_buffer(adapter, hdr, buffer_update,
255 &flags);
256 if (rc != -EAGAIN)
257 break;
258 }
259
260 spin_unlock_irqrestore(&hdr->access_lock, flags);
261
262 return rc;
263}
264