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