blob: 6e04521accd8488ee565e3c7be4b25e25417c921 [file] [log] [blame]
Peter Oberparleitere6b6e102007-04-27 16:01:28 +02001/*
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. */
25static 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. */
31static 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. */
38int 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 */
50u8 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 */
74int 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 */
83static 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 */
113static 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
133static 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
143static 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
165static 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
185static 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
195void 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
201int 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 */
217static 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
228static 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
252static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
253
254static 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
264static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
265
266static 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
278static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
279
280static 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
292static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
293
294static 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
302static struct attribute_group chp_attr_group = {
303 .attrs = chp_attrs,
304};
305
306static 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 */
321int 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 /* Get channel-measurement characteristics. */
343 if (css_characteristics_avail && css_chsc_characteristics.scmc
344 && css_chsc_characteristics.secm) {
345 ret = chsc_get_channel_measurement_chars(chp);
346 if (ret)
347 goto out_free;
348 } else {
349 static int msg_done;
350
351 if (!msg_done) {
352 printk(KERN_WARNING "cio: Channel measurements not "
353 "available, continuing.\n");
354 msg_done = 1;
355 }
356 chp->cmg = -1;
357 }
358
359 /* make it known to the system */
360 ret = device_register(&chp->dev);
361 if (ret) {
362 printk(KERN_WARNING "%s: could not register %x.%02x\n",
363 __func__, chpid.cssid, chpid.id);
364 goto out_free;
365 }
366 ret = sysfs_create_group(&chp->dev.kobj, &chp_attr_group);
367 if (ret) {
368 device_unregister(&chp->dev);
369 goto out_free;
370 }
371 mutex_lock(&css[chpid.cssid]->mutex);
372 if (css[chpid.cssid]->cm_enabled) {
373 ret = chp_add_cmg_attr(chp);
374 if (ret) {
375 sysfs_remove_group(&chp->dev.kobj, &chp_attr_group);
376 device_unregister(&chp->dev);
377 mutex_unlock(&css[chpid.cssid]->mutex);
378 goto out_free;
379 }
380 }
381 css[chpid.cssid]->chps[chpid.id] = chp;
382 mutex_unlock(&css[chpid.cssid]->mutex);
383 return ret;
384out_free:
385 kfree(chp);
386 return ret;
387}
388
389/**
390 * chp_get_chp_desc - return newly allocated channel-path description
391 * @chpid: channel-path ID
392 *
393 * On success return a newly allocated copy of the channel-path description
394 * data associated with the given channel-path ID. Return %NULL on error.
395 */
396void *chp_get_chp_desc(struct chp_id chpid)
397{
398 struct channel_path *chp;
399 struct channel_path_desc *desc;
400
401 chp = chpid_to_chp(chpid);
402 if (!chp)
403 return NULL;
404 desc = kmalloc(sizeof(struct channel_path_desc), GFP_KERNEL);
405 if (!desc)
406 return NULL;
407 memcpy(desc, &chp->desc, sizeof(struct channel_path_desc));
408 return desc;
409}
410
411/**
412 * chp_process_crw - process channel-path status change
413 * @id: channel-path ID number
414 * @status: non-zero if channel-path has become available, zero otherwise
415 *
416 * Handle channel-report-words indicating that the status of a channel-path
417 * has changed.
418 */
419int chp_process_crw(int id, int status)
420{
421 struct chp_id chpid;
422
423 chp_id_init(&chpid);
424 chpid.id = id;
425 if (status) {
426 if (!chp_is_registered(chpid))
427 chp_new(chpid);
428 return chsc_chp_online(chpid);
429 } else {
430 chsc_chp_offline(chpid);
431 return 0;
432 }
433}