2 * drivers/s390/cio/chp.c
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>
10 #include <linux/bug.h>
11 #include <linux/workqueue.h>
12 #include <linux/spinlock.h>
13 #include <asm/errno.h>
19 #include "cio_debug.h"
22 #define to_channelpath(device) container_of(device, struct channel_path, dev)
24 /* Return channel_path struct for given chpid. */
25 static inline struct channel_path *chpid_to_chp(struct chp_id chpid)
27 return css[chpid.cssid]->chps[chpid.id];
30 /* Set vary state for given chpid. */
31 static void set_chp_logically_online(struct chp_id chpid, int onoff)
33 chpid_to_chp(chpid)->state = onoff;
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)
40 return (chpid_to_chp(chpid) ? chpid_to_chp(chpid)->state : -ENODEV);
44 * chp_get_sch_opm - return opm for subchannel
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.
50 u8 chp_get_sch_opm(struct subchannel *sch)
58 for (i=0; i < 8; i++) {
60 chpid.id = sch->schib.pmcw.chpid[i];
61 if (chp_get_status(chpid) != 0)
68 * chp_is_registered - check if a channel-path is registered
69 * @chpid: channel-path ID
71 * Return non-zero if a channel-path with the given chpid is registered,
74 int chp_is_registered(struct chp_id chpid)
76 return chpid_to_chp(chpid) != NULL;
80 * Function: s390_vary_chpid
81 * Varies the specified chpid online or offline
83 static int s390_vary_chpid(struct chp_id chpid, int on)
88 sprintf(dbf_text, on?"varyon%x.%02x":"varyoff%x.%02x", chpid.cssid,
90 CIO_TRACE_EVENT( 2, dbf_text);
92 status = chp_get_status(chpid);
94 printk(KERN_ERR "Can't vary unknown chpid %x.%02x\n",
95 chpid.cssid, chpid.id);
100 printk(KERN_ERR "chpid %x.%02x is already offline\n",
101 chpid.cssid, chpid.id);
105 set_chp_logically_online(chpid, on);
106 chsc_chp_vary(chpid, on);
111 * Channel measurement related functions
113 static ssize_t chp_measurement_chars_read(struct kobject *kobj, char *buf,
114 loff_t off, size_t count)
116 struct channel_path *chp;
119 chp = to_channelpath(container_of(kobj, struct device, kobj));
123 size = sizeof(struct cmg_chars);
127 if (off + count > size)
129 memcpy(buf, chp->cmg_chars + off, count);
133 static struct bin_attribute chp_measurement_chars_attr = {
135 .name = "measurement_chars",
137 .owner = THIS_MODULE,
139 .size = sizeof(struct cmg_chars),
140 .read = chp_measurement_chars_read,
143 static void chp_measurement_copy_block(struct cmg_entry *buf,
144 struct channel_subsystem *css,
148 struct cmg_entry *entry, reference_buf;
151 if (chpid.id < 128) {
152 area = css->cub_addr1;
155 area = css->cub_addr2;
156 idx = chpid.id - 128;
158 entry = area + (idx * sizeof(struct cmg_entry));
160 memcpy(buf, entry, sizeof(*entry));
161 memcpy(&reference_buf, entry, sizeof(*entry));
162 } while (reference_buf.values[0] != buf->values[0]);
165 static ssize_t chp_measurement_read(struct kobject *kobj, char *buf,
166 loff_t off, size_t count)
168 struct channel_path *chp;
169 struct channel_subsystem *css;
172 chp = to_channelpath(container_of(kobj, struct device, kobj));
173 css = to_css(chp->dev.parent);
175 size = sizeof(struct cmg_entry);
177 /* Only allow single reads. */
178 if (off || count < size)
180 chp_measurement_copy_block((struct cmg_entry *)buf, css, chp->chpid);
185 static struct bin_attribute chp_measurement_attr = {
187 .name = "measurement",
189 .owner = THIS_MODULE,
191 .size = sizeof(struct cmg_entry),
192 .read = chp_measurement_read,
195 void chp_remove_cmg_attr(struct channel_path *chp)
197 device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
198 device_remove_bin_file(&chp->dev, &chp_measurement_attr);
201 int chp_add_cmg_attr(struct channel_path *chp)
205 ret = device_create_bin_file(&chp->dev, &chp_measurement_chars_attr);
208 ret = device_create_bin_file(&chp->dev, &chp_measurement_attr);
210 device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
215 * Files for the channel path entries.
217 static ssize_t chp_status_show(struct device *dev,
218 struct device_attribute *attr, char *buf)
220 struct channel_path *chp = container_of(dev, struct channel_path, dev);
224 return (chp_get_status(chp->chpid) ? sprintf(buf, "online\n") :
225 sprintf(buf, "offline\n"));
228 static ssize_t chp_status_write(struct device *dev,
229 struct device_attribute *attr,
230 const char *buf, size_t count)
232 struct channel_path *cp = container_of(dev, struct channel_path, dev);
237 num_args = sscanf(buf, "%5s", cmd);
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);
248 return error < 0 ? error : count;
252 static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
254 static ssize_t chp_type_show(struct device *dev, struct device_attribute *attr,
257 struct channel_path *chp = container_of(dev, struct channel_path, dev);
261 return sprintf(buf, "%x\n", chp->desc.desc);
264 static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
266 static ssize_t chp_cmg_show(struct device *dev, struct device_attribute *attr,
269 struct channel_path *chp = to_channelpath(dev);
273 if (chp->cmg == -1) /* channel measurements not available */
274 return sprintf(buf, "unknown\n");
275 return sprintf(buf, "%x\n", chp->cmg);
278 static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
280 static ssize_t chp_shared_show(struct device *dev,
281 struct device_attribute *attr, char *buf)
283 struct channel_path *chp = to_channelpath(dev);
287 if (chp->shared == -1) /* channel measurements not available */
288 return sprintf(buf, "unknown\n");
289 return sprintf(buf, "%x\n", chp->shared);
292 static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
294 static struct attribute * chp_attrs[] = {
295 &dev_attr_status.attr,
298 &dev_attr_shared.attr,
302 static struct attribute_group chp_attr_group = {
306 static void chp_release(struct device *dev)
308 struct channel_path *cp;
310 cp = container_of(dev, struct channel_path, dev);
315 * chp_new - register a new channel-path
316 * @chpid - channel-path ID
318 * Create and register data structure representing new channel-path. Return
319 * zero on success, non-zero otherwise.
321 int chp_new(struct chp_id chpid)
323 struct channel_path *chp;
326 chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
330 /* fill in status, etc. */
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,
338 /* Obtain channel path description and fill it in. */
339 ret = chsc_determine_channel_path_description(chpid, &chp->desc);
342 if ((chp->desc.flags & 0x80) == 0) {
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);
356 printk(KERN_WARNING "cio: Channel measurements not "
357 "available, continuing.\n");
363 /* make it known to the system */
364 ret = device_register(&chp->dev);
366 printk(KERN_WARNING "%s: could not register %x.%02x\n",
367 __func__, chpid.cssid, chpid.id);
370 ret = sysfs_create_group(&chp->dev.kobj, &chp_attr_group);
372 device_unregister(&chp->dev);
375 mutex_lock(&css[chpid.cssid]->mutex);
376 if (css[chpid.cssid]->cm_enabled) {
377 ret = chp_add_cmg_attr(chp);
379 sysfs_remove_group(&chp->dev.kobj, &chp_attr_group);
380 device_unregister(&chp->dev);
381 mutex_unlock(&css[chpid.cssid]->mutex);
385 css[chpid.cssid]->chps[chpid.id] = chp;
386 mutex_unlock(&css[chpid.cssid]->mutex);
394 * chp_get_chp_desc - return newly allocated channel-path description
395 * @chpid: channel-path ID
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.
400 void *chp_get_chp_desc(struct chp_id chpid)
402 struct channel_path *chp;
403 struct channel_path_desc *desc;
405 chp = chpid_to_chp(chpid);
408 desc = kmalloc(sizeof(struct channel_path_desc), GFP_KERNEL);
411 memcpy(desc, &chp->desc, sizeof(struct channel_path_desc));
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
420 * Handle channel-report-words indicating that the status of a channel-path
423 int chp_process_crw(int id, int status)
430 if (!chp_is_registered(chpid))
432 return chsc_chp_online(chpid);
434 chsc_chp_offline(chpid);