[S390] cio: observe chpid valid flag
[pandora-kernel.git] / drivers / s390 / cio / chp.c
1 /*
2  *  drivers/s390/cio/chp.c
3  *
4  *    Copyright IBM Corp. 1999,2007
5  *    Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
6  *               Arnd Bergmann (arndb@de.ibm.com)
7  *               Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
8  */
9
10 #include <linux/bug.h>
11 #include <linux/workqueue.h>
12 #include <linux/spinlock.h>
13 #include <asm/errno.h>
14
15 #include "chpid.h"
16 #include "cio.h"
17 #include "css.h"
18 #include "ioasm.h"
19 #include "cio_debug.h"
20 #include "chp.h"
21
22 #define to_channelpath(device) container_of(device, struct channel_path, dev)
23
24 /* Return channel_path struct for given chpid. */
25 static inline struct channel_path *chpid_to_chp(struct chp_id chpid)
26 {
27         return css[chpid.cssid]->chps[chpid.id];
28 }
29
30 /* Set vary state for given chpid. */
31 static void set_chp_logically_online(struct chp_id chpid, int onoff)
32 {
33         chpid_to_chp(chpid)->state = onoff;
34 }
35
36 /* On succes return 0 if channel-path is varied offline, 1 if it is varied
37  * online. Return -ENODEV if channel-path is not registered. */
38 int chp_get_status(struct chp_id chpid)
39 {
40         return (chpid_to_chp(chpid) ? chpid_to_chp(chpid)->state : -ENODEV);
41 }
42
43 /**
44  * chp_get_sch_opm - return opm for subchannel
45  * @sch: subchannel
46  *
47  * Calculate and return the operational path mask (opm) based on the chpids
48  * used by the subchannel and the status of the associated channel-paths.
49  */
50 u8 chp_get_sch_opm(struct subchannel *sch)
51 {
52         struct chp_id chpid;
53         int opm;
54         int i;
55
56         opm = 0;
57         chp_id_init(&chpid);
58         for (i=0; i < 8; i++) {
59                 opm <<= 1;
60                 chpid.id = sch->schib.pmcw.chpid[i];
61                 if (chp_get_status(chpid) != 0)
62                         opm |= 1;
63         }
64         return opm;
65 }
66
67 /**
68  * chp_is_registered - check if a channel-path is registered
69  * @chpid: channel-path ID
70  *
71  * Return non-zero if a channel-path with the given chpid is registered,
72  * zero otherwise.
73  */
74 int chp_is_registered(struct chp_id chpid)
75 {
76         return chpid_to_chp(chpid) != NULL;
77 }
78
79 /*
80  * Function: s390_vary_chpid
81  * Varies the specified chpid online or offline
82  */
83 static int s390_vary_chpid(struct chp_id chpid, int on)
84 {
85         char dbf_text[15];
86         int status;
87
88         sprintf(dbf_text, on?"varyon%x.%02x":"varyoff%x.%02x", chpid.cssid,
89                 chpid.id);
90         CIO_TRACE_EVENT( 2, dbf_text);
91
92         status = chp_get_status(chpid);
93         if (status < 0) {
94                 printk(KERN_ERR "Can't vary unknown chpid %x.%02x\n",
95                        chpid.cssid, chpid.id);
96                 return -EINVAL;
97         }
98
99         if (!on && !status) {
100                 printk(KERN_ERR "chpid %x.%02x is already offline\n",
101                        chpid.cssid, chpid.id);
102                 return -EINVAL;
103         }
104
105         set_chp_logically_online(chpid, on);
106         chsc_chp_vary(chpid, on);
107         return 0;
108 }
109
110 /*
111  * Channel measurement related functions
112  */
113 static ssize_t chp_measurement_chars_read(struct kobject *kobj, char *buf,
114                                           loff_t off, size_t count)
115 {
116         struct channel_path *chp;
117         unsigned int size;
118
119         chp = to_channelpath(container_of(kobj, struct device, kobj));
120         if (!chp->cmg_chars)
121                 return 0;
122
123         size = sizeof(struct cmg_chars);
124
125         if (off > size)
126                 return 0;
127         if (off + count > size)
128                 count = size - off;
129         memcpy(buf, chp->cmg_chars + off, count);
130         return count;
131 }
132
133 static struct bin_attribute chp_measurement_chars_attr = {
134         .attr = {
135                 .name = "measurement_chars",
136                 .mode = S_IRUSR,
137                 .owner = THIS_MODULE,
138         },
139         .size = sizeof(struct cmg_chars),
140         .read = chp_measurement_chars_read,
141 };
142
143 static void chp_measurement_copy_block(struct cmg_entry *buf,
144                                        struct channel_subsystem *css,
145                                        struct chp_id chpid)
146 {
147         void *area;
148         struct cmg_entry *entry, reference_buf;
149         int idx;
150
151         if (chpid.id < 128) {
152                 area = css->cub_addr1;
153                 idx = chpid.id;
154         } else {
155                 area = css->cub_addr2;
156                 idx = chpid.id - 128;
157         }
158         entry = area + (idx * sizeof(struct cmg_entry));
159         do {
160                 memcpy(buf, entry, sizeof(*entry));
161                 memcpy(&reference_buf, entry, sizeof(*entry));
162         } while (reference_buf.values[0] != buf->values[0]);
163 }
164
165 static ssize_t chp_measurement_read(struct kobject *kobj, char *buf,
166                                     loff_t off, size_t count)
167 {
168         struct channel_path *chp;
169         struct channel_subsystem *css;
170         unsigned int size;
171
172         chp = to_channelpath(container_of(kobj, struct device, kobj));
173         css = to_css(chp->dev.parent);
174
175         size = sizeof(struct cmg_entry);
176
177         /* Only allow single reads. */
178         if (off || count < size)
179                 return 0;
180         chp_measurement_copy_block((struct cmg_entry *)buf, css, chp->chpid);
181         count = size;
182         return count;
183 }
184
185 static struct bin_attribute chp_measurement_attr = {
186         .attr = {
187                 .name = "measurement",
188                 .mode = S_IRUSR,
189                 .owner = THIS_MODULE,
190         },
191         .size = sizeof(struct cmg_entry),
192         .read = chp_measurement_read,
193 };
194
195 void chp_remove_cmg_attr(struct channel_path *chp)
196 {
197         device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
198         device_remove_bin_file(&chp->dev, &chp_measurement_attr);
199 }
200
201 int chp_add_cmg_attr(struct channel_path *chp)
202 {
203         int ret;
204
205         ret = device_create_bin_file(&chp->dev, &chp_measurement_chars_attr);
206         if (ret)
207                 return ret;
208         ret = device_create_bin_file(&chp->dev, &chp_measurement_attr);
209         if (ret)
210                 device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
211         return ret;
212 }
213
214 /*
215  * Files for the channel path entries.
216  */
217 static ssize_t chp_status_show(struct device *dev,
218                                struct device_attribute *attr, char *buf)
219 {
220         struct channel_path *chp = container_of(dev, struct channel_path, dev);
221
222         if (!chp)
223                 return 0;
224         return (chp_get_status(chp->chpid) ? sprintf(buf, "online\n") :
225                 sprintf(buf, "offline\n"));
226 }
227
228 static ssize_t chp_status_write(struct device *dev,
229                                 struct device_attribute *attr,
230                                 const char *buf, size_t count)
231 {
232         struct channel_path *cp = container_of(dev, struct channel_path, dev);
233         char cmd[10];
234         int num_args;
235         int error;
236
237         num_args = sscanf(buf, "%5s", cmd);
238         if (!num_args)
239                 return count;
240
241         if (!strnicmp(cmd, "on", 2) || !strcmp(cmd, "1"))
242                 error = s390_vary_chpid(cp->chpid, 1);
243         else if (!strnicmp(cmd, "off", 3) || !strcmp(cmd, "0"))
244                 error = s390_vary_chpid(cp->chpid, 0);
245         else
246                 error = -EINVAL;
247
248         return error < 0 ? error : count;
249
250 }
251
252 static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
253
254 static ssize_t chp_type_show(struct device *dev, struct device_attribute *attr,
255                              char *buf)
256 {
257         struct channel_path *chp = container_of(dev, struct channel_path, dev);
258
259         if (!chp)
260                 return 0;
261         return sprintf(buf, "%x\n", chp->desc.desc);
262 }
263
264 static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
265
266 static ssize_t chp_cmg_show(struct device *dev, struct device_attribute *attr,
267                             char *buf)
268 {
269         struct channel_path *chp = to_channelpath(dev);
270
271         if (!chp)
272                 return 0;
273         if (chp->cmg == -1) /* channel measurements not available */
274                 return sprintf(buf, "unknown\n");
275         return sprintf(buf, "%x\n", chp->cmg);
276 }
277
278 static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
279
280 static ssize_t chp_shared_show(struct device *dev,
281                                struct device_attribute *attr, char *buf)
282 {
283         struct channel_path *chp = to_channelpath(dev);
284
285         if (!chp)
286                 return 0;
287         if (chp->shared == -1) /* channel measurements not available */
288                 return sprintf(buf, "unknown\n");
289         return sprintf(buf, "%x\n", chp->shared);
290 }
291
292 static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
293
294 static struct attribute * chp_attrs[] = {
295         &dev_attr_status.attr,
296         &dev_attr_type.attr,
297         &dev_attr_cmg.attr,
298         &dev_attr_shared.attr,
299         NULL,
300 };
301
302 static struct attribute_group chp_attr_group = {
303         .attrs = chp_attrs,
304 };
305
306 static void chp_release(struct device *dev)
307 {
308         struct channel_path *cp;
309
310         cp = container_of(dev, struct channel_path, dev);
311         kfree(cp);
312 }
313
314 /**
315  * chp_new - register a new channel-path
316  * @chpid - channel-path ID
317  *
318  * Create and register data structure representing new channel-path. Return
319  * zero on success, non-zero otherwise.
320  */
321 int chp_new(struct chp_id chpid)
322 {
323         struct channel_path *chp;
324         int ret;
325
326         chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
327         if (!chp)
328                 return -ENOMEM;
329
330         /* fill in status, etc. */
331         chp->chpid = chpid;
332         chp->state = 1;
333         chp->dev.parent = &css[chpid.cssid]->device;
334         chp->dev.release = chp_release;
335         snprintf(chp->dev.bus_id, BUS_ID_SIZE, "chp%x.%02x", chpid.cssid,
336                  chpid.id);
337
338         /* Obtain channel path description and fill it in. */
339         ret = chsc_determine_channel_path_description(chpid, &chp->desc);
340         if (ret)
341                 goto out_free;
342         if ((chp->desc.flags & 0x80) == 0) {
343                 ret = -ENODEV;
344                 goto out_free;
345         }
346         /* Get channel-measurement characteristics. */
347         if (css_characteristics_avail && css_chsc_characteristics.scmc
348             && css_chsc_characteristics.secm) {
349                 ret = chsc_get_channel_measurement_chars(chp);
350                 if (ret)
351                         goto out_free;
352         } else {
353                 static int msg_done;
354
355                 if (!msg_done) {
356                         printk(KERN_WARNING "cio: Channel measurements not "
357                                "available, continuing.\n");
358                         msg_done = 1;
359                 }
360                 chp->cmg = -1;
361         }
362
363         /* make it known to the system */
364         ret = device_register(&chp->dev);
365         if (ret) {
366                 printk(KERN_WARNING "%s: could not register %x.%02x\n",
367                        __func__, chpid.cssid, chpid.id);
368                 goto out_free;
369         }
370         ret = sysfs_create_group(&chp->dev.kobj, &chp_attr_group);
371         if (ret) {
372                 device_unregister(&chp->dev);
373                 goto out_free;
374         }
375         mutex_lock(&css[chpid.cssid]->mutex);
376         if (css[chpid.cssid]->cm_enabled) {
377                 ret = chp_add_cmg_attr(chp);
378                 if (ret) {
379                         sysfs_remove_group(&chp->dev.kobj, &chp_attr_group);
380                         device_unregister(&chp->dev);
381                         mutex_unlock(&css[chpid.cssid]->mutex);
382                         goto out_free;
383                 }
384         }
385         css[chpid.cssid]->chps[chpid.id] = chp;
386         mutex_unlock(&css[chpid.cssid]->mutex);
387         return ret;
388 out_free:
389         kfree(chp);
390         return ret;
391 }
392
393 /**
394  * chp_get_chp_desc - return newly allocated channel-path description
395  * @chpid: channel-path ID
396  *
397  * On success return a newly allocated copy of the channel-path description
398  * data associated with the given channel-path ID. Return %NULL on error.
399  */
400 void *chp_get_chp_desc(struct chp_id chpid)
401 {
402         struct channel_path *chp;
403         struct channel_path_desc *desc;
404
405         chp = chpid_to_chp(chpid);
406         if (!chp)
407                 return NULL;
408         desc = kmalloc(sizeof(struct channel_path_desc), GFP_KERNEL);
409         if (!desc)
410                 return NULL;
411         memcpy(desc, &chp->desc, sizeof(struct channel_path_desc));
412         return desc;
413 }
414
415 /**
416  * chp_process_crw - process channel-path status change
417  * @id: channel-path ID number
418  * @status: non-zero if channel-path has become available, zero otherwise
419  *
420  * Handle channel-report-words indicating that the status of a channel-path
421  * has changed.
422  */
423 int chp_process_crw(int id, int status)
424 {
425         struct chp_id chpid;
426
427         chp_id_init(&chpid);
428         chpid.id = id;
429         if (status) {
430                 if (!chp_is_registered(chpid))
431                         chp_new(chpid);
432                 return chsc_chp_online(chpid);
433         } else {
434                 chsc_chp_offline(chpid);
435                 return 0;
436         }
437 }