[SCSI] zfcp: Simplify zfcp data structures
[linux-2.6.git] / 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                  (unsigned long long) adapter->peer_wwnn);
30 ZFCP_DEFINE_ATTR(zfcp_adapter, adapter, peer_wwpn, "0x%016llx\n",
31                  (unsigned long long) 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         u64 wwpn;
139         int retval = 0;
140         LIST_HEAD(port_remove_lh);
141
142         down(&zfcp_data.config_sema);
143         if (atomic_read(&adapter->status) & ZFCP_STATUS_COMMON_REMOVE) {
144                 retval = -EBUSY;
145                 goto out;
146         }
147
148         if (strict_strtoull(buf, 0, (unsigned long long *) &wwpn)) {
149                 retval = -EINVAL;
150                 goto out;
151         }
152
153         write_lock_irq(&zfcp_data.config_lock);
154         port = zfcp_get_port_by_wwpn(adapter, wwpn);
155         if (port && (atomic_read(&port->refcount) == 0)) {
156                 zfcp_port_get(port);
157                 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &port->status);
158                 list_move(&port->list, &port_remove_lh);
159         } else
160                 port = NULL;
161         write_unlock_irq(&zfcp_data.config_lock);
162
163         if (!port) {
164                 retval = -ENXIO;
165                 goto out;
166         }
167
168         zfcp_erp_port_shutdown(port, 0, 92, NULL);
169         zfcp_erp_wait(adapter);
170         zfcp_port_put(port);
171         zfcp_port_dequeue(port);
172  out:
173         up(&zfcp_data.config_sema);
174         return retval ? retval : (ssize_t) count;
175 }
176 static ZFCP_DEV_ATTR(adapter, port_remove, S_IWUSR, NULL,
177                      zfcp_sysfs_port_remove_store);
178
179 static struct attribute *zfcp_adapter_attrs[] = {
180         &dev_attr_adapter_failed.attr,
181         &dev_attr_adapter_in_recovery.attr,
182         &dev_attr_adapter_port_remove.attr,
183         &dev_attr_adapter_port_rescan.attr,
184         &dev_attr_adapter_peer_wwnn.attr,
185         &dev_attr_adapter_peer_wwpn.attr,
186         &dev_attr_adapter_peer_d_id.attr,
187         &dev_attr_adapter_card_version.attr,
188         &dev_attr_adapter_lic_version.attr,
189         &dev_attr_adapter_status.attr,
190         &dev_attr_adapter_hardware_version.attr,
191         NULL
192 };
193
194 struct attribute_group zfcp_sysfs_adapter_attrs = {
195         .attrs = zfcp_adapter_attrs,
196 };
197
198 static ssize_t zfcp_sysfs_unit_add_store(struct device *dev,
199                                          struct device_attribute *attr,
200                                          const char *buf, size_t count)
201 {
202         struct zfcp_port *port = dev_get_drvdata(dev);
203         struct zfcp_unit *unit;
204         u64 fcp_lun;
205         int retval = -EINVAL;
206
207         down(&zfcp_data.config_sema);
208         if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_REMOVE) {
209                 retval = -EBUSY;
210                 goto out;
211         }
212
213         if (strict_strtoull(buf, 0, (unsigned long long *) &fcp_lun))
214                 goto out;
215
216         unit = zfcp_unit_enqueue(port, fcp_lun);
217         if (IS_ERR(unit))
218                 goto out;
219
220         retval = 0;
221
222         zfcp_erp_unit_reopen(unit, 0, 94, NULL);
223         zfcp_erp_wait(unit->port->adapter);
224         zfcp_unit_put(unit);
225 out:
226         up(&zfcp_data.config_sema);
227         return retval ? retval : (ssize_t) count;
228 }
229 static DEVICE_ATTR(unit_add, S_IWUSR, NULL, zfcp_sysfs_unit_add_store);
230
231 static ssize_t zfcp_sysfs_unit_remove_store(struct device *dev,
232                                             struct device_attribute *attr,
233                                             const char *buf, size_t count)
234 {
235         struct zfcp_port *port = dev_get_drvdata(dev);
236         struct zfcp_unit *unit;
237         u64 fcp_lun;
238         int retval = 0;
239         LIST_HEAD(unit_remove_lh);
240
241         down(&zfcp_data.config_sema);
242         if (atomic_read(&port->status) & ZFCP_STATUS_COMMON_REMOVE) {
243                 retval = -EBUSY;
244                 goto out;
245         }
246
247         if (strict_strtoull(buf, 0, (unsigned long long *) &fcp_lun)) {
248                 retval = -EINVAL;
249                 goto out;
250         }
251
252         write_lock_irq(&zfcp_data.config_lock);
253         unit = zfcp_get_unit_by_lun(port, fcp_lun);
254         if (unit && (atomic_read(&unit->refcount) == 0)) {
255                 zfcp_unit_get(unit);
256                 atomic_set_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status);
257                 list_move(&unit->list, &unit_remove_lh);
258         } else
259                 unit = NULL;
260
261         write_unlock_irq(&zfcp_data.config_lock);
262
263         if (!unit) {
264                 retval = -ENXIO;
265                 goto out;
266         }
267
268         zfcp_erp_unit_shutdown(unit, 0, 95, NULL);
269         zfcp_erp_wait(unit->port->adapter);
270         zfcp_unit_put(unit);
271         zfcp_unit_dequeue(unit);
272 out:
273         up(&zfcp_data.config_sema);
274         return retval ? retval : (ssize_t) count;
275 }
276 static DEVICE_ATTR(unit_remove, S_IWUSR, NULL, zfcp_sysfs_unit_remove_store);
277
278 static struct attribute *zfcp_port_attrs[] = {
279         &dev_attr_unit_add.attr,
280         &dev_attr_unit_remove.attr,
281         &dev_attr_port_failed.attr,
282         &dev_attr_port_in_recovery.attr,
283         &dev_attr_port_status.attr,
284         &dev_attr_port_access_denied.attr,
285         NULL
286 };
287
288 /**
289  * zfcp_sysfs_port_attrs - sysfs attributes for all other ports
290  */
291 struct attribute_group zfcp_sysfs_port_attrs = {
292         .attrs = zfcp_port_attrs,
293 };
294
295 static struct attribute *zfcp_unit_attrs[] = {
296         &dev_attr_unit_failed.attr,
297         &dev_attr_unit_in_recovery.attr,
298         &dev_attr_unit_status.attr,
299         &dev_attr_unit_access_denied.attr,
300         &dev_attr_unit_access_shared.attr,
301         &dev_attr_unit_access_readonly.attr,
302         NULL
303 };
304
305 struct attribute_group zfcp_sysfs_unit_attrs = {
306         .attrs = zfcp_unit_attrs,
307 };
308
309 #define ZFCP_DEFINE_LATENCY_ATTR(_name)                                 \
310 static ssize_t                                                          \
311 zfcp_sysfs_unit_##_name##_latency_show(struct device *dev,              \
312                                        struct device_attribute *attr,   \
313                                        char *buf) {                     \
314         struct scsi_device *sdev = to_scsi_device(dev);                 \
315         struct zfcp_unit *unit = sdev->hostdata;                        \
316         struct zfcp_latencies *lat = &unit->latencies;                  \
317         struct zfcp_adapter *adapter = unit->port->adapter;             \
318         unsigned long flags;                                            \
319         unsigned long long fsum, fmin, fmax, csum, cmin, cmax, cc;      \
320                                                                         \
321         spin_lock_irqsave(&lat->lock, flags);                           \
322         fsum = lat->_name.fabric.sum * adapter->timer_ticks;            \
323         fmin = lat->_name.fabric.min * adapter->timer_ticks;            \
324         fmax = lat->_name.fabric.max * adapter->timer_ticks;            \
325         csum = lat->_name.channel.sum * adapter->timer_ticks;           \
326         cmin = lat->_name.channel.min * adapter->timer_ticks;           \
327         cmax = lat->_name.channel.max * adapter->timer_ticks;           \
328         cc  = lat->_name.counter;                                       \
329         spin_unlock_irqrestore(&lat->lock, flags);                      \
330                                                                         \
331         do_div(fsum, 1000);                                             \
332         do_div(fmin, 1000);                                             \
333         do_div(fmax, 1000);                                             \
334         do_div(csum, 1000);                                             \
335         do_div(cmin, 1000);                                             \
336         do_div(cmax, 1000);                                             \
337                                                                         \
338         return sprintf(buf, "%llu %llu %llu %llu %llu %llu %llu\n",     \
339                        fmin, fmax, fsum, cmin, cmax, csum, cc);         \
340 }                                                                       \
341 static ssize_t                                                          \
342 zfcp_sysfs_unit_##_name##_latency_store(struct device *dev,             \
343                                         struct device_attribute *attr,  \
344                                         const char *buf, size_t count)  \
345 {                                                                       \
346         struct scsi_device *sdev = to_scsi_device(dev);                 \
347         struct zfcp_unit *unit = sdev->hostdata;                        \
348         struct zfcp_latencies *lat = &unit->latencies;                  \
349         unsigned long flags;                                            \
350                                                                         \
351         spin_lock_irqsave(&lat->lock, flags);                           \
352         lat->_name.fabric.sum = 0;                                      \
353         lat->_name.fabric.min = 0xFFFFFFFF;                             \
354         lat->_name.fabric.max = 0;                                      \
355         lat->_name.channel.sum = 0;                                     \
356         lat->_name.channel.min = 0xFFFFFFFF;                            \
357         lat->_name.channel.max = 0;                                     \
358         lat->_name.counter = 0;                                         \
359         spin_unlock_irqrestore(&lat->lock, flags);                      \
360                                                                         \
361         return (ssize_t) count;                                         \
362 }                                                                       \
363 static DEVICE_ATTR(_name##_latency, S_IWUSR | S_IRUGO,                  \
364                    zfcp_sysfs_unit_##_name##_latency_show,              \
365                    zfcp_sysfs_unit_##_name##_latency_store);
366
367 ZFCP_DEFINE_LATENCY_ATTR(read);
368 ZFCP_DEFINE_LATENCY_ATTR(write);
369 ZFCP_DEFINE_LATENCY_ATTR(cmd);
370
371 #define ZFCP_DEFINE_SCSI_ATTR(_name, _format, _value)                   \
372 static ssize_t zfcp_sysfs_scsi_##_name##_show(struct device *dev,       \
373                                               struct device_attribute *attr,\
374                                               char *buf)                 \
375 {                                                                        \
376         struct scsi_device *sdev  = to_scsi_device(dev);                 \
377         struct zfcp_unit *unit = sdev->hostdata;                         \
378                                                                          \
379         return sprintf(buf, _format, _value);                            \
380 }                                                                        \
381 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_scsi_##_name##_show, NULL);
382
383 ZFCP_DEFINE_SCSI_ATTR(hba_id, "%s\n",
384         unit->port->adapter->ccw_device->dev.bus_id);
385 ZFCP_DEFINE_SCSI_ATTR(wwpn, "0x%016llx\n",
386                       (unsigned long long) unit->port->wwpn);
387 ZFCP_DEFINE_SCSI_ATTR(fcp_lun, "0x%016llx\n",
388                       (unsigned long long) unit->fcp_lun);
389
390 struct device_attribute *zfcp_sysfs_sdev_attrs[] = {
391         &dev_attr_fcp_lun,
392         &dev_attr_wwpn,
393         &dev_attr_hba_id,
394         &dev_attr_read_latency,
395         &dev_attr_write_latency,
396         &dev_attr_cmd_latency,
397         NULL
398 };
399
400 static ssize_t zfcp_sysfs_adapter_util_show(struct device *dev,
401                                             struct device_attribute *attr,
402                                             char *buf)
403 {
404         struct Scsi_Host *scsi_host = dev_to_shost(dev);
405         struct fsf_qtcb_bottom_port *qtcb_port;
406         struct zfcp_adapter *adapter;
407         int retval;
408
409         adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
410         if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
411                 return -EOPNOTSUPP;
412
413         qtcb_port = kzalloc(sizeof(struct fsf_qtcb_bottom_port), GFP_KERNEL);
414         if (!qtcb_port)
415                 return -ENOMEM;
416
417         retval = zfcp_fsf_exchange_port_data_sync(adapter, qtcb_port);
418         if (!retval)
419                 retval = sprintf(buf, "%u %u %u\n", qtcb_port->cp_util,
420                                  qtcb_port->cb_util, qtcb_port->a_util);
421         kfree(qtcb_port);
422         return retval;
423 }
424 static DEVICE_ATTR(utilization, S_IRUGO, zfcp_sysfs_adapter_util_show, NULL);
425
426 static int zfcp_sysfs_adapter_ex_config(struct device *dev,
427                                         struct fsf_statistics_info *stat_inf)
428 {
429         struct Scsi_Host *scsi_host = dev_to_shost(dev);
430         struct fsf_qtcb_bottom_config *qtcb_config;
431         struct zfcp_adapter *adapter;
432         int retval;
433
434         adapter = (struct zfcp_adapter *) scsi_host->hostdata[0];
435         if (!(adapter->adapter_features & FSF_FEATURE_MEASUREMENT_DATA))
436                 return -EOPNOTSUPP;
437
438         qtcb_config = kzalloc(sizeof(struct fsf_qtcb_bottom_config),
439                               GFP_KERNEL);
440         if (!qtcb_config)
441                 return -ENOMEM;
442
443         retval = zfcp_fsf_exchange_config_data_sync(adapter, qtcb_config);
444         if (!retval)
445                 *stat_inf = qtcb_config->stat_info;
446
447         kfree(qtcb_config);
448         return retval;
449 }
450
451 #define ZFCP_SHOST_ATTR(_name, _format, _arg...)                        \
452 static ssize_t zfcp_sysfs_adapter_##_name##_show(struct device *dev,    \
453                                                  struct device_attribute *attr,\
454                                                  char *buf)             \
455 {                                                                       \
456         struct fsf_statistics_info stat_info;                           \
457         int retval;                                                     \
458                                                                         \
459         retval = zfcp_sysfs_adapter_ex_config(dev, &stat_info);         \
460         if (retval)                                                     \
461                 return retval;                                          \
462                                                                         \
463         return sprintf(buf, _format, ## _arg);                          \
464 }                                                                       \
465 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_adapter_##_name##_show, NULL);
466
467 ZFCP_SHOST_ATTR(requests, "%llu %llu %llu\n",
468                 (unsigned long long) stat_info.input_req,
469                 (unsigned long long) stat_info.output_req,
470                 (unsigned long long) stat_info.control_req);
471
472 ZFCP_SHOST_ATTR(megabytes, "%llu %llu\n",
473                 (unsigned long long) stat_info.input_mb,
474                 (unsigned long long) stat_info.output_mb);
475
476 ZFCP_SHOST_ATTR(seconds_active, "%llu\n",
477                 (unsigned long long) stat_info.seconds_act);
478
479 static ssize_t zfcp_sysfs_adapter_q_full_show(struct device *dev,
480                                               struct device_attribute *attr,
481                                               char *buf)
482 {
483         struct Scsi_Host *scsi_host = class_to_shost(dev);
484         struct zfcp_adapter *adapter =
485                 (struct zfcp_adapter *) scsi_host->hostdata[0];
486
487         return sprintf(buf, "%d\n", atomic_read(&adapter->qdio_outb_full));
488 }
489 static DEVICE_ATTR(queue_full, S_IRUGO, zfcp_sysfs_adapter_q_full_show, NULL);
490
491 struct device_attribute *zfcp_sysfs_shost_attrs[] = {
492         &dev_attr_utilization,
493         &dev_attr_requests,
494         &dev_attr_megabytes,
495         &dev_attr_seconds_active,
496         &dev_attr_queue_full,
497         NULL
498 };