1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * zfcp device driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Functions to handle diagnostics.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Copyright IBM Corp. 2018
8*4882a593Smuzhiyun */
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <linux/spinlock.h>
11*4882a593Smuzhiyun #include <linux/jiffies.h>
12*4882a593Smuzhiyun #include <linux/string.h>
13*4882a593Smuzhiyun #include <linux/kernfs.h>
14*4882a593Smuzhiyun #include <linux/sysfs.h>
15*4882a593Smuzhiyun #include <linux/errno.h>
16*4882a593Smuzhiyun #include <linux/slab.h>
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include "zfcp_diag.h"
19*4882a593Smuzhiyun #include "zfcp_ext.h"
20*4882a593Smuzhiyun #include "zfcp_def.h"
21*4882a593Smuzhiyun
22*4882a593Smuzhiyun static DECLARE_WAIT_QUEUE_HEAD(__zfcp_diag_publish_wait);
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun /**
25*4882a593Smuzhiyun * zfcp_diag_adapter_setup() - Setup storage for adapter diagnostics.
26*4882a593Smuzhiyun * @adapter: the adapter to setup diagnostics for.
27*4882a593Smuzhiyun *
28*4882a593Smuzhiyun * Creates the data-structures to store the diagnostics for an adapter. This
29*4882a593Smuzhiyun * overwrites whatever was stored before at &zfcp_adapter->diagnostics!
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * Return:
32*4882a593Smuzhiyun * * 0 - Everyting is OK
33*4882a593Smuzhiyun * * -ENOMEM - Could not allocate all/parts of the data-structures;
34*4882a593Smuzhiyun * &zfcp_adapter->diagnostics remains unchanged
35*4882a593Smuzhiyun */
zfcp_diag_adapter_setup(struct zfcp_adapter * const adapter)36*4882a593Smuzhiyun int zfcp_diag_adapter_setup(struct zfcp_adapter *const adapter)
37*4882a593Smuzhiyun {
38*4882a593Smuzhiyun struct zfcp_diag_adapter *diag;
39*4882a593Smuzhiyun struct zfcp_diag_header *hdr;
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun diag = kzalloc(sizeof(*diag), GFP_KERNEL);
42*4882a593Smuzhiyun if (diag == NULL)
43*4882a593Smuzhiyun return -ENOMEM;
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun diag->max_age = (5 * 1000); /* default value: 5 s */
46*4882a593Smuzhiyun
47*4882a593Smuzhiyun /* setup header for port_data */
48*4882a593Smuzhiyun hdr = &diag->port_data.header;
49*4882a593Smuzhiyun
50*4882a593Smuzhiyun spin_lock_init(&hdr->access_lock);
51*4882a593Smuzhiyun hdr->buffer = &diag->port_data.data;
52*4882a593Smuzhiyun hdr->buffer_size = sizeof(diag->port_data.data);
53*4882a593Smuzhiyun /* set the timestamp so that the first test on age will always fail */
54*4882a593Smuzhiyun hdr->timestamp = jiffies - msecs_to_jiffies(diag->max_age);
55*4882a593Smuzhiyun
56*4882a593Smuzhiyun /* setup header for config_data */
57*4882a593Smuzhiyun hdr = &diag->config_data.header;
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun spin_lock_init(&hdr->access_lock);
60*4882a593Smuzhiyun hdr->buffer = &diag->config_data.data;
61*4882a593Smuzhiyun hdr->buffer_size = sizeof(diag->config_data.data);
62*4882a593Smuzhiyun /* set the timestamp so that the first test on age will always fail */
63*4882a593Smuzhiyun hdr->timestamp = jiffies - msecs_to_jiffies(diag->max_age);
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun adapter->diagnostics = diag;
66*4882a593Smuzhiyun return 0;
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /**
70*4882a593Smuzhiyun * zfcp_diag_adapter_free() - Frees all adapter diagnostics allocations.
71*4882a593Smuzhiyun * @adapter: the adapter whose diagnostic structures should be freed.
72*4882a593Smuzhiyun *
73*4882a593Smuzhiyun * Frees all data-structures in the given adapter that store diagnostics
74*4882a593Smuzhiyun * information. Can savely be called with partially setup diagnostics.
75*4882a593Smuzhiyun */
zfcp_diag_adapter_free(struct zfcp_adapter * const adapter)76*4882a593Smuzhiyun void zfcp_diag_adapter_free(struct zfcp_adapter *const adapter)
77*4882a593Smuzhiyun {
78*4882a593Smuzhiyun kfree(adapter->diagnostics);
79*4882a593Smuzhiyun adapter->diagnostics = NULL;
80*4882a593Smuzhiyun }
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun /**
83*4882a593Smuzhiyun * zfcp_diag_sysfs_setup() - Setup the sysfs-group for adapter-diagnostics.
84*4882a593Smuzhiyun * @adapter: target adapter to which the group should be added.
85*4882a593Smuzhiyun *
86*4882a593Smuzhiyun * Return: 0 on success; Something else otherwise (see sysfs_create_group()).
87*4882a593Smuzhiyun */
zfcp_diag_sysfs_setup(struct zfcp_adapter * const adapter)88*4882a593Smuzhiyun int zfcp_diag_sysfs_setup(struct zfcp_adapter *const adapter)
89*4882a593Smuzhiyun {
90*4882a593Smuzhiyun int rc = sysfs_create_group(&adapter->ccw_device->dev.kobj,
91*4882a593Smuzhiyun &zfcp_sysfs_diag_attr_group);
92*4882a593Smuzhiyun if (rc == 0)
93*4882a593Smuzhiyun adapter->diagnostics->sysfs_established = 1;
94*4882a593Smuzhiyun
95*4882a593Smuzhiyun return rc;
96*4882a593Smuzhiyun }
97*4882a593Smuzhiyun
98*4882a593Smuzhiyun /**
99*4882a593Smuzhiyun * zfcp_diag_sysfs_destroy() - Remove the sysfs-group for adapter-diagnostics.
100*4882a593Smuzhiyun * @adapter: target adapter from which the group should be removed.
101*4882a593Smuzhiyun */
zfcp_diag_sysfs_destroy(struct zfcp_adapter * const adapter)102*4882a593Smuzhiyun void zfcp_diag_sysfs_destroy(struct zfcp_adapter *const adapter)
103*4882a593Smuzhiyun {
104*4882a593Smuzhiyun if (adapter->diagnostics == NULL ||
105*4882a593Smuzhiyun !adapter->diagnostics->sysfs_established)
106*4882a593Smuzhiyun return;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /*
109*4882a593Smuzhiyun * We need this state-handling so we can prevent warnings being printed
110*4882a593Smuzhiyun * on the kernel-console in case we have to abort a halfway done
111*4882a593Smuzhiyun * zfcp_adapter_enqueue(), in which the sysfs-group was not yet
112*4882a593Smuzhiyun * established. sysfs_remove_group() does this checking as well, but
113*4882a593Smuzhiyun * still prints a warning in case we try to remove a group that has not
114*4882a593Smuzhiyun * been established before
115*4882a593Smuzhiyun */
116*4882a593Smuzhiyun adapter->diagnostics->sysfs_established = 0;
117*4882a593Smuzhiyun sysfs_remove_group(&adapter->ccw_device->dev.kobj,
118*4882a593Smuzhiyun &zfcp_sysfs_diag_attr_group);
119*4882a593Smuzhiyun }
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun
122*4882a593Smuzhiyun /**
123*4882a593Smuzhiyun * zfcp_diag_update_xdata() - Update a diagnostics buffer.
124*4882a593Smuzhiyun * @hdr: the meta data to update.
125*4882a593Smuzhiyun * @data: data to use for the update.
126*4882a593Smuzhiyun * @incomplete: flag stating whether the data in @data is incomplete.
127*4882a593Smuzhiyun */
zfcp_diag_update_xdata(struct zfcp_diag_header * const hdr,const void * const data,const bool incomplete)128*4882a593Smuzhiyun void zfcp_diag_update_xdata(struct zfcp_diag_header *const hdr,
129*4882a593Smuzhiyun const void *const data, const bool incomplete)
130*4882a593Smuzhiyun {
131*4882a593Smuzhiyun const unsigned long capture_timestamp = jiffies;
132*4882a593Smuzhiyun unsigned long flags;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun spin_lock_irqsave(&hdr->access_lock, flags);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /* make sure we never go into the past with an update */
137*4882a593Smuzhiyun if (!time_after_eq(capture_timestamp, hdr->timestamp))
138*4882a593Smuzhiyun goto out;
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun hdr->timestamp = capture_timestamp;
141*4882a593Smuzhiyun hdr->incomplete = incomplete;
142*4882a593Smuzhiyun memcpy(hdr->buffer, data, hdr->buffer_size);
143*4882a593Smuzhiyun out:
144*4882a593Smuzhiyun spin_unlock_irqrestore(&hdr->access_lock, flags);
145*4882a593Smuzhiyun }
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /**
148*4882a593Smuzhiyun * zfcp_diag_update_port_data_buffer() - Implementation of
149*4882a593Smuzhiyun * &typedef zfcp_diag_update_buffer_func
150*4882a593Smuzhiyun * to collect and update Port Data.
151*4882a593Smuzhiyun * @adapter: Adapter to collect Port Data from.
152*4882a593Smuzhiyun *
153*4882a593Smuzhiyun * This call is SYNCHRONOUS ! It blocks till the respective command has
154*4882a593Smuzhiyun * finished completely, or has failed in some way.
155*4882a593Smuzhiyun *
156*4882a593Smuzhiyun * Return:
157*4882a593Smuzhiyun * * 0 - Successfully retrieved new Diagnostics and Updated the buffer;
158*4882a593Smuzhiyun * this also includes cases where data was retrieved, but
159*4882a593Smuzhiyun * incomplete; you'll have to check the flag ``incomplete``
160*4882a593Smuzhiyun * of &struct zfcp_diag_header.
161*4882a593Smuzhiyun * * see zfcp_fsf_exchange_port_data_sync() for possible error-codes (
162*4882a593Smuzhiyun * excluding -EAGAIN)
163*4882a593Smuzhiyun */
zfcp_diag_update_port_data_buffer(struct zfcp_adapter * const adapter)164*4882a593Smuzhiyun int zfcp_diag_update_port_data_buffer(struct zfcp_adapter *const adapter)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun int rc;
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun rc = zfcp_fsf_exchange_port_data_sync(adapter->qdio, NULL);
169*4882a593Smuzhiyun if (rc == -EAGAIN)
170*4882a593Smuzhiyun rc = 0; /* signaling incomplete via struct zfcp_diag_header */
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun /* buffer-data was updated in zfcp_fsf_exchange_port_data_handler() */
173*4882a593Smuzhiyun
174*4882a593Smuzhiyun return rc;
175*4882a593Smuzhiyun }
176*4882a593Smuzhiyun
177*4882a593Smuzhiyun /**
178*4882a593Smuzhiyun * zfcp_diag_update_config_data_buffer() - Implementation of
179*4882a593Smuzhiyun * &typedef zfcp_diag_update_buffer_func
180*4882a593Smuzhiyun * to collect and update Config Data.
181*4882a593Smuzhiyun * @adapter: Adapter to collect Config Data from.
182*4882a593Smuzhiyun *
183*4882a593Smuzhiyun * This call is SYNCHRONOUS ! It blocks till the respective command has
184*4882a593Smuzhiyun * finished completely, or has failed in some way.
185*4882a593Smuzhiyun *
186*4882a593Smuzhiyun * Return:
187*4882a593Smuzhiyun * * 0 - Successfully retrieved new Diagnostics and Updated the buffer;
188*4882a593Smuzhiyun * this also includes cases where data was retrieved, but
189*4882a593Smuzhiyun * incomplete; you'll have to check the flag ``incomplete``
190*4882a593Smuzhiyun * of &struct zfcp_diag_header.
191*4882a593Smuzhiyun * * see zfcp_fsf_exchange_config_data_sync() for possible error-codes (
192*4882a593Smuzhiyun * excluding -EAGAIN)
193*4882a593Smuzhiyun */
zfcp_diag_update_config_data_buffer(struct zfcp_adapter * const adapter)194*4882a593Smuzhiyun int zfcp_diag_update_config_data_buffer(struct zfcp_adapter *const adapter)
195*4882a593Smuzhiyun {
196*4882a593Smuzhiyun int rc;
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun rc = zfcp_fsf_exchange_config_data_sync(adapter->qdio, NULL);
199*4882a593Smuzhiyun if (rc == -EAGAIN)
200*4882a593Smuzhiyun rc = 0; /* signaling incomplete via struct zfcp_diag_header */
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun /* buffer-data was updated in zfcp_fsf_exchange_config_data_handler() */
203*4882a593Smuzhiyun
204*4882a593Smuzhiyun return rc;
205*4882a593Smuzhiyun }
206*4882a593Smuzhiyun
__zfcp_diag_update_buffer(struct zfcp_adapter * const adapter,struct zfcp_diag_header * const hdr,zfcp_diag_update_buffer_func buffer_update,unsigned long * const flags)207*4882a593Smuzhiyun static int __zfcp_diag_update_buffer(struct zfcp_adapter *const adapter,
208*4882a593Smuzhiyun struct zfcp_diag_header *const hdr,
209*4882a593Smuzhiyun zfcp_diag_update_buffer_func buffer_update,
210*4882a593Smuzhiyun unsigned long *const flags)
211*4882a593Smuzhiyun __must_hold(hdr->access_lock)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun int rc;
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun if (hdr->updating == 1) {
216*4882a593Smuzhiyun rc = wait_event_interruptible_lock_irq(__zfcp_diag_publish_wait,
217*4882a593Smuzhiyun hdr->updating == 0,
218*4882a593Smuzhiyun hdr->access_lock);
219*4882a593Smuzhiyun rc = (rc == 0 ? -EAGAIN : -EINTR);
220*4882a593Smuzhiyun } else {
221*4882a593Smuzhiyun hdr->updating = 1;
222*4882a593Smuzhiyun spin_unlock_irqrestore(&hdr->access_lock, *flags);
223*4882a593Smuzhiyun
224*4882a593Smuzhiyun /* unlocked, because update function sleeps */
225*4882a593Smuzhiyun rc = buffer_update(adapter);
226*4882a593Smuzhiyun
227*4882a593Smuzhiyun spin_lock_irqsave(&hdr->access_lock, *flags);
228*4882a593Smuzhiyun hdr->updating = 0;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /*
231*4882a593Smuzhiyun * every thread waiting here went via an interruptible wait,
232*4882a593Smuzhiyun * so its fine to only wake those
233*4882a593Smuzhiyun */
234*4882a593Smuzhiyun wake_up_interruptible_all(&__zfcp_diag_publish_wait);
235*4882a593Smuzhiyun }
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun return rc;
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
240*4882a593Smuzhiyun static bool
__zfcp_diag_test_buffer_age_isfresh(const struct zfcp_diag_adapter * const diag,const struct zfcp_diag_header * const hdr)241*4882a593Smuzhiyun __zfcp_diag_test_buffer_age_isfresh(const struct zfcp_diag_adapter *const diag,
242*4882a593Smuzhiyun const struct zfcp_diag_header *const hdr)
243*4882a593Smuzhiyun __must_hold(hdr->access_lock)
244*4882a593Smuzhiyun {
245*4882a593Smuzhiyun const unsigned long now = jiffies;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /*
248*4882a593Smuzhiyun * Should not happen (data is from the future).. if it does, still
249*4882a593Smuzhiyun * signal that it needs refresh
250*4882a593Smuzhiyun */
251*4882a593Smuzhiyun if (!time_after_eq(now, hdr->timestamp))
252*4882a593Smuzhiyun return false;
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun if (jiffies_to_msecs(now - hdr->timestamp) >= diag->max_age)
255*4882a593Smuzhiyun return false;
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun return true;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun
260*4882a593Smuzhiyun /**
261*4882a593Smuzhiyun * zfcp_diag_update_buffer_limited() - Collect diagnostics and update a
262*4882a593Smuzhiyun * diagnostics buffer rate limited.
263*4882a593Smuzhiyun * @adapter: Adapter to collect the diagnostics from.
264*4882a593Smuzhiyun * @hdr: buffer-header for which to update with the collected diagnostics.
265*4882a593Smuzhiyun * @buffer_update: Specific implementation for collecting and updating.
266*4882a593Smuzhiyun *
267*4882a593Smuzhiyun * This function will cause an update of the given @hdr by calling the also
268*4882a593Smuzhiyun * given @buffer_update function. If called by multiple sources at the same
269*4882a593Smuzhiyun * time, it will synchornize the update by only allowing one source to call
270*4882a593Smuzhiyun * @buffer_update and the others to wait for that source to complete instead
271*4882a593Smuzhiyun * (the wait is interruptible).
272*4882a593Smuzhiyun *
273*4882a593Smuzhiyun * Additionally this version is rate-limited and will only exit if either the
274*4882a593Smuzhiyun * buffer is fresh enough (within the limit) - it will do nothing if the buffer
275*4882a593Smuzhiyun * is fresh enough to begin with -, or if the source/thread that started this
276*4882a593Smuzhiyun * update is the one that made the update (to prevent endless loops).
277*4882a593Smuzhiyun *
278*4882a593Smuzhiyun * Return:
279*4882a593Smuzhiyun * * 0 - If the update was successfully published and/or the buffer is
280*4882a593Smuzhiyun * fresh enough
281*4882a593Smuzhiyun * * -EINTR - If the thread went into the wait-state and was interrupted
282*4882a593Smuzhiyun * * whatever @buffer_update returns
283*4882a593Smuzhiyun */
zfcp_diag_update_buffer_limited(struct zfcp_adapter * const adapter,struct zfcp_diag_header * const hdr,zfcp_diag_update_buffer_func buffer_update)284*4882a593Smuzhiyun int zfcp_diag_update_buffer_limited(struct zfcp_adapter *const adapter,
285*4882a593Smuzhiyun struct zfcp_diag_header *const hdr,
286*4882a593Smuzhiyun zfcp_diag_update_buffer_func buffer_update)
287*4882a593Smuzhiyun {
288*4882a593Smuzhiyun unsigned long flags;
289*4882a593Smuzhiyun int rc;
290*4882a593Smuzhiyun
291*4882a593Smuzhiyun spin_lock_irqsave(&hdr->access_lock, flags);
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun for (rc = 0;
294*4882a593Smuzhiyun !__zfcp_diag_test_buffer_age_isfresh(adapter->diagnostics, hdr);
295*4882a593Smuzhiyun rc = 0) {
296*4882a593Smuzhiyun rc = __zfcp_diag_update_buffer(adapter, hdr, buffer_update,
297*4882a593Smuzhiyun &flags);
298*4882a593Smuzhiyun if (rc != -EAGAIN)
299*4882a593Smuzhiyun break;
300*4882a593Smuzhiyun }
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun spin_unlock_irqrestore(&hdr->access_lock, flags);
303*4882a593Smuzhiyun
304*4882a593Smuzhiyun return rc;
305*4882a593Smuzhiyun }
306