1 // SPDX-License-Identifier: GPL-2.0
5 * Functions to handle diagnostics.
7 * Copyright IBM Corp. 2018
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>
16 #include "zfcp_diag.h"
20 static DECLARE_WAIT_QUEUE_HEAD(__zfcp_diag_publish_wait
);
23 * zfcp_diag_adapter_setup() - Setup storage for adapter diagnostics.
24 * @adapter: the adapter to setup diagnostics for.
26 * Creates the data-structures to store the diagnostics for an adapter. This
27 * overwrites whatever was stored before at &zfcp_adapter->diagnostics!
30 * * 0 - Everyting is OK
31 * * -ENOMEM - Could not allocate all/parts of the data-structures;
32 * &zfcp_adapter->diagnostics remains unchanged
34 int zfcp_diag_adapter_setup(struct zfcp_adapter
*const adapter
)
36 struct zfcp_diag_adapter
*diag
;
37 struct zfcp_diag_header
*hdr
;
39 diag
= kzalloc(sizeof(*diag
), GFP_KERNEL
);
43 diag
->max_age
= (5 * 1000); /* default value: 5 s */
45 /* setup header for port_data */
46 hdr
= &diag
->port_data
.header
;
48 spin_lock_init(&hdr
->access_lock
);
49 hdr
->buffer
= &diag
->port_data
.data
;
50 hdr
->buffer_size
= sizeof(diag
->port_data
.data
);
51 /* set the timestamp so that the first test on age will always fail */
52 hdr
->timestamp
= jiffies
- msecs_to_jiffies(diag
->max_age
);
54 /* setup header for config_data */
55 hdr
= &diag
->config_data
.header
;
57 spin_lock_init(&hdr
->access_lock
);
58 hdr
->buffer
= &diag
->config_data
.data
;
59 hdr
->buffer_size
= sizeof(diag
->config_data
.data
);
60 /* set the timestamp so that the first test on age will always fail */
61 hdr
->timestamp
= jiffies
- msecs_to_jiffies(diag
->max_age
);
63 adapter
->diagnostics
= diag
;
68 * zfcp_diag_adapter_free() - Frees all adapter diagnostics allocations.
69 * @adapter: the adapter whose diagnostic structures should be freed.
71 * Frees all data-structures in the given adapter that store diagnostics
72 * information. Can savely be called with partially setup diagnostics.
74 void zfcp_diag_adapter_free(struct zfcp_adapter
*const adapter
)
76 kfree(adapter
->diagnostics
);
77 adapter
->diagnostics
= NULL
;
81 * zfcp_diag_update_xdata() - Update a diagnostics buffer.
82 * @hdr: the meta data to update.
83 * @data: data to use for the update.
84 * @incomplete: flag stating whether the data in @data is incomplete.
86 void zfcp_diag_update_xdata(struct zfcp_diag_header
*const hdr
,
87 const void *const data
, const bool incomplete
)
89 const unsigned long capture_timestamp
= jiffies
;
92 spin_lock_irqsave(&hdr
->access_lock
, flags
);
94 /* make sure we never go into the past with an update */
95 if (!time_after_eq(capture_timestamp
, hdr
->timestamp
))
98 hdr
->timestamp
= capture_timestamp
;
99 hdr
->incomplete
= incomplete
;
100 memcpy(hdr
->buffer
, data
, hdr
->buffer_size
);
102 spin_unlock_irqrestore(&hdr
->access_lock
, flags
);
106 * zfcp_diag_update_port_data_buffer() - Implementation of
107 * &typedef zfcp_diag_update_buffer_func
108 * to collect and update Port Data.
109 * @adapter: Adapter to collect Port Data from.
111 * This call is SYNCHRONOUS ! It blocks till the respective command has
112 * finished completely, or has failed in some way.
115 * * 0 - Successfully retrieved new Diagnostics and Updated the buffer;
116 * this also includes cases where data was retrieved, but
117 * incomplete; you'll have to check the flag ``incomplete``
118 * of &struct zfcp_diag_header.
119 * * see zfcp_fsf_exchange_port_data_sync() for possible error-codes (
122 int zfcp_diag_update_port_data_buffer(struct zfcp_adapter
*const adapter
)
126 rc
= zfcp_fsf_exchange_port_data_sync(adapter
->qdio
, NULL
);
128 rc
= 0; /* signaling incomplete via struct zfcp_diag_header */
130 /* buffer-data was updated in zfcp_fsf_exchange_port_data_handler() */
136 * zfcp_diag_update_config_data_buffer() - Implementation of
137 * &typedef zfcp_diag_update_buffer_func
138 * to collect and update Config Data.
139 * @adapter: Adapter to collect Config Data from.
141 * This call is SYNCHRONOUS ! It blocks till the respective command has
142 * finished completely, or has failed in some way.
145 * * 0 - Successfully retrieved new Diagnostics and Updated the buffer;
146 * this also includes cases where data was retrieved, but
147 * incomplete; you'll have to check the flag ``incomplete``
148 * of &struct zfcp_diag_header.
149 * * see zfcp_fsf_exchange_config_data_sync() for possible error-codes (
152 int zfcp_diag_update_config_data_buffer(struct zfcp_adapter
*const adapter
)
156 rc
= zfcp_fsf_exchange_config_data_sync(adapter
->qdio
, NULL
);
158 rc
= 0; /* signaling incomplete via struct zfcp_diag_header */
160 /* buffer-data was updated in zfcp_fsf_exchange_config_data_handler() */
165 static 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
)
173 if (hdr
->updating
== 1) {
174 rc
= wait_event_interruptible_lock_irq(__zfcp_diag_publish_wait
,
177 rc
= (rc
== 0 ? -EAGAIN
: -EINTR
);
180 spin_unlock_irqrestore(&hdr
->access_lock
, *flags
);
182 /* unlocked, because update function sleeps */
183 rc
= buffer_update(adapter
);
185 spin_lock_irqsave(&hdr
->access_lock
, *flags
);
189 * every thread waiting here went via an interruptible wait,
190 * so its fine to only wake those
192 wake_up_interruptible_all(&__zfcp_diag_publish_wait
);
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
)
203 const unsigned long now
= jiffies
;
206 * Should not happen (data is from the future).. if it does, still
207 * signal that it needs refresh
209 if (!time_after_eq(now
, hdr
->timestamp
))
212 if (jiffies_to_msecs(now
- hdr
->timestamp
) >= diag
->max_age
)
219 * zfcp_diag_update_buffer_limited() - Collect diagnostics and update a
220 * diagnostics buffer rate limited.
221 * @adapter: Adapter to collect the diagnostics from.
222 * @hdr: buffer-header for which to update with the collected diagnostics.
223 * @buffer_update: Specific implementation for collecting and updating.
225 * This function will cause an update of the given @hdr by calling the also
226 * given @buffer_update function. If called by multiple sources at the same
227 * time, it will synchornize the update by only allowing one source to call
228 * @buffer_update and the others to wait for that source to complete instead
229 * (the wait is interruptible).
231 * Additionally this version is rate-limited and will only exit if either the
232 * buffer is fresh enough (within the limit) - it will do nothing if the buffer
233 * is fresh enough to begin with -, or if the source/thread that started this
234 * update is the one that made the update (to prevent endless loops).
237 * * 0 - If the update was successfully published and/or the buffer is
239 * * -EINTR - If the thread went into the wait-state and was interrupted
240 * * whatever @buffer_update returns
242 int 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
)
249 spin_lock_irqsave(&hdr
->access_lock
, flags
);
252 !__zfcp_diag_test_buffer_age_isfresh(adapter
->diagnostics
, hdr
);
254 rc
= __zfcp_diag_update_buffer(adapter
, hdr
, buffer_update
,
260 spin_unlock_irqrestore(&hdr
->access_lock
, flags
);