blob: 1d16189f2f2dbd60b17f29e5400a11cf8da5477e [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>
Peter Oberparleitere5854a52007-04-27 16:01:31 +020013#include <linux/init.h>
14#include <linux/jiffies.h>
15#include <linux/wait.h>
16#include <linux/mutex.h>
Cornelia Hucka0ea22c32007-10-12 16:11:19 +020017#include <linux/errno.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090018#include <linux/slab.h>
Peter Oberparleitere5854a52007-04-27 16:01:31 +020019#include <asm/chpid.h>
20#include <asm/sclp.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010021#include <asm/crw.h>
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020022
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020023#include "cio.h"
24#include "css.h"
25#include "ioasm.h"
26#include "cio_debug.h"
27#include "chp.h"
28
29#define to_channelpath(device) container_of(device, struct channel_path, dev)
Peter Oberparleitere5854a52007-04-27 16:01:31 +020030#define CHP_INFO_UPDATE_INTERVAL 1*HZ
31
32enum cfg_task_t {
33 cfg_none,
34 cfg_configure,
35 cfg_deconfigure
36};
37
38/* Map for pending configure tasks. */
39static enum cfg_task_t chp_cfg_task[__MAX_CSSID + 1][__MAX_CHPID + 1];
40static DEFINE_MUTEX(cfg_lock);
41static int cfg_busy;
42
43/* Map for channel-path status. */
44static struct sclp_chp_info chp_info;
45static DEFINE_MUTEX(info_lock);
46
47/* Time after which channel-path status may be outdated. */
48static unsigned long chp_info_expires;
49
50/* Workqueue to perform pending configure tasks. */
51static struct workqueue_struct *chp_wq;
52static struct work_struct cfg_work;
53
54/* Wait queue for configure completion events. */
55static wait_queue_head_t cfg_wait_queue;
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020056
57/* Return channel_path struct for given chpid. */
58static inline struct channel_path *chpid_to_chp(struct chp_id chpid)
59{
Cornelia Huck7c9f4e32007-10-12 16:11:13 +020060 return channel_subsystems[chpid.cssid]->chps[chpid.id];
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020061}
62
63/* Set vary state for given chpid. */
64static void set_chp_logically_online(struct chp_id chpid, int onoff)
65{
66 chpid_to_chp(chpid)->state = onoff;
67}
68
André Goddard Rosaaf901ca2009-11-14 13:09:05 -020069/* On success return 0 if channel-path is varied offline, 1 if it is varied
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020070 * online. Return -ENODEV if channel-path is not registered. */
71int chp_get_status(struct chp_id chpid)
72{
73 return (chpid_to_chp(chpid) ? chpid_to_chp(chpid)->state : -ENODEV);
74}
75
76/**
77 * chp_get_sch_opm - return opm for subchannel
78 * @sch: subchannel
79 *
80 * Calculate and return the operational path mask (opm) based on the chpids
81 * used by the subchannel and the status of the associated channel-paths.
82 */
83u8 chp_get_sch_opm(struct subchannel *sch)
84{
85 struct chp_id chpid;
86 int opm;
87 int i;
88
89 opm = 0;
90 chp_id_init(&chpid);
Cornelia Hucka0ea22c32007-10-12 16:11:19 +020091 for (i = 0; i < 8; i++) {
Peter Oberparleitere6b6e102007-04-27 16:01:28 +020092 opm <<= 1;
93 chpid.id = sch->schib.pmcw.chpid[i];
94 if (chp_get_status(chpid) != 0)
95 opm |= 1;
96 }
97 return opm;
98}
Cornelia Huck44a1c192008-07-14 09:58:47 +020099EXPORT_SYMBOL_GPL(chp_get_sch_opm);
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200100
101/**
102 * chp_is_registered - check if a channel-path is registered
103 * @chpid: channel-path ID
104 *
105 * Return non-zero if a channel-path with the given chpid is registered,
106 * zero otherwise.
107 */
108int chp_is_registered(struct chp_id chpid)
109{
110 return chpid_to_chp(chpid) != NULL;
111}
112
113/*
114 * Function: s390_vary_chpid
115 * Varies the specified chpid online or offline
116 */
117static int s390_vary_chpid(struct chp_id chpid, int on)
118{
119 char dbf_text[15];
120 int status;
121
122 sprintf(dbf_text, on?"varyon%x.%02x":"varyoff%x.%02x", chpid.cssid,
123 chpid.id);
Cornelia Hucka0ea22c32007-10-12 16:11:19 +0200124 CIO_TRACE_EVENT(2, dbf_text);
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200125
126 status = chp_get_status(chpid);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200127 if (!on && !status)
128 return 0;
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200129
130 set_chp_logically_online(chpid, on);
131 chsc_chp_vary(chpid, on);
132 return 0;
133}
134
135/*
136 * Channel measurement related functions
137 */
Zhang Rui91a69022007-06-09 13:57:22 +0800138static ssize_t chp_measurement_chars_read(struct kobject *kobj,
139 struct bin_attribute *bin_attr,
140 char *buf, loff_t off, size_t count)
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200141{
142 struct channel_path *chp;
Heiko Carstens364c8552007-10-12 16:11:35 +0200143 struct device *device;
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200144
Heiko Carstens364c8552007-10-12 16:11:35 +0200145 device = container_of(kobj, struct device, kobj);
146 chp = to_channelpath(device);
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200147 if (!chp->cmg_chars)
148 return 0;
149
Akinobu Mitad9cef212008-07-14 09:59:15 +0200150 return memory_read_from_buffer(buf, count, &off,
151 chp->cmg_chars, sizeof(struct cmg_chars));
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200152}
153
154static struct bin_attribute chp_measurement_chars_attr = {
155 .attr = {
156 .name = "measurement_chars",
157 .mode = S_IRUSR,
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200158 },
159 .size = sizeof(struct cmg_chars),
160 .read = chp_measurement_chars_read,
161};
162
163static void chp_measurement_copy_block(struct cmg_entry *buf,
164 struct channel_subsystem *css,
165 struct chp_id chpid)
166{
167 void *area;
168 struct cmg_entry *entry, reference_buf;
169 int idx;
170
171 if (chpid.id < 128) {
172 area = css->cub_addr1;
173 idx = chpid.id;
174 } else {
175 area = css->cub_addr2;
176 idx = chpid.id - 128;
177 }
178 entry = area + (idx * sizeof(struct cmg_entry));
179 do {
180 memcpy(buf, entry, sizeof(*entry));
181 memcpy(&reference_buf, entry, sizeof(*entry));
182 } while (reference_buf.values[0] != buf->values[0]);
183}
184
Zhang Rui91a69022007-06-09 13:57:22 +0800185static ssize_t chp_measurement_read(struct kobject *kobj,
186 struct bin_attribute *bin_attr,
187 char *buf, loff_t off, size_t count)
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200188{
189 struct channel_path *chp;
190 struct channel_subsystem *css;
Heiko Carstens364c8552007-10-12 16:11:35 +0200191 struct device *device;
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200192 unsigned int size;
193
Heiko Carstens364c8552007-10-12 16:11:35 +0200194 device = container_of(kobj, struct device, kobj);
195 chp = to_channelpath(device);
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200196 css = to_css(chp->dev.parent);
197
198 size = sizeof(struct cmg_entry);
199
200 /* Only allow single reads. */
201 if (off || count < size)
202 return 0;
203 chp_measurement_copy_block((struct cmg_entry *)buf, css, chp->chpid);
204 count = size;
205 return count;
206}
207
208static struct bin_attribute chp_measurement_attr = {
209 .attr = {
210 .name = "measurement",
211 .mode = S_IRUSR,
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200212 },
213 .size = sizeof(struct cmg_entry),
214 .read = chp_measurement_read,
215};
216
217void chp_remove_cmg_attr(struct channel_path *chp)
218{
219 device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
220 device_remove_bin_file(&chp->dev, &chp_measurement_attr);
221}
222
223int chp_add_cmg_attr(struct channel_path *chp)
224{
225 int ret;
226
227 ret = device_create_bin_file(&chp->dev, &chp_measurement_chars_attr);
228 if (ret)
229 return ret;
230 ret = device_create_bin_file(&chp->dev, &chp_measurement_attr);
231 if (ret)
232 device_remove_bin_file(&chp->dev, &chp_measurement_chars_attr);
233 return ret;
234}
235
236/*
237 * Files for the channel path entries.
238 */
239static ssize_t chp_status_show(struct device *dev,
240 struct device_attribute *attr, char *buf)
241{
Cornelia Huck05469602007-10-22 12:52:40 +0200242 struct channel_path *chp = to_channelpath(dev);
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200243
244 if (!chp)
245 return 0;
246 return (chp_get_status(chp->chpid) ? sprintf(buf, "online\n") :
247 sprintf(buf, "offline\n"));
248}
249
250static ssize_t chp_status_write(struct device *dev,
251 struct device_attribute *attr,
252 const char *buf, size_t count)
253{
Cornelia Huck05469602007-10-22 12:52:40 +0200254 struct channel_path *cp = to_channelpath(dev);
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200255 char cmd[10];
256 int num_args;
257 int error;
258
259 num_args = sscanf(buf, "%5s", cmd);
260 if (!num_args)
261 return count;
262
263 if (!strnicmp(cmd, "on", 2) || !strcmp(cmd, "1"))
264 error = s390_vary_chpid(cp->chpid, 1);
265 else if (!strnicmp(cmd, "off", 3) || !strcmp(cmd, "0"))
266 error = s390_vary_chpid(cp->chpid, 0);
267 else
268 error = -EINVAL;
269
270 return error < 0 ? error : count;
271
272}
273
274static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
275
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200276static ssize_t chp_configure_show(struct device *dev,
277 struct device_attribute *attr, char *buf)
278{
279 struct channel_path *cp;
280 int status;
281
Cornelia Huck05469602007-10-22 12:52:40 +0200282 cp = to_channelpath(dev);
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200283 status = chp_info_get_status(cp->chpid);
284 if (status < 0)
285 return status;
286
287 return snprintf(buf, PAGE_SIZE, "%d\n", status);
288}
289
290static int cfg_wait_idle(void);
291
292static ssize_t chp_configure_write(struct device *dev,
293 struct device_attribute *attr,
294 const char *buf, size_t count)
295{
296 struct channel_path *cp;
297 int val;
298 char delim;
299
300 if (sscanf(buf, "%d %c", &val, &delim) != 1)
301 return -EINVAL;
302 if (val != 0 && val != 1)
303 return -EINVAL;
Cornelia Huck05469602007-10-22 12:52:40 +0200304 cp = to_channelpath(dev);
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200305 chp_cfg_schedule(cp->chpid, val);
306 cfg_wait_idle();
307
308 return count;
309}
310
311static DEVICE_ATTR(configure, 0644, chp_configure_show, chp_configure_write);
312
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200313static ssize_t chp_type_show(struct device *dev, struct device_attribute *attr,
314 char *buf)
315{
Cornelia Huck05469602007-10-22 12:52:40 +0200316 struct channel_path *chp = to_channelpath(dev);
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200317
318 if (!chp)
319 return 0;
320 return sprintf(buf, "%x\n", chp->desc.desc);
321}
322
323static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
324
325static ssize_t chp_cmg_show(struct device *dev, struct device_attribute *attr,
326 char *buf)
327{
328 struct channel_path *chp = to_channelpath(dev);
329
330 if (!chp)
331 return 0;
332 if (chp->cmg == -1) /* channel measurements not available */
333 return sprintf(buf, "unknown\n");
334 return sprintf(buf, "%x\n", chp->cmg);
335}
336
337static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
338
339static ssize_t chp_shared_show(struct device *dev,
340 struct device_attribute *attr, char *buf)
341{
342 struct channel_path *chp = to_channelpath(dev);
343
344 if (!chp)
345 return 0;
346 if (chp->shared == -1) /* channel measurements not available */
347 return sprintf(buf, "unknown\n");
348 return sprintf(buf, "%x\n", chp->shared);
349}
350
351static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
352
Cornelia Hucka0ea22c32007-10-12 16:11:19 +0200353static struct attribute *chp_attrs[] = {
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200354 &dev_attr_status.attr,
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200355 &dev_attr_configure.attr,
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200356 &dev_attr_type.attr,
357 &dev_attr_cmg.attr,
358 &dev_attr_shared.attr,
359 NULL,
360};
361
362static struct attribute_group chp_attr_group = {
363 .attrs = chp_attrs,
364};
365
366static void chp_release(struct device *dev)
367{
368 struct channel_path *cp;
369
Cornelia Huck05469602007-10-22 12:52:40 +0200370 cp = to_channelpath(dev);
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200371 kfree(cp);
372}
373
374/**
375 * chp_new - register a new channel-path
376 * @chpid - channel-path ID
377 *
378 * Create and register data structure representing new channel-path. Return
379 * zero on success, non-zero otherwise.
380 */
381int chp_new(struct chp_id chpid)
382{
383 struct channel_path *chp;
384 int ret;
385
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200386 if (chp_is_registered(chpid))
387 return 0;
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200388 chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
389 if (!chp)
390 return -ENOMEM;
391
392 /* fill in status, etc. */
393 chp->chpid = chpid;
394 chp->state = 1;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200395 chp->dev.parent = &channel_subsystems[chpid.cssid]->device;
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200396 chp->dev.release = chp_release;
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200397
398 /* Obtain channel path description and fill it in. */
Cornelia Huck9d92a7e2008-07-14 09:59:05 +0200399 ret = chsc_determine_base_channel_path_desc(chpid, &chp->desc);
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200400 if (ret)
401 goto out_free;
Peter Oberparleiterc9182e02007-04-27 16:01:29 +0200402 if ((chp->desc.flags & 0x80) == 0) {
403 ret = -ENODEV;
404 goto out_free;
405 }
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200406 /* Get channel-measurement characteristics. */
Cornelia Huck75784c02008-07-14 09:58:57 +0200407 if (css_chsc_characteristics.scmc && css_chsc_characteristics.secm) {
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200408 ret = chsc_get_channel_measurement_chars(chp);
409 if (ret)
410 goto out_free;
411 } else {
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200412 chp->cmg = -1;
413 }
Michael Ernstec004402009-10-06 10:33:59 +0200414 dev_set_name(&chp->dev, "chp%x.%02x", chpid.cssid, chpid.id);
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200415
416 /* make it known to the system */
417 ret = device_register(&chp->dev);
418 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200419 CIO_MSG_EVENT(0, "Could not register chp%x.%02x: %d\n",
420 chpid.cssid, chpid.id, ret);
Sebastian Ottc6304932009-09-11 10:28:38 +0200421 put_device(&chp->dev);
422 goto out;
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200423 }
424 ret = sysfs_create_group(&chp->dev.kobj, &chp_attr_group);
425 if (ret) {
426 device_unregister(&chp->dev);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200427 goto out;
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200428 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200429 mutex_lock(&channel_subsystems[chpid.cssid]->mutex);
430 if (channel_subsystems[chpid.cssid]->cm_enabled) {
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200431 ret = chp_add_cmg_attr(chp);
432 if (ret) {
433 sysfs_remove_group(&chp->dev.kobj, &chp_attr_group);
434 device_unregister(&chp->dev);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200435 mutex_unlock(&channel_subsystems[chpid.cssid]->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200436 goto out;
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200437 }
438 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200439 channel_subsystems[chpid.cssid]->chps[chpid.id] = chp;
440 mutex_unlock(&channel_subsystems[chpid.cssid]->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200441 goto out;
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200442out_free:
443 kfree(chp);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200444out:
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200445 return ret;
446}
447
448/**
449 * chp_get_chp_desc - return newly allocated channel-path description
450 * @chpid: channel-path ID
451 *
452 * On success return a newly allocated copy of the channel-path description
453 * data associated with the given channel-path ID. Return %NULL on error.
454 */
455void *chp_get_chp_desc(struct chp_id chpid)
456{
457 struct channel_path *chp;
458 struct channel_path_desc *desc;
459
460 chp = chpid_to_chp(chpid);
461 if (!chp)
462 return NULL;
463 desc = kmalloc(sizeof(struct channel_path_desc), GFP_KERNEL);
464 if (!desc)
465 return NULL;
466 memcpy(desc, &chp->desc, sizeof(struct channel_path_desc));
467 return desc;
468}
469
470/**
471 * chp_process_crw - process channel-path status change
Cornelia Huckc1156182008-07-14 09:58:46 +0200472 * @crw0: channel report-word to handler
473 * @crw1: second channel-report word (always NULL)
474 * @overflow: crw overflow indication
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200475 *
476 * Handle channel-report-words indicating that the status of a channel-path
477 * has changed.
478 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200479static void chp_process_crw(struct crw *crw0, struct crw *crw1,
480 int overflow)
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200481{
482 struct chp_id chpid;
483
Cornelia Huckc1156182008-07-14 09:58:46 +0200484 if (overflow) {
485 css_schedule_eval_all();
486 return;
487 }
488 CIO_CRW_EVENT(2, "CRW reports slct=%d, oflw=%d, "
489 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
490 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
491 crw0->erc, crw0->rsid);
492 /*
493 * Check for solicited machine checks. These are
494 * created by reset channel path and need not be
495 * handled here.
496 */
497 if (crw0->slct) {
498 CIO_CRW_EVENT(2, "solicited machine check for "
499 "channel path %02X\n", crw0->rsid);
500 return;
501 }
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200502 chp_id_init(&chpid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200503 chpid.id = crw0->rsid;
504 switch (crw0->erc) {
505 case CRW_ERC_IPARM: /* Path has come. */
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200506 if (!chp_is_registered(chpid))
507 chp_new(chpid);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200508 chsc_chp_online(chpid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200509 break;
510 case CRW_ERC_PERRI: /* Path has gone. */
511 case CRW_ERC_PERRN:
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200512 chsc_chp_offline(chpid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200513 break;
514 default:
515 CIO_CRW_EVENT(2, "Don't know how to handle erc=%x\n",
516 crw0->erc);
517 }
Peter Oberparleitere6b6e102007-04-27 16:01:28 +0200518}
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200519
Cornelia Huck99611f82008-07-14 09:59:02 +0200520int chp_ssd_get_mask(struct chsc_ssd_info *ssd, struct chp_link *link)
Cornelia Huckc820de32008-07-14 09:58:45 +0200521{
522 int i;
523 int mask;
524
525 for (i = 0; i < 8; i++) {
526 mask = 0x80 >> i;
527 if (!(ssd->path_mask & mask))
528 continue;
Cornelia Huck99611f82008-07-14 09:59:02 +0200529 if (!chp_id_is_equal(&ssd->chpid[i], &link->chpid))
Cornelia Huckc820de32008-07-14 09:58:45 +0200530 continue;
531 if ((ssd->fla_valid_mask & mask) &&
Cornelia Huck99611f82008-07-14 09:59:02 +0200532 ((ssd->fla[i] & link->fla_mask) != link->fla))
Cornelia Huckc820de32008-07-14 09:58:45 +0200533 continue;
534 return mask;
535 }
536 return 0;
537}
538EXPORT_SYMBOL_GPL(chp_ssd_get_mask);
539
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200540static inline int info_bit_num(struct chp_id id)
541{
542 return id.id + id.cssid * (__MAX_CHPID + 1);
543}
544
545/* Force chp_info refresh on next call to info_validate(). */
546static void info_expire(void)
547{
548 mutex_lock(&info_lock);
549 chp_info_expires = jiffies - 1;
550 mutex_unlock(&info_lock);
551}
552
553/* Ensure that chp_info is up-to-date. */
554static int info_update(void)
555{
556 int rc;
557
558 mutex_lock(&info_lock);
559 rc = 0;
560 if (time_after(jiffies, chp_info_expires)) {
561 /* Data is too old, update. */
562 rc = sclp_chp_read_info(&chp_info);
563 chp_info_expires = jiffies + CHP_INFO_UPDATE_INTERVAL ;
564 }
565 mutex_unlock(&info_lock);
566
567 return rc;
568}
569
570/**
571 * chp_info_get_status - retrieve configure status of a channel-path
572 * @chpid: channel-path ID
573 *
574 * On success, return 0 for standby, 1 for configured, 2 for reserved,
575 * 3 for not recognized. Return negative error code on error.
576 */
577int chp_info_get_status(struct chp_id chpid)
578{
579 int rc;
580 int bit;
581
582 rc = info_update();
583 if (rc)
584 return rc;
585
586 bit = info_bit_num(chpid);
587 mutex_lock(&info_lock);
588 if (!chp_test_bit(chp_info.recognized, bit))
589 rc = CHP_STATUS_NOT_RECOGNIZED;
590 else if (chp_test_bit(chp_info.configured, bit))
591 rc = CHP_STATUS_CONFIGURED;
592 else if (chp_test_bit(chp_info.standby, bit))
593 rc = CHP_STATUS_STANDBY;
594 else
595 rc = CHP_STATUS_RESERVED;
596 mutex_unlock(&info_lock);
597
598 return rc;
599}
600
601/* Return configure task for chpid. */
602static enum cfg_task_t cfg_get_task(struct chp_id chpid)
603{
604 return chp_cfg_task[chpid.cssid][chpid.id];
605}
606
607/* Set configure task for chpid. */
608static void cfg_set_task(struct chp_id chpid, enum cfg_task_t cfg)
609{
610 chp_cfg_task[chpid.cssid][chpid.id] = cfg;
611}
612
613/* Perform one configure/deconfigure request. Reschedule work function until
614 * last request. */
615static void cfg_func(struct work_struct *work)
616{
617 struct chp_id chpid;
618 enum cfg_task_t t;
Peter Oberparleiter683c5412008-07-14 09:59:04 +0200619 int rc;
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200620
621 mutex_lock(&cfg_lock);
622 t = cfg_none;
623 chp_id_for_each(&chpid) {
624 t = cfg_get_task(chpid);
625 if (t != cfg_none) {
626 cfg_set_task(chpid, cfg_none);
627 break;
628 }
629 }
630 mutex_unlock(&cfg_lock);
631
632 switch (t) {
633 case cfg_configure:
Peter Oberparleiter683c5412008-07-14 09:59:04 +0200634 rc = sclp_chp_configure(chpid);
635 if (rc)
636 CIO_MSG_EVENT(2, "chp: sclp_chp_configure(%x.%02x)="
637 "%d\n", chpid.cssid, chpid.id, rc);
638 else {
639 info_expire();
640 chsc_chp_online(chpid);
641 }
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200642 break;
643 case cfg_deconfigure:
Peter Oberparleiter683c5412008-07-14 09:59:04 +0200644 rc = sclp_chp_deconfigure(chpid);
645 if (rc)
646 CIO_MSG_EVENT(2, "chp: sclp_chp_deconfigure(%x.%02x)="
647 "%d\n", chpid.cssid, chpid.id, rc);
648 else {
649 info_expire();
650 chsc_chp_offline(chpid);
651 }
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200652 break;
653 case cfg_none:
654 /* Get updated information after last change. */
655 info_update();
656 mutex_lock(&cfg_lock);
657 cfg_busy = 0;
658 mutex_unlock(&cfg_lock);
659 wake_up_interruptible(&cfg_wait_queue);
660 return;
661 }
662 queue_work(chp_wq, &cfg_work);
663}
664
665/**
666 * chp_cfg_schedule - schedule chpid configuration request
667 * @chpid - channel-path ID
668 * @configure - Non-zero for configure, zero for deconfigure
669 *
670 * Schedule a channel-path configuration/deconfiguration request.
671 */
672void chp_cfg_schedule(struct chp_id chpid, int configure)
673{
674 CIO_MSG_EVENT(2, "chp_cfg_sched%x.%02x=%d\n", chpid.cssid, chpid.id,
675 configure);
676 mutex_lock(&cfg_lock);
677 cfg_set_task(chpid, configure ? cfg_configure : cfg_deconfigure);
678 cfg_busy = 1;
679 mutex_unlock(&cfg_lock);
680 queue_work(chp_wq, &cfg_work);
681}
682
683/**
684 * chp_cfg_cancel_deconfigure - cancel chpid deconfiguration request
685 * @chpid - channel-path ID
686 *
687 * Cancel an active channel-path deconfiguration request if it has not yet
688 * been performed.
689 */
690void chp_cfg_cancel_deconfigure(struct chp_id chpid)
691{
692 CIO_MSG_EVENT(2, "chp_cfg_cancel:%x.%02x\n", chpid.cssid, chpid.id);
693 mutex_lock(&cfg_lock);
694 if (cfg_get_task(chpid) == cfg_deconfigure)
695 cfg_set_task(chpid, cfg_none);
696 mutex_unlock(&cfg_lock);
697}
698
699static int cfg_wait_idle(void)
700{
701 if (wait_event_interruptible(cfg_wait_queue, !cfg_busy))
702 return -ERESTARTSYS;
703 return 0;
704}
705
706static int __init chp_init(void)
707{
708 struct chp_id chpid;
Cornelia Huckc1156182008-07-14 09:58:46 +0200709 int ret;
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200710
Heiko Carstensf5daba12009-03-26 15:24:01 +0100711 ret = crw_register_handler(CRW_RSC_CPATH, chp_process_crw);
Cornelia Huckc1156182008-07-14 09:58:46 +0200712 if (ret)
713 return ret;
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200714 chp_wq = create_singlethread_workqueue("cio_chp");
Cornelia Huckc1156182008-07-14 09:58:46 +0200715 if (!chp_wq) {
Heiko Carstensf5daba12009-03-26 15:24:01 +0100716 crw_unregister_handler(CRW_RSC_CPATH);
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200717 return -ENOMEM;
Cornelia Huckc1156182008-07-14 09:58:46 +0200718 }
Peter Oberparleitere5854a52007-04-27 16:01:31 +0200719 INIT_WORK(&cfg_work, cfg_func);
720 init_waitqueue_head(&cfg_wait_queue);
721 if (info_update())
722 return 0;
723 /* Register available channel-paths. */
724 chp_id_for_each(&chpid) {
725 if (chp_info_get_status(chpid) != CHP_STATUS_NOT_RECOGNIZED)
726 chp_new(chpid);
727 }
728
729 return 0;
730}
731
732subsys_initcall(chp_init);