0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/spinlock.h>
0011 #include <linux/jiffies.h>
0012 #include <linux/string.h>
0013 #include <linux/errno.h>
0014 #include <linux/slab.h>
0015
0016 #include "zfcp_diag.h"
0017 #include "zfcp_ext.h"
0018 #include "zfcp_def.h"
0019
0020 static DECLARE_WAIT_QUEUE_HEAD(__zfcp_diag_publish_wait);
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 int zfcp_diag_adapter_setup(struct zfcp_adapter *const adapter)
0035 {
0036 struct zfcp_diag_adapter *diag;
0037 struct zfcp_diag_header *hdr;
0038
0039 diag = kzalloc(sizeof(*diag), GFP_KERNEL);
0040 if (diag == NULL)
0041 return -ENOMEM;
0042
0043 diag->max_age = (5 * 1000);
0044
0045
0046 hdr = &diag->port_data.header;
0047
0048 spin_lock_init(&hdr->access_lock);
0049 hdr->buffer = &diag->port_data.data;
0050 hdr->buffer_size = sizeof(diag->port_data.data);
0051
0052 hdr->timestamp = jiffies - msecs_to_jiffies(diag->max_age);
0053
0054
0055 hdr = &diag->config_data.header;
0056
0057 spin_lock_init(&hdr->access_lock);
0058 hdr->buffer = &diag->config_data.data;
0059 hdr->buffer_size = sizeof(diag->config_data.data);
0060
0061 hdr->timestamp = jiffies - msecs_to_jiffies(diag->max_age);
0062
0063 adapter->diagnostics = diag;
0064 return 0;
0065 }
0066
0067
0068
0069
0070
0071
0072
0073
0074 void zfcp_diag_adapter_free(struct zfcp_adapter *const adapter)
0075 {
0076 kfree(adapter->diagnostics);
0077 adapter->diagnostics = NULL;
0078 }
0079
0080
0081
0082
0083
0084
0085
0086 void zfcp_diag_update_xdata(struct zfcp_diag_header *const hdr,
0087 const void *const data, const bool incomplete)
0088 {
0089 const unsigned long capture_timestamp = jiffies;
0090 unsigned long flags;
0091
0092 spin_lock_irqsave(&hdr->access_lock, flags);
0093
0094
0095 if (!time_after_eq(capture_timestamp, hdr->timestamp))
0096 goto out;
0097
0098 hdr->timestamp = capture_timestamp;
0099 hdr->incomplete = incomplete;
0100 memcpy(hdr->buffer, data, hdr->buffer_size);
0101 out:
0102 spin_unlock_irqrestore(&hdr->access_lock, flags);
0103 }
0104
0105
0106
0107
0108
0109
0110
0111
0112
0113
0114
0115
0116
0117
0118
0119
0120
0121
0122 int zfcp_diag_update_port_data_buffer(struct zfcp_adapter *const adapter)
0123 {
0124 int rc;
0125
0126 rc = zfcp_fsf_exchange_port_data_sync(adapter->qdio, NULL);
0127 if (rc == -EAGAIN)
0128 rc = 0;
0129
0130
0131
0132 return rc;
0133 }
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148
0149
0150
0151
0152 int zfcp_diag_update_config_data_buffer(struct zfcp_adapter *const adapter)
0153 {
0154 int rc;
0155
0156 rc = zfcp_fsf_exchange_config_data_sync(adapter->qdio, NULL);
0157 if (rc == -EAGAIN)
0158 rc = 0;
0159
0160
0161
0162 return rc;
0163 }
0164
0165 static int __zfcp_diag_update_buffer(struct zfcp_adapter *const adapter,
0166 struct zfcp_diag_header *const hdr,
0167 zfcp_diag_update_buffer_func buffer_update,
0168 unsigned long *const flags)
0169 __must_hold(hdr->access_lock)
0170 {
0171 int rc;
0172
0173 if (hdr->updating == 1) {
0174 rc = wait_event_interruptible_lock_irq(__zfcp_diag_publish_wait,
0175 hdr->updating == 0,
0176 hdr->access_lock);
0177 rc = (rc == 0 ? -EAGAIN : -EINTR);
0178 } else {
0179 hdr->updating = 1;
0180 spin_unlock_irqrestore(&hdr->access_lock, *flags);
0181
0182
0183 rc = buffer_update(adapter);
0184
0185 spin_lock_irqsave(&hdr->access_lock, *flags);
0186 hdr->updating = 0;
0187
0188
0189
0190
0191
0192 wake_up_interruptible_all(&__zfcp_diag_publish_wait);
0193 }
0194
0195 return rc;
0196 }
0197
0198 static bool
0199 __zfcp_diag_test_buffer_age_isfresh(const struct zfcp_diag_adapter *const diag,
0200 const struct zfcp_diag_header *const hdr)
0201 __must_hold(hdr->access_lock)
0202 {
0203 const unsigned long now = jiffies;
0204
0205
0206
0207
0208
0209 if (!time_after_eq(now, hdr->timestamp))
0210 return false;
0211
0212 if (jiffies_to_msecs(now - hdr->timestamp) >= diag->max_age)
0213 return false;
0214
0215 return true;
0216 }
0217
0218
0219
0220
0221
0222
0223
0224
0225
0226
0227
0228
0229
0230
0231
0232
0233
0234
0235
0236
0237
0238
0239
0240
0241
0242 int zfcp_diag_update_buffer_limited(struct zfcp_adapter *const adapter,
0243 struct zfcp_diag_header *const hdr,
0244 zfcp_diag_update_buffer_func buffer_update)
0245 {
0246 unsigned long flags;
0247 int rc;
0248
0249 spin_lock_irqsave(&hdr->access_lock, flags);
0250
0251 for (rc = 0;
0252 !__zfcp_diag_test_buffer_age_isfresh(adapter->diagnostics, hdr);
0253 rc = 0) {
0254 rc = __zfcp_diag_update_buffer(adapter, hdr, buffer_update,
0255 &flags);
0256 if (rc != -EAGAIN)
0257 break;
0258 }
0259
0260 spin_unlock_irqrestore(&hdr->access_lock, flags);
0261
0262 return rc;
0263 }