Merge branch 'linus' into cpus4096
[linux-2.6] / drivers / s390 / scsi / zfcp_sysfs.c
1 /*
2  * zfcp device driver
3  *
4  * sysfs attributes.
5  *
6  * Copyright IBM Corporation 2008
7  */
8
9 #include "zfcp_ext.h"
10
11 #define ZFCP_DEV_ATTR(_feat, _name, _mode, _show, _store) \
12 struct device_attribute dev_attr_##_feat##_##_name = __ATTR(_name, _mode,\
13                                                             _show, _store)
14 #define ZFCP_DEFINE_ATTR(_feat_def, _feat, _name, _format, _value)             \
15 static ssize_t zfcp_sysfs_##_feat##_##_name##_show(struct device *dev,         \
16                                                    struct device_attribute *at,\
17                                                    char *buf)                  \
18 {                                                                              \
19         struct _feat_def *_feat = dev_get_drvdata(dev);                        \
20                                                                                \
21         return sprintf(buf, _format, _value);                                  \
22 }                                                                              \
23 static ZFCP_DEV_ATTR(_feat, _name, S_IRUGO,                                    \
24                      zfcp_sysfs_##_feat##_##_name##_show, NULL);
25
26 ZFCP_DEFINE_ATTR(zfcp_adapter, adapter, status, "0x%08x\n",
27                  atomic_read(&adapter->status));
28 ZFCP_DEFINE_ATTR(zfcp_adapter, adapter, peer_wwnn, "0x%016llx\n",
29                  adapter->peer_wwnn);
30 ZFCP_DEFINE_ATTR(zfcp_adapter, adapter, peer_wwpn, "0x%016llx\n",
31                  adapter->peer_wwpn);
32 ZFCP_DEFINE_ATTR(zfcp_adapter, adapter, peer_d_id, "0x%06x\n",
33                  adapter->peer_d_id);
34 ZFCP_DEFINE_ATTR(zfcp_adapter, adapter, card_version, "0x%04x\n",
35                  adapter->hydra_version);
36 ZFCP_DEFINE_ATTR(zfcp_adapter, adapter, lic_version, "0x%08x\n",
37                  adapter->fsf_lic_version);
38 ZFCP_DEFINE_ATTR(zfcp_adapter, adapter, hardware_version, "0x%08x\n",
39                  adapter->hardware_version);
40 ZFCP_DEFINE_ATTR(zfcp_adapter, adapter, in_recovery, "%d\n",
41                  (atomic_read(&adapter->status) &
42                   ZFCP_STATUS_COMMON_ERP_INUSE) != 0);
43
44 ZFCP_DEFINE_ATTR(zfcp_port, port, status, "0x%08x\n",
45                  atomic_read(&port->status));
46 ZFCP_DEFINE_ATTR(zfcp_port, port, in_recovery, "%d\n",
47                  (atomic_read(&port->status) &
48                   ZFCP_STATUS_COMMON_ERP_INUSE) != 0);
49 ZFCP_DEFINE_ATTR(zfcp_port, port, access_denied, "%d\n",
50                  (atomic_read(&port->status) &
51                   ZFCP_STATUS_COMMON_ACCESS_DENIED) != 0);
52
53 ZFCP_DEFINE_ATTR(zfcp_unit, unit, status, "0x%08x\n",
54                  atomic_read(&unit->status));
55 ZFCP_DEFINE_ATTR(zfcp_unit, unit, in_recovery, "%d\n",
56                  (atomic_read(&unit->status) &
57                   ZFCP_STATUS_COMMON_ERP_INUSE) != 0);
58 ZFCP_DEFINE_ATTR(zfcp_unit, unit, access_denied, "%d\n",
59                  (atomic_read(&unit->status) &
60                   ZFCP_STATUS_COMMON_ACCESS_DENIED) != 0);
61 ZFCP_DEFINE_ATTR(zfcp_unit, unit, access_shared, "%d\n",
62                  (atomic_read(&unit->status) &
63                   ZFCP_STATUS_UNIT_SHARED) != 0);
64 ZFCP_DEFINE_ATTR(zfcp_unit, unit, access_readonly, "%d\n",
65                  (atomic_read(&unit->status) &
66                   ZFCP_STATUS_UNIT_READONLY) != 0);
67
68 #define ZFCP_SYSFS_FAILED(_feat_def, _feat, _adapter, _mod_id, _reopen_id)     \
69 static ssize_t zfcp_sysfs_##_feat##_failed_show(struct device *dev,            \
70                                                 struct device_attribute *attr, \
71                                                 char *buf)                     \
72 {                                                                              \
73         struct _feat_def *_feat = dev_get_drvdata(dev);                        \
74                                                                                \
75         if (atomic_read(&_feat->status) & ZFCP_STATUS_COMMON_ERP_FAILED)       \
76                 return sprintf(buf, "1\n");                                    \
77         else                                                                   \
78                 return sprintf(buf, "0\n");                                    \
79 }                                                                              \
80 static ssize_t zfcp_sysfs_##_feat##_failed_store(struct device *dev,           \
81                                                  struct device_attribute *attr,\
82                                                  const char *buf, size_t count)\
83 {                                                                              \
84         struct _feat_def *_feat = dev_get_drvdata(dev);                        \
85         unsigned long val;                                                     \
86         int retval = 0;                                                        \
87                                                                                \
88         down(&zfcp_data.config_sema);                                          \
89         if (atomic_read(&_feat->status) & ZFCP_STATUS_COMMON_REMOVE) {         \
90                 retval = -EBUSY;                                               \
91                 goto out;                                                      \
92         }                                                                      \
93                                                                                \
94         if (strict_strtoul(buf, 0, &val) || val != 0) {                        \
95                 retval = -EINVAL;                                              \
96                 goto out;                                                      \
97         }                                                                      \
98                                                                                \
99         zfcp_erp_modify_##_feat##_status(_feat, _mod_id, NULL,                 \
100                                          ZFCP_STATUS_COMMON_RUNNING, ZFCP_SET);\
101         zfcp_erp_##_feat##_reopen(_feat, ZFCP_STATUS_COMMON_ERP_FAILED,        \
102                                   _reopen_id, NULL);                           \
103         zfcp_erp_wait(_adapter);                                               \
104 out:                                                                           \
105         up(&zfcp_data.config_sema);                                            \
106         return retval ? retval : (ssize_t) count;                              \
107 }                                                                              \
108 static ZFCP_DEV_ATTR(_feat, failed, S_IWUSR | S_IRUGO,                         \
109                      zfcp_sysfs_##_feat##_failed_show,                         \
110                      zfcp_sysfs_##_feat##_failed_store);
111
112 ZFCP_SYSFS_FAILED(zfcp_adapter, adapter, adapter, 44, 93);
113 ZFCP_SYSFS_FAILED(zfcp_port, port, port->adapter, 45, 96);
114 ZFCP_SYSFS_FAILED(zfcp_unit, unit, unit->port->adapter, 46, 97);
115
116 static ssize_t zfcp_sysfs_port_rescan_store(struct device *dev,
117                                             struct device_attribute *attr,
118                                             const char *buf, size_t count)
119 {
120         struct zfcp_adapter *adapter = dev_get_drvdata(dev);
121         int ret;
122
123         if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_REMOVE)
124                 return -EBUSY;
125
126         ret = zfcp_scan_ports(adapter);
127         return ret ? ret : (ssize_t) count;
128 }
129 static ZFCP_DEV_ATTR(adapter, port_rescan, S_IWUSR, NULL,
130                      zfcp_sysfs_port_rescan_store);
131
132 static ssize_t zfcp_sysfs_port_remove_store(struct device *dev,
133                                             struct device_attribute *attr,
134                                             const char *buf, size_t count)
135 {
136         struct zfcp_adapter *adapter = dev_get_drvdata(dev);
137         struct zfcp_port *port;
138         wwn_t wwpn;
139         int retval = 0;
140
141         down(&zfcp_data.config_sema);
142         if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_REMOVE) {
143                 retval = -EBUSY;
144                 goto out;
145         }
146
147         if (strict_strtoull(buf, 0, &wwpn)) {
148                 retval = -EINVAL;
149                 goto out;
150         }
151
152         write_lock_irq(&zfcp_data.config_lock);
153         port = zfcp_get_port_by_wwpn(adapter, wwpn);
154         if (port && (atomic_read(&port->refcount) == 0)) {
155                 zfcp_port_get(port);
156                 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
157                 list_move(&port->list, &adapter->port_remove_lh);
158         } else
159                 port = NULL;
160         write_unlock_irq(&zfcp_data.config_lock);
161
162         if (!port) {
163                 retval = -ENXIO;
164                 goto out;
165         }
166
167         zfcp_erp_port_shutdown(port, 0, 92, NULL);
168         zfcp_erp_wait(adapter);
169         zfcp_port_put(port);
170         zfcp_port_dequeue(port);
171  out:
172         up(&zfcp_data.config_sema);
173         return retval ? retval : (ssize_t) count;
174 }
175 static ZFCP_DEV_ATTR(adapter, port_remove, S_IWUSR, NULL,
176                      zfcp_sysfs_port_remove_store);
177
178 static struct attribute *zfcp_adapter_attrs[] = {
179         &dev_attr_adapter_failed.attr,
180         &dev_attr_adapter_in_recovery.attr,
181         &dev_attr_adapter_port_remove.attr,
182         &dev_attr_adapter_port_rescan.attr,
183         &dev_attr_adapter_peer_wwnn.attr,
184         &dev_attr_adapter_peer_wwpn.attr,
185         &dev_attr_adapter_peer_d_id.attr,
186         &dev_attr_adapter_card_version.attr,
187         &dev_attr_adapter_lic_version.attr,
188         &dev_attr_adapter_status.attr,
189         &dev_attr_adapter_hardware_version.attr,
190         NULL
191 };
192
193 struct attribute_group zfcp_sysfs_adapter_attrs = {
194         .attrs = zfcp_adapter_attrs,
195 };
196
197 static ssize_t zfcp_sysfs_unit_add_store(struct device *dev,
198                                          struct device_attribute *attr,
199                                          const char *buf, size_t count)
200 {
201         struct zfcp_port *port = dev_get_drvdata(dev);
202         struct zfcp_unit *unit;
203         fcp_lun_t fcp_lun;
204         int retval = -EINVAL;
205
206         down(&zfcp_data.config_sema);
207         if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_REMOVE) {
208                 retval = -EBUSY;
209                 goto out;
210         }
211
212         if (strict_strtoull(buf, 0, &fcp_lun))
213                 goto out;
214
215         unit = zfcp_unit_enqueue(port, fcp_lun);
216         if (IS_ERR(unit))
217                 goto out;
218
219         retval = 0;
220
221         zfcp_erp_unit_reopen(unit, 0, 94, NULL);
222         zfcp_erp_wait(unit->port->adapter);
223         zfcp_unit_put(unit);
224 out:
225         up(&zfcp_data.config_sema);
226         return retval ? retval : (ssize_t) count;
227 }
228 static DEVICE_ATTR(unit_add, S_IWUSR, NULL, zfcp_sysfs_unit_add_store);
229
230 static ssize_t zfcp_sysfs_unit_remove_store(struct device *dev,
231                                             struct device_attribute *attr,
232                                             const char *buf, size_t count)
233 {
234         struct zfcp_port *port = dev_get_drvdata(dev);
235         struct zfcp_unit *unit;
236         fcp_lun_t fcp_lun;
237         int retval = 0;
238
239         down(&zfcp_data.config_sema);
240         if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_REMOVE) {
241                 retval = -EBUSY;
242                 goto out;
243         }
244
245         if (strict_strtoull(buf, 0, &fcp_lun)) {
246                 retval = -EINVAL;
247                 goto out;
248         }
249
250         write_lock_irq(&zfcp_data.config_lock);
251         unit = zfcp_get_unit_by_lun(port, fcp_lun);
252         if (unit && (atomic_read(&unit->refcount) == 0)) {
253                 zfcp_unit_get(unit);
254                 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
255                 list_move(&unit->list, &port->unit_remove_lh);
256         } else
257                 unit = NULL;
258
259         write_unlock_irq(&zfcp_data.config_lock);
260
261         if (!unit) {
262                 retval = -ENXIO;
263                 goto out;
264         }
265
266         zfcp_erp_unit_shutdown(unit, 0, 95, NULL);
267         zfcp_erp_wait(unit->port->adapter);
268         zfcp_unit_put(unit);
269         zfcp_unit_dequeue(unit);
270 out:
271         up(&zfcp_data.config_sema);
272         return retval ? retval : (ssize_t) count;
273 }
274 static DEVICE_ATTR(unit_remove, S_IWUSR, NULL, zfcp_sysfs_unit_remove_store);
275
276 static struct attribute *zfcp_port_ns_attrs[] = {
277         &dev_attr_port_failed.attr,
278         &dev_attr_port_in_recovery.attr,
279         &dev_attr_port_status.attr,
280         &dev_attr_port_access_denied.attr,
281         NULL
282 };
283
284 /**
285  * zfcp_sysfs_ns_port_attrs - sysfs attributes for nameserver
286  */
287 struct attribute_group zfcp_sysfs_ns_port_attrs = {
288         .attrs = zfcp_port_ns_attrs,
289 };
290
291 static struct attribute *zfcp_port_no_ns_attrs[] = {
292         &dev_attr_unit_add.attr,
293         &dev_attr_unit_remove.attr,
294         &dev_attr_port_failed.attr,
295         &dev_attr_port_in_recovery.attr,
296         &dev_attr_port_status.attr,
297         &dev_attr_port_access_denied.attr,
298         NULL
299 };
300
301 /**
302  * zfcp_sysfs_port_attrs - sysfs attributes for all other ports
303  */
304 struct attribute_group zfcp_sysfs_port_attrs = {
305         .attrs = zfcp_port_no_ns_attrs,
306 };
307
308 static struct attribute *zfcp_unit_attrs[] = {
309         &dev_attr_unit_failed.attr,
310         &dev_attr_unit_in_recovery.attr,
311         &dev_attr_unit_status.attr,
312         &dev_attr_unit_access_denied.attr,
313         &dev_attr_unit_access_shared.attr,
314         &dev_attr_unit_access_readonly.attr,
315         NULL
316 };
317
318 struct attribute_group zfcp_sysfs_unit_attrs = {
319         .attrs = zfcp_unit_attrs,
320 };
321
322 #define ZFCP_DEFINE_LATENCY_ATTR(_name)                                 \
323 static ssize_t                                                          \
324 zfcp_sysfs_unit_##_name##_latency_show(struct device *dev,              \
325                                        struct device_attribute *attr,   \
326                                        char *buf) {                     \
327         struct scsi_device *sdev = to_scsi_device(dev);                 \
328         struct zfcp_unit *unit = sdev->hostdata;                        \
329         struct zfcp_latencies *lat = &unit->latencies;                  \
330         struct zfcp_adapter *adapter = unit->port->adapter;             \
331         unsigned long flags;                                            \
332         unsigned long long fsum, fmin, fmax, csum, cmin, cmax, cc;      \
333                                                                         \
334         spin_lock_irqsave(&lat->lock, flags);                           \
335         fsum = lat->_name.fabric.sum * adapter->timer_ticks;            \
336         fmin = lat->_name.fabric.min * adapter->timer_ticks;            \
337         fmax = lat->_name.fabric.max * adapter->timer_ticks;            \
338         csum = lat->_name.channel.sum * adapter->timer_ticks;           \
339         cmin = lat->_name.channel.min * adapter->timer_ticks;           \
340         cmax = lat->_name.channel.max * adapter->timer_ticks;           \
341         cc  = lat->_name.counter;                                       \
342         spin_unlock_irqrestore(&lat->lock, flags);                      \
343                                                                         \
344         do_div(fsum, 1000);                                             \
345         do_div(fmin, 1000);                                             \
346         do_div(fmax, 1000);                                             \
347         do_div(csum, 1000);                                             \
348         do_div(cmin, 1000);                                             \
349         do_div(cmax, 1000);                                             \
350                                                                         \
351         return sprintf(buf, "%llu %llu %llu %llu %llu %llu %llu\n",     \
352                        fmin, fmax, fsum, cmin, cmax, csum, cc);         \
353 }                                                                       \
354 static ssize_t                                                          \
355 zfcp_sysfs_unit_##_name##_latency_store(struct device *dev,             \
356                                         struct device_attribute *attr,  \
357                                         const char *buf, size_t count)  \
358 {                                                                       \
359         struct scsi_device *sdev = to_scsi_device(dev);                 \
360         struct zfcp_unit *unit = sdev->hostdata;                        \
361         struct zfcp_latencies *lat = &unit->latencies;                  \
362         unsigned long flags;                                            \
363                                                                         \
364         spin_lock_irqsave(&lat->lock, flags);                           \
365         lat->_name.fabric.sum = 0;                                      \
366         lat->_name.fabric.min = 0xFFFFFFFF;                             \
367         lat->_name.fabric.max = 0;                                      \
368         lat->_name.channel.sum = 0;                                     \
369         lat->_name.channel.min = 0xFFFFFFFF;                            \
370         lat->_name.channel.max = 0;                                     \
371         lat->_name.counter = 0;                                         \
372         spin_unlock_irqrestore(&lat->lock, flags);                      \
373                                                                         \
374         return (ssize_t) count;                                         \
375 }                                                                       \
376 static DEVICE_ATTR(_name##_latency, S_IWUSR | S_IRUGO,                  \
377                    zfcp_sysfs_unit_##_name##_latency_show,              \
378                    zfcp_sysfs_unit_##_name##_latency_store);
379
380 ZFCP_DEFINE_LATENCY_ATTR(read);
381 ZFCP_DEFINE_LATENCY_ATTR(write);
382 ZFCP_DEFINE_LATENCY_ATTR(cmd);
383
384 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value)                   \
385 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev,       \
386                                               struct device_attribute *attr,\
387                                               char *buf)                 \
388 {                                                                        \
389         struct scsi_device *sdev  = to_scsi_device(dev);                 \
390         struct zfcp_unit *unit = sdev->hostdata;                         \
391                                                                          \
392         return sprintf(buf, _format, _value);                            \
393 }                                                                        \
394 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
395
396 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n",
397         unit->port->adapter->ccw_device->dev.bus_id);
398 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n", unit->port->wwpn);
399 ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n", unit->fcp_lun);
400
401 struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
402         &dev_attr_fcp_lun,
403         &dev_attr_wwpn,
404         &dev_attr_hba_id,
405         &dev_attr_read_latency,
406         &dev_attr_write_latency,
407         &dev_attr_cmd_latency,
408         NULL
409 };
410
411 static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
412                                             struct device_attribute *attr,
413                                             char *buf)
414 {
415         struct Scsi_Host *scsi_host = dev_to_shost(dev);
416         struct fsf_qtcb_bottom_port *qtcb_port;
417         struct zfcp_adapter *adapter;
418         int retval;
419
420         adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
421         if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
422                 return -EOPNOTSUPP;
423
424         qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
425         if (!qtcb_port)
426                 return -ENOMEM;
427
428         retval = zfcp_fsf_exchange_port_data_sync(adapter, qtcb_port);
429         if (!retval)
430                 retval = sprintf(buf, "%u %u %u\n", qtcb_port->cp_util,
431                                  qtcb_port->cb_util, qtcb_port->a_util);
432         kfree(qtcb_port);
433         return retval;
434 }
435 static DEVICE_ATTR(utilization, S_IRUGO, zfcp_sysfs_adapter_util_show, NULL);
436
437 static int zfcp_sysfs_adapter_ex_config(struct device *dev,
438                                         struct fsf_statistics_info *stat_inf)
439 {
440         struct Scsi_Host *scsi_host = dev_to_shost(dev);
441         struct fsf_qtcb_bottom_config *qtcb_config;
442         struct zfcp_adapter *adapter;
443         int retval;
444
445         adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
446         if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
447                 return -EOPNOTSUPP;
448
449         qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
450                               GFP_KERNEL);
451         if (!qtcb_config)
452                 return -ENOMEM;
453
454         retval = zfcp_fsf_exchange_config_data_sync(adapter, qtcb_config);
455         if (!retval)
456                 *stat_inf = qtcb_config->stat_info;
457
458         kfree(qtcb_config);
459         return retval;
460 }
461
462 #define ZFCP_SHOST_ATTR(_name, _format, _arg...)                        \
463 static ssize_t zfcp_sysfs_adapter_##_name##_show(struct device *dev,    \
464                                                  struct device_attribute *attr,\
465                                                  char *buf)             \
466 {                                                                       \
467         struct fsf_statistics_info stat_info;                           \
468         int retval;                                                     \
469                                                                         \
470         retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);         \
471         if (retval)                                                     \
472                 return retval;                                          \
473                                                                         \
474         return sprintf(buf, _format, ## _arg);                          \
475 }                                                                       \
476 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_adapter_##_name##_show, NULL);
477
478 ZFCP_SHOST_ATTR(requests, "%llu %llu %llu\n",
479                 (unsigned long long) stat_info.input_req,
480                 (unsigned long long) stat_info.output_req,
481                 (unsigned long long) stat_info.control_req);
482
483 ZFCP_SHOST_ATTR(megabytes, "%llu %llu\n",
484                 (unsigned long long) stat_info.input_mb,
485                 (unsigned long long) stat_info.output_mb);
486
487 ZFCP_SHOST_ATTR(seconds_active, "%llu\n",
488                 (unsigned long long) stat_info.seconds_act);
489
490 struct device_attribute *zfcp_sysfs_shost_attrs[] = {
491         &dev_attr_utilization,
492         &dev_attr_requests,
493         &dev_attr_megabytes,
494         &dev_attr_seconds_active,
495         NULL
496 };