Merge with git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
[linux-2.6] / drivers / s390 / scsi / zfcp_sysfs_unit.c
1 /*
2  * linux/drivers/s390/scsi/zfcp_sysfs_unit.c
3  *
4  * FCP adapter driver for IBM eServer zSeries
5  *
6  * sysfs unit related routines
7  *
8  * (C) Copyright IBM Corp. 2003, 2004
9  *
10  * Authors:
11  *      Martin Peschke <mpeschke@de.ibm.com>
12  *      Heiko Carstens <heiko.carstens@de.ibm.com>
13  *      Andreas Herrmann <aherrman@de.ibm.com>
14  *      Volker Sameske <sameske@de.ibm.com>
15  *
16  * This program is free software; you can redistribute it and/or modify
17  * it under the terms of the GNU General Public License as published by
18  * the Free Software Foundation; either version 2, or (at your option)
19  * any later version.
20  *
21  * This program is distributed in the hope that it will be useful,
22  * but WITHOUT ANY WARRANTY; without even the implied warranty of
23  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24  * GNU General Public License for more details.
25  *
26  * You should have received a copy of the GNU General Public License
27  * along with this program; if not, write to the Free Software
28  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29  */
30
31 #include "zfcp_ext.h"
32
33 #define ZFCP_LOG_AREA                   ZFCP_LOG_AREA_CONFIG
34
35 /**
36  * zfcp_sysfs_unit_release - gets called when a struct device unit is released
37  * @dev: pointer to belonging device
38  */
39 void
40 zfcp_sysfs_unit_release(struct device *dev)
41 {
42         kfree(dev);
43 }
44
45 /**
46  * ZFCP_DEFINE_UNIT_ATTR
47  * @_name:   name of show attribute
48  * @_format: format string
49  * @_value:  value to print
50  *
51  * Generates attribute for a unit.
52  */
53 #define ZFCP_DEFINE_UNIT_ATTR(_name, _format, _value)                    \
54 static ssize_t zfcp_sysfs_unit_##_name##_show(struct device *dev, struct device_attribute *attr,        \
55                                               char *buf)                 \
56 {                                                                        \
57         struct zfcp_unit *unit;                                          \
58                                                                          \
59         unit = dev_get_drvdata(dev);                                     \
60         return sprintf(buf, _format, _value);                            \
61 }                                                                        \
62                                                                          \
63 static DEVICE_ATTR(_name, S_IRUGO, zfcp_sysfs_unit_##_name##_show, NULL);
64
65 ZFCP_DEFINE_UNIT_ATTR(status, "0x%08x\n", atomic_read(&unit->status));
66 ZFCP_DEFINE_UNIT_ATTR(in_recovery, "%d\n", atomic_test_mask
67                       (ZFCP_STATUS_COMMON_ERP_INUSE, &unit->status));
68 ZFCP_DEFINE_UNIT_ATTR(access_denied, "%d\n", atomic_test_mask
69                       (ZFCP_STATUS_COMMON_ACCESS_DENIED, &unit->status));
70 ZFCP_DEFINE_UNIT_ATTR(access_shared, "%d\n", atomic_test_mask
71                       (ZFCP_STATUS_UNIT_SHARED, &unit->status));
72 ZFCP_DEFINE_UNIT_ATTR(access_readonly, "%d\n", atomic_test_mask
73                       (ZFCP_STATUS_UNIT_READONLY, &unit->status));
74
75 /**
76  * zfcp_sysfs_unit_failed_store - failed state of unit
77  * @dev: pointer to belonging device
78  * @buf: pointer to input buffer
79  * @count: number of bytes in buffer
80  *
81  * Store function of the "failed" attribute of a unit.
82  * If a "0" gets written to "failed", error recovery will be
83  * started for the belonging unit.
84  */
85 static ssize_t
86 zfcp_sysfs_unit_failed_store(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
87 {
88         struct zfcp_unit *unit;
89         unsigned int val;
90         char *endp;
91         int retval = 0;
92
93         down(&zfcp_data.config_sema);
94         unit = dev_get_drvdata(dev);
95         if (atomic_test_mask(ZFCP_STATUS_COMMON_REMOVE, &unit->status)) {
96                 retval = -EBUSY;
97                 goto out;
98         }
99
100         val = simple_strtoul(buf, &endp, 0);
101         if (((endp + 1) < (buf + count)) || (val != 0)) {
102                 retval = -EINVAL;
103                 goto out;
104         }
105
106         zfcp_erp_modify_unit_status(unit, ZFCP_STATUS_COMMON_RUNNING, ZFCP_SET);
107         zfcp_erp_unit_reopen(unit, ZFCP_STATUS_COMMON_ERP_FAILED);
108         zfcp_erp_wait(unit->port->adapter);
109  out:
110         up(&zfcp_data.config_sema);
111         return retval ? retval : (ssize_t) count;
112 }
113
114 /**
115  * zfcp_sysfs_unit_failed_show - failed state of unit
116  * @dev: pointer to belonging device
117  * @buf: pointer to input buffer
118  *
119  * Show function of "failed" attribute of unit. Will be
120  * "0" if unit is working, otherwise "1".
121  */
122 static ssize_t
123 zfcp_sysfs_unit_failed_show(struct device *dev, struct device_attribute *attr, char *buf)
124 {
125         struct zfcp_unit *unit;
126
127         unit = dev_get_drvdata(dev);
128         if (atomic_test_mask(ZFCP_STATUS_COMMON_ERP_FAILED, &unit->status))
129                 return sprintf(buf, "1\n");
130         else
131                 return sprintf(buf, "0\n");
132 }
133
134 static DEVICE_ATTR(failed, S_IWUSR | S_IRUGO, zfcp_sysfs_unit_failed_show,
135                    zfcp_sysfs_unit_failed_store);
136
137 static struct attribute *zfcp_unit_attrs[] = {
138         &dev_attr_failed.attr,
139         &dev_attr_in_recovery.attr,
140         &dev_attr_status.attr,
141         &dev_attr_access_denied.attr,
142         &dev_attr_access_shared.attr,
143         &dev_attr_access_readonly.attr,
144         NULL
145 };
146
147 static struct attribute_group zfcp_unit_attr_group = {
148         .attrs = zfcp_unit_attrs,
149 };
150
151 /** 
152  * zfcp_sysfs_create_unit_files - create sysfs unit files
153  * @dev: pointer to belonging device
154  *
155  * Create all attributes of the sysfs representation of a unit.
156  */
157 int
158 zfcp_sysfs_unit_create_files(struct device *dev)
159 {
160         return sysfs_create_group(&dev->kobj, &zfcp_unit_attr_group);
161 }
162
163 /** 
164  * zfcp_sysfs_remove_unit_files - remove sysfs unit files
165  * @dev: pointer to belonging device
166  *
167  * Remove all attributes of the sysfs representation of a unit.
168  */
169 void
170 zfcp_sysfs_unit_remove_files(struct device *dev)
171 {
172         sysfs_remove_group(&dev->kobj, &zfcp_unit_attr_group);
173 }
174
175 #undef ZFCP_LOG_AREA