Merge tag 's390-6.10-1' of git://git.kernel.org/pub/scm/linux/kernel/git/s390/linux
[linux-2.6-block.git] / drivers / s390 / cio / chp.c
CommitLineData
724117b7 1// SPDX-License-Identifier: GPL-2.0
e6b6e10a 2/*
a53c8fab 3 * Copyright IBM Corp. 1999, 2010
e6b6e10a
PO
4 * Author(s): Cornelia Huck (cornelia.huck@de.ibm.com)
5 * Arnd Bergmann (arndb@de.ibm.com)
6 * Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
7 */
8
9#include <linux/bug.h>
10#include <linux/workqueue.h>
11#include <linux/spinlock.h>
3a4c5d59
HC
12#include <linux/export.h>
13#include <linux/sched.h>
e5854a58
PO
14#include <linux/init.h>
15#include <linux/jiffies.h>
16#include <linux/wait.h>
17#include <linux/mutex.h>
a0ea22c3 18#include <linux/errno.h>
5a0e3ad6 19#include <linux/slab.h>
e5854a58
PO
20#include <asm/chpid.h>
21#include <asm/sclp.h>
f5daba1d 22#include <asm/crw.h>
e6b6e10a 23
e6b6e10a
PO
24#include "cio.h"
25#include "css.h"
26#include "ioasm.h"
27#include "cio_debug.h"
28#include "chp.h"
29
30#define to_channelpath(device) container_of(device, struct channel_path, dev)
e5854a58
PO
31#define CHP_INFO_UPDATE_INTERVAL 1*HZ
32
33enum cfg_task_t {
34 cfg_none,
35 cfg_configure,
36 cfg_deconfigure
37};
38
39/* Map for pending configure tasks. */
40static enum cfg_task_t chp_cfg_task[__MAX_CSSID + 1][__MAX_CHPID + 1];
837c5220 41static DEFINE_SPINLOCK(cfg_lock);
e5854a58
PO
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
e5854a58
PO
50static struct work_struct cfg_work;
51
52/* Wait queue for configure completion events. */
3081e616 53static DECLARE_WAIT_QUEUE_HEAD(cfg_wait_queue);
e6b6e10a 54
e6b6e10a
PO
55/* Set vary state for given chpid. */
56static void set_chp_logically_online(struct chp_id chpid, int onoff)
57{
58 chpid_to_chp(chpid)->state = onoff;
59}
60
af901ca1 61/* On success return 0 if channel-path is varied offline, 1 if it is varied
e6b6e10a
PO
62 * online. Return -ENODEV if channel-path is not registered. */
63int chp_get_status(struct chp_id chpid)
64{
65 return (chpid_to_chp(chpid) ? chpid_to_chp(chpid)->state : -ENODEV);
66}
67
68/**
69 * chp_get_sch_opm - return opm for subchannel
70 * @sch: subchannel
71 *
72 * Calculate and return the operational path mask (opm) based on the chpids
73 * used by the subchannel and the status of the associated channel-paths.
74 */
75u8 chp_get_sch_opm(struct subchannel *sch)
76{
77 struct chp_id chpid;
78 int opm;
79 int i;
80
81 opm = 0;
82 chp_id_init(&chpid);
a0ea22c3 83 for (i = 0; i < 8; i++) {
e6b6e10a
PO
84 opm <<= 1;
85 chpid.id = sch->schib.pmcw.chpid[i];
86 if (chp_get_status(chpid) != 0)
87 opm |= 1;
88 }
89 return opm;
90}
44a1c19e 91EXPORT_SYMBOL_GPL(chp_get_sch_opm);
e6b6e10a
PO
92
93/**
94 * chp_is_registered - check if a channel-path is registered
95 * @chpid: channel-path ID
96 *
97 * Return non-zero if a channel-path with the given chpid is registered,
98 * zero otherwise.
99 */
100int chp_is_registered(struct chp_id chpid)
101{
102 return chpid_to_chp(chpid) != NULL;
103}
104
105/*
106 * Function: s390_vary_chpid
107 * Varies the specified chpid online or offline
108 */
109static int s390_vary_chpid(struct chp_id chpid, int on)
110{
111 char dbf_text[15];
112 int status;
113
114 sprintf(dbf_text, on?"varyon%x.%02x":"varyoff%x.%02x", chpid.cssid,
115 chpid.id);
a0ea22c3 116 CIO_TRACE_EVENT(2, dbf_text);
e6b6e10a
PO
117
118 status = chp_get_status(chpid);
c78aa6cb
ME
119 if (!on && !status)
120 return 0;
e6b6e10a
PO
121
122 set_chp_logically_online(chpid, on);
123 chsc_chp_vary(chpid, on);
124 return 0;
125}
126
127/*
128 * Channel measurement related functions
129 */
b4691baa
PO
130static ssize_t measurement_chars_read(struct file *filp, struct kobject *kobj,
131 struct bin_attribute *bin_attr,
132 char *buf, loff_t off, size_t count)
e6b6e10a
PO
133{
134 struct channel_path *chp;
364c8558 135 struct device *device;
e6b6e10a 136
0d730b57 137 device = kobj_to_dev(kobj);
364c8558 138 chp = to_channelpath(device);
0d9bfe91 139 if (chp->cmg == -1)
e6b6e10a
PO
140 return 0;
141
0d9bfe91
SO
142 return memory_read_from_buffer(buf, count, &off, &chp->cmg_chars,
143 sizeof(chp->cmg_chars));
e6b6e10a 144}
b4691baa 145static BIN_ATTR_ADMIN_RO(measurement_chars, sizeof(struct cmg_chars));
e6b6e10a 146
2dc8903a
PO
147static ssize_t chp_measurement_copy_block(void *buf, loff_t off, size_t count,
148 struct kobject *kobj, bool extended)
e6b6e10a
PO
149{
150 struct channel_path *chp;
151 struct channel_subsystem *css;
364c8558 152 struct device *device;
e6b6e10a 153 unsigned int size;
2dc8903a
PO
154 void *area, *entry;
155 int id, idx;
e6b6e10a 156
0d730b57 157 device = kobj_to_dev(kobj);
364c8558 158 chp = to_channelpath(device);
e6b6e10a 159 css = to_css(chp->dev.parent);
2dc8903a 160 id = chp->chpid.id;
e6b6e10a 161
2dc8903a
PO
162 if (extended) {
163 /* Check if extended measurement data is available. */
164 if (!chp->extended)
165 return 0;
166
167 size = sizeof(struct cmg_ext_entry);
168 area = css->ecub[id / CSS_ECUES_PER_PAGE];
169 idx = id % CSS_ECUES_PER_PAGE;
170 } else {
171 size = sizeof(struct cmg_entry);
172 area = css->cub[id / CSS_CUES_PER_PAGE];
173 idx = id % CSS_CUES_PER_PAGE;
174 }
175 entry = area + (idx * size);
e6b6e10a
PO
176
177 /* Only allow single reads. */
178 if (off || count < size)
179 return 0;
2dc8903a
PO
180
181 memcpy(buf, entry, size);
182
183 return size;
184}
185
186static ssize_t measurement_read(struct file *filp, struct kobject *kobj,
187 struct bin_attribute *bin_attr,
188 char *buf, loff_t off, size_t count)
189{
190 return chp_measurement_copy_block(buf, off, count, kobj, false);
e6b6e10a 191}
b4691baa 192static BIN_ATTR_ADMIN_RO(measurement, sizeof(struct cmg_entry));
e6b6e10a 193
2dc8903a
PO
194static ssize_t ext_measurement_read(struct file *filp, struct kobject *kobj,
195 struct bin_attribute *bin_attr,
196 char *buf, loff_t off, size_t count)
197{
198 return chp_measurement_copy_block(buf, off, count, kobj, true);
199}
200static BIN_ATTR_ADMIN_RO(ext_measurement, sizeof(struct cmg_ext_entry));
201
b4691baa
PO
202static struct bin_attribute *measurement_attrs[] = {
203 &bin_attr_measurement_chars,
204 &bin_attr_measurement,
2dc8903a 205 &bin_attr_ext_measurement,
b4691baa 206 NULL,
e6b6e10a 207};
b4691baa 208BIN_ATTRIBUTE_GROUPS(measurement);
e6b6e10a
PO
209
210void chp_remove_cmg_attr(struct channel_path *chp)
211{
b4691baa 212 device_remove_groups(&chp->dev, measurement_groups);
e6b6e10a
PO
213}
214
215int chp_add_cmg_attr(struct channel_path *chp)
216{
b4691baa 217 return device_add_groups(&chp->dev, measurement_groups);
e6b6e10a
PO
218}
219
220/*
221 * Files for the channel path entries.
222 */
223static ssize_t chp_status_show(struct device *dev,
224 struct device_attribute *attr, char *buf)
225{
05469607 226 struct channel_path *chp = to_channelpath(dev);
b730f3a9 227 int status;
e6b6e10a 228
b730f3a9
SO
229 mutex_lock(&chp->lock);
230 status = chp->state;
231 mutex_unlock(&chp->lock);
232
233 return status ? sprintf(buf, "online\n") : sprintf(buf, "offline\n");
e6b6e10a
PO
234}
235
236static ssize_t chp_status_write(struct device *dev,
237 struct device_attribute *attr,
238 const char *buf, size_t count)
239{
05469607 240 struct channel_path *cp = to_channelpath(dev);
e6b6e10a
PO
241 char cmd[10];
242 int num_args;
243 int error;
244
245 num_args = sscanf(buf, "%5s", cmd);
246 if (!num_args)
247 return count;
248
c749d8c0
VV
249 /* Wait until previous actions have settled. */
250 css_wait_for_slow_path();
251
3f8bfd9a 252 if (!strncasecmp(cmd, "on", 2) || !strcmp(cmd, "1")) {
b730f3a9 253 mutex_lock(&cp->lock);
e6b6e10a 254 error = s390_vary_chpid(cp->chpid, 1);
b730f3a9 255 mutex_unlock(&cp->lock);
3f8bfd9a 256 } else if (!strncasecmp(cmd, "off", 3) || !strcmp(cmd, "0")) {
b730f3a9 257 mutex_lock(&cp->lock);
e6b6e10a 258 error = s390_vary_chpid(cp->chpid, 0);
b730f3a9
SO
259 mutex_unlock(&cp->lock);
260 } else
e6b6e10a
PO
261 error = -EINVAL;
262
263 return error < 0 ? error : count;
e6b6e10a
PO
264}
265
266static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
267
e5854a58
PO
268static ssize_t chp_configure_show(struct device *dev,
269 struct device_attribute *attr, char *buf)
270{
271 struct channel_path *cp;
272 int status;
273
05469607 274 cp = to_channelpath(dev);
e5854a58
PO
275 status = chp_info_get_status(cp->chpid);
276 if (status < 0)
277 return status;
278
4b9e0436 279 return sysfs_emit(buf, "%d\n", status);
e5854a58
PO
280}
281
282static int cfg_wait_idle(void);
283
284static ssize_t chp_configure_write(struct device *dev,
285 struct device_attribute *attr,
286 const char *buf, size_t count)
287{
288 struct channel_path *cp;
289 int val;
290 char delim;
291
292 if (sscanf(buf, "%d %c", &val, &delim) != 1)
293 return -EINVAL;
294 if (val != 0 && val != 1)
295 return -EINVAL;
05469607 296 cp = to_channelpath(dev);
e5854a58
PO
297 chp_cfg_schedule(cp->chpid, val);
298 cfg_wait_idle();
299
300 return count;
301}
302
303static DEVICE_ATTR(configure, 0644, chp_configure_show, chp_configure_write);
304
e6b6e10a
PO
305static ssize_t chp_type_show(struct device *dev, struct device_attribute *attr,
306 char *buf)
307{
05469607 308 struct channel_path *chp = to_channelpath(dev);
b730f3a9 309 u8 type;
e6b6e10a 310
b730f3a9
SO
311 mutex_lock(&chp->lock);
312 type = chp->desc.desc;
313 mutex_unlock(&chp->lock);
314 return sprintf(buf, "%x\n", type);
e6b6e10a
PO
315}
316
317static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
318
319static ssize_t chp_cmg_show(struct device *dev, struct device_attribute *attr,
320 char *buf)
321{
322 struct channel_path *chp = to_channelpath(dev);
323
324 if (!chp)
325 return 0;
326 if (chp->cmg == -1) /* channel measurements not available */
327 return sprintf(buf, "unknown\n");
f48781d2 328 return sprintf(buf, "%d\n", chp->cmg);
e6b6e10a
PO
329}
330
331static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
332
333static ssize_t chp_shared_show(struct device *dev,
334 struct device_attribute *attr, char *buf)
335{
336 struct channel_path *chp = to_channelpath(dev);
337
338 if (!chp)
339 return 0;
340 if (chp->shared == -1) /* channel measurements not available */
341 return sprintf(buf, "unknown\n");
342 return sprintf(buf, "%x\n", chp->shared);
343}
344
345static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
346
ba54229d
PO
347static ssize_t chp_chid_show(struct device *dev, struct device_attribute *attr,
348 char *buf)
349{
350 struct channel_path *chp = to_channelpath(dev);
351 ssize_t rc;
352
353 mutex_lock(&chp->lock);
354 if (chp->desc_fmt1.flags & 0x10)
355 rc = sprintf(buf, "%04x\n", chp->desc_fmt1.chid);
356 else
357 rc = 0;
358 mutex_unlock(&chp->lock);
359
360 return rc;
361}
362static DEVICE_ATTR(chid, 0444, chp_chid_show, NULL);
363
364static ssize_t chp_chid_external_show(struct device *dev,
365 struct device_attribute *attr, char *buf)
366{
367 struct channel_path *chp = to_channelpath(dev);
368 ssize_t rc;
369
370 mutex_lock(&chp->lock);
371 if (chp->desc_fmt1.flags & 0x10)
372 rc = sprintf(buf, "%x\n", chp->desc_fmt1.flags & 0x8 ? 1 : 0);
373 else
374 rc = 0;
375 mutex_unlock(&chp->lock);
376
377 return rc;
378}
379static DEVICE_ATTR(chid_external, 0444, chp_chid_external_show, NULL);
380
8b8b0915
SO
381static ssize_t chp_esc_show(struct device *dev,
382 struct device_attribute *attr, char *buf)
383{
384 struct channel_path *chp = to_channelpath(dev);
385 ssize_t rc;
386
387 mutex_lock(&chp->lock);
388 rc = sprintf(buf, "%x\n", chp->desc_fmt1.esc);
389 mutex_unlock(&chp->lock);
390
391 return rc;
392}
393static DEVICE_ATTR(esc, 0444, chp_esc_show, NULL);
394
0f987e6c
PO
395static char apply_max_suffix(unsigned long *value, unsigned long base)
396{
397 static char suffixes[] = { 0, 'K', 'M', 'G', 'T' };
398 int i;
399
400 for (i = 0; i < ARRAY_SIZE(suffixes) - 1; i++) {
401 if (*value < base || *value % base != 0)
402 break;
403 *value /= base;
404 }
405
406 return suffixes[i];
407}
408
409static ssize_t speed_bps_show(struct device *dev,
410 struct device_attribute *attr, char *buf)
411{
412 struct channel_path *chp = to_channelpath(dev);
413 unsigned long speed = chp->speed;
414 char suffix;
415
416 suffix = apply_max_suffix(&speed, 1000);
417
418 return suffix ? sysfs_emit(buf, "%lu%c\n", speed, suffix) :
419 sysfs_emit(buf, "%lu\n", speed);
420}
421
422static DEVICE_ATTR_RO(speed_bps);
423
b9dd6524
SO
424static ssize_t util_string_read(struct file *filp, struct kobject *kobj,
425 struct bin_attribute *attr, char *buf,
426 loff_t off, size_t count)
427{
428 struct channel_path *chp = to_channelpath(kobj_to_dev(kobj));
429 ssize_t rc;
430
431 mutex_lock(&chp->lock);
432 rc = memory_read_from_buffer(buf, count, &off, chp->desc_fmt3.util_str,
433 sizeof(chp->desc_fmt3.util_str));
434 mutex_unlock(&chp->lock);
435
436 return rc;
437}
438static BIN_ATTR_RO(util_string,
439 sizeof(((struct channel_path_desc_fmt3 *)0)->util_str));
440
441static struct bin_attribute *chp_bin_attrs[] = {
442 &bin_attr_util_string,
443 NULL,
444};
445
a0ea22c3 446static struct attribute *chp_attrs[] = {
e6b6e10a 447 &dev_attr_status.attr,
e5854a58 448 &dev_attr_configure.attr,
e6b6e10a
PO
449 &dev_attr_type.attr,
450 &dev_attr_cmg.attr,
451 &dev_attr_shared.attr,
ba54229d
PO
452 &dev_attr_chid.attr,
453 &dev_attr_chid_external.attr,
8b8b0915 454 &dev_attr_esc.attr,
0f987e6c 455 &dev_attr_speed_bps.attr,
e6b6e10a
PO
456 NULL,
457};
e6b6e10a
PO
458static struct attribute_group chp_attr_group = {
459 .attrs = chp_attrs,
b9dd6524 460 .bin_attrs = chp_bin_attrs,
e6b6e10a 461};
ed35ba9a
SO
462static const struct attribute_group *chp_attr_groups[] = {
463 &chp_attr_group,
464 NULL,
465};
e6b6e10a
PO
466
467static void chp_release(struct device *dev)
468{
469 struct channel_path *cp;
470
05469607 471 cp = to_channelpath(dev);
e6b6e10a
PO
472 kfree(cp);
473}
474
cce0eacc
PO
475/**
476 * chp_update_desc - update channel-path description
364e3f90 477 * @chp: channel-path
cce0eacc 478 *
9f3d6d7a
SO
479 * Update the channel-path description of the specified channel-path
480 * including channel measurement related information.
cce0eacc
PO
481 * Return zero on success, non-zero otherwise.
482 */
483int chp_update_desc(struct channel_path *chp)
484{
485 int rc;
486
ded27d8d 487 rc = chsc_determine_fmt0_channel_path_desc(chp->chpid, &chp->desc);
cce0eacc
PO
488 if (rc)
489 return rc;
490
0b601373
SO
491 /*
492 * Fetching the following data is optional. Not all machines or
493 * hypervisors implement the required chsc commands.
494 */
495 chsc_determine_fmt1_channel_path_desc(chp->chpid, &chp->desc_fmt1);
fcc6dd4b 496 chsc_determine_fmt3_channel_path_desc(chp->chpid, &chp->desc_fmt3);
0b601373 497 chsc_get_channel_measurement_chars(chp);
cce0eacc 498
0b601373 499 return 0;
cce0eacc
PO
500}
501
e6b6e10a
PO
502/**
503 * chp_new - register a new channel-path
364e3f90 504 * @chpid: channel-path ID
e6b6e10a
PO
505 *
506 * Create and register data structure representing new channel-path. Return
507 * zero on success, non-zero otherwise.
508 */
509int chp_new(struct chp_id chpid)
510{
98cc43ab 511 struct channel_subsystem *css = css_by_id(chpid.cssid);
e6b6e10a 512 struct channel_path *chp;
87dc8a01 513 int ret = 0;
e6b6e10a 514
87dc8a01 515 mutex_lock(&css->mutex);
e5854a58 516 if (chp_is_registered(chpid))
87dc8a01 517 goto out;
e6b6e10a 518
87dc8a01
SO
519 chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
520 if (!chp) {
521 ret = -ENOMEM;
522 goto out;
523 }
e6b6e10a
PO
524 /* fill in status, etc. */
525 chp->chpid = chpid;
526 chp->state = 1;
98cc43ab 527 chp->dev.parent = &css->device;
ed35ba9a 528 chp->dev.groups = chp_attr_groups;
e6b6e10a 529 chp->dev.release = chp_release;
b730f3a9 530 mutex_init(&chp->lock);
e6b6e10a
PO
531
532 /* Obtain channel path description and fill it in. */
cce0eacc 533 ret = chp_update_desc(chp);
e6b6e10a
PO
534 if (ret)
535 goto out_free;
c9182e0f
PO
536 if ((chp->desc.flags & 0x80) == 0) {
537 ret = -ENODEV;
538 goto out_free;
539 }
ec004407 540 dev_set_name(&chp->dev, "chp%x.%02x", chpid.cssid, chpid.id);
e6b6e10a
PO
541
542 /* make it known to the system */
543 ret = device_register(&chp->dev);
544 if (ret) {
e556bbbd
CH
545 CIO_MSG_EVENT(0, "Could not register chp%x.%02x: %d\n",
546 chpid.cssid, chpid.id, ret);
c6304933
SO
547 put_device(&chp->dev);
548 goto out;
e6b6e10a 549 }
87dc8a01 550
98cc43ab 551 if (css->cm_enabled) {
e6b6e10a
PO
552 ret = chp_add_cmg_attr(chp);
553 if (ret) {
e6b6e10a 554 device_unregister(&chp->dev);
a2164b81 555 goto out;
e6b6e10a
PO
556 }
557 }
98cc43ab 558 css->chps[chpid.id] = chp;
a2164b81 559 goto out;
e6b6e10a
PO
560out_free:
561 kfree(chp);
a2164b81 562out:
87dc8a01 563 mutex_unlock(&css->mutex);
e6b6e10a
PO
564 return ret;
565}
566
567/**
568 * chp_get_chp_desc - return newly allocated channel-path description
569 * @chpid: channel-path ID
570 *
571 * On success return a newly allocated copy of the channel-path description
572 * data associated with the given channel-path ID. Return %NULL on error.
573 */
ded27d8d 574struct channel_path_desc_fmt0 *chp_get_chp_desc(struct chp_id chpid)
e6b6e10a
PO
575{
576 struct channel_path *chp;
ded27d8d 577 struct channel_path_desc_fmt0 *desc;
e6b6e10a
PO
578
579 chp = chpid_to_chp(chpid);
580 if (!chp)
581 return NULL;
ded27d8d 582 desc = kmalloc(sizeof(*desc), GFP_KERNEL);
e6b6e10a
PO
583 if (!desc)
584 return NULL;
b730f3a9
SO
585
586 mutex_lock(&chp->lock);
ded27d8d 587 memcpy(desc, &chp->desc, sizeof(*desc));
b730f3a9 588 mutex_unlock(&chp->lock);
e6b6e10a
PO
589 return desc;
590}
591
592/**
593 * chp_process_crw - process channel-path status change
c1156189
CH
594 * @crw0: channel report-word to handler
595 * @crw1: second channel-report word (always NULL)
596 * @overflow: crw overflow indication
e6b6e10a
PO
597 *
598 * Handle channel-report-words indicating that the status of a channel-path
599 * has changed.
600 */
c1156189
CH
601static void chp_process_crw(struct crw *crw0, struct crw *crw1,
602 int overflow)
e6b6e10a
PO
603{
604 struct chp_id chpid;
605
c1156189
CH
606 if (overflow) {
607 css_schedule_eval_all();
608 return;
609 }
610 CIO_CRW_EVENT(2, "CRW reports slct=%d, oflw=%d, "
611 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
612 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
613 crw0->erc, crw0->rsid);
614 /*
615 * Check for solicited machine checks. These are
616 * created by reset channel path and need not be
617 * handled here.
618 */
619 if (crw0->slct) {
620 CIO_CRW_EVENT(2, "solicited machine check for "
621 "channel path %02X\n", crw0->rsid);
622 return;
623 }
e6b6e10a 624 chp_id_init(&chpid);
c1156189
CH
625 chpid.id = crw0->rsid;
626 switch (crw0->erc) {
627 case CRW_ERC_IPARM: /* Path has come. */
2daace78 628 case CRW_ERC_INIT:
87dc8a01 629 chp_new(chpid);
83b3370c 630 chsc_chp_online(chpid);
c1156189
CH
631 break;
632 case CRW_ERC_PERRI: /* Path has gone. */
633 case CRW_ERC_PERRN:
e6b6e10a 634 chsc_chp_offline(chpid);
c1156189
CH
635 break;
636 default:
637 CIO_CRW_EVENT(2, "Don't know how to handle erc=%x\n",
638 crw0->erc);
639 }
e6b6e10a 640}
e5854a58 641
99611f87 642int chp_ssd_get_mask(struct chsc_ssd_info *ssd, struct chp_link *link)
c820de39
CH
643{
644 int i;
645 int mask;
646
647 for (i = 0; i < 8; i++) {
648 mask = 0x80 >> i;
649 if (!(ssd->path_mask & mask))
650 continue;
99611f87 651 if (!chp_id_is_equal(&ssd->chpid[i], &link->chpid))
c820de39
CH
652 continue;
653 if ((ssd->fla_valid_mask & mask) &&
99611f87 654 ((ssd->fla[i] & link->fla_mask) != link->fla))
c820de39
CH
655 continue;
656 return mask;
657 }
658 return 0;
659}
660EXPORT_SYMBOL_GPL(chp_ssd_get_mask);
661
e5854a58
PO
662static inline int info_bit_num(struct chp_id id)
663{
664 return id.id + id.cssid * (__MAX_CHPID + 1);
665}
666
667/* Force chp_info refresh on next call to info_validate(). */
668static void info_expire(void)
669{
670 mutex_lock(&info_lock);
671 chp_info_expires = jiffies - 1;
672 mutex_unlock(&info_lock);
673}
674
675/* Ensure that chp_info is up-to-date. */
676static int info_update(void)
677{
678 int rc;
679
680 mutex_lock(&info_lock);
681 rc = 0;
682 if (time_after(jiffies, chp_info_expires)) {
683 /* Data is too old, update. */
684 rc = sclp_chp_read_info(&chp_info);
685 chp_info_expires = jiffies + CHP_INFO_UPDATE_INTERVAL ;
686 }
687 mutex_unlock(&info_lock);
688
689 return rc;
690}
691
692/**
693 * chp_info_get_status - retrieve configure status of a channel-path
694 * @chpid: channel-path ID
695 *
696 * On success, return 0 for standby, 1 for configured, 2 for reserved,
697 * 3 for not recognized. Return negative error code on error.
698 */
699int chp_info_get_status(struct chp_id chpid)
700{
701 int rc;
702 int bit;
703
704 rc = info_update();
705 if (rc)
706 return rc;
707
708 bit = info_bit_num(chpid);
709 mutex_lock(&info_lock);
710 if (!chp_test_bit(chp_info.recognized, bit))
711 rc = CHP_STATUS_NOT_RECOGNIZED;
712 else if (chp_test_bit(chp_info.configured, bit))
713 rc = CHP_STATUS_CONFIGURED;
714 else if (chp_test_bit(chp_info.standby, bit))
715 rc = CHP_STATUS_STANDBY;
716 else
717 rc = CHP_STATUS_RESERVED;
718 mutex_unlock(&info_lock);
719
720 return rc;
721}
722
723/* Return configure task for chpid. */
724static enum cfg_task_t cfg_get_task(struct chp_id chpid)
725{
726 return chp_cfg_task[chpid.cssid][chpid.id];
727}
728
729/* Set configure task for chpid. */
730static void cfg_set_task(struct chp_id chpid, enum cfg_task_t cfg)
731{
732 chp_cfg_task[chpid.cssid][chpid.id] = cfg;
733}
734
4475aeb8
SO
735/* Fetch the first configure task. Set chpid accordingly. */
736static enum cfg_task_t chp_cfg_fetch_task(struct chp_id *chpid)
737{
738 enum cfg_task_t t = cfg_none;
739
740 chp_id_for_each(chpid) {
741 t = cfg_get_task(*chpid);
742 if (t != cfg_none)
743 break;
744 }
745
746 return t;
747}
748
e5854a58
PO
749/* Perform one configure/deconfigure request. Reschedule work function until
750 * last request. */
751static void cfg_func(struct work_struct *work)
752{
753 struct chp_id chpid;
754 enum cfg_task_t t;
683c5418 755 int rc;
e5854a58 756
837c5220 757 spin_lock(&cfg_lock);
4475aeb8 758 t = chp_cfg_fetch_task(&chpid);
837c5220 759 spin_unlock(&cfg_lock);
e5854a58
PO
760
761 switch (t) {
762 case cfg_configure:
683c5418
PO
763 rc = sclp_chp_configure(chpid);
764 if (rc)
765 CIO_MSG_EVENT(2, "chp: sclp_chp_configure(%x.%02x)="
766 "%d\n", chpid.cssid, chpid.id, rc);
767 else {
768 info_expire();
769 chsc_chp_online(chpid);
770 }
e5854a58
PO
771 break;
772 case cfg_deconfigure:
683c5418
PO
773 rc = sclp_chp_deconfigure(chpid);
774 if (rc)
775 CIO_MSG_EVENT(2, "chp: sclp_chp_deconfigure(%x.%02x)="
776 "%d\n", chpid.cssid, chpid.id, rc);
777 else {
778 info_expire();
779 chsc_chp_offline(chpid);
780 }
e5854a58
PO
781 break;
782 case cfg_none:
783 /* Get updated information after last change. */
784 info_update();
e5854a58
PO
785 wake_up_interruptible(&cfg_wait_queue);
786 return;
787 }
4475aeb8
SO
788 spin_lock(&cfg_lock);
789 if (t == cfg_get_task(chpid))
790 cfg_set_task(chpid, cfg_none);
791 spin_unlock(&cfg_lock);
7b1058bc 792 schedule_work(&cfg_work);
e5854a58
PO
793}
794
795/**
796 * chp_cfg_schedule - schedule chpid configuration request
364e3f90
SO
797 * @chpid: channel-path ID
798 * @configure: Non-zero for configure, zero for deconfigure
e5854a58
PO
799 *
800 * Schedule a channel-path configuration/deconfiguration request.
801 */
802void chp_cfg_schedule(struct chp_id chpid, int configure)
803{
804 CIO_MSG_EVENT(2, "chp_cfg_sched%x.%02x=%d\n", chpid.cssid, chpid.id,
805 configure);
837c5220 806 spin_lock(&cfg_lock);
e5854a58 807 cfg_set_task(chpid, configure ? cfg_configure : cfg_deconfigure);
837c5220 808 spin_unlock(&cfg_lock);
7b1058bc 809 schedule_work(&cfg_work);
e5854a58
PO
810}
811
812/**
813 * chp_cfg_cancel_deconfigure - cancel chpid deconfiguration request
364e3f90 814 * @chpid: channel-path ID
e5854a58
PO
815 *
816 * Cancel an active channel-path deconfiguration request if it has not yet
817 * been performed.
818 */
819void chp_cfg_cancel_deconfigure(struct chp_id chpid)
820{
821 CIO_MSG_EVENT(2, "chp_cfg_cancel:%x.%02x\n", chpid.cssid, chpid.id);
837c5220 822 spin_lock(&cfg_lock);
e5854a58
PO
823 if (cfg_get_task(chpid) == cfg_deconfigure)
824 cfg_set_task(chpid, cfg_none);
837c5220 825 spin_unlock(&cfg_lock);
e5854a58
PO
826}
827
4475aeb8
SO
828static bool cfg_idle(void)
829{
830 struct chp_id chpid;
831 enum cfg_task_t t;
832
833 spin_lock(&cfg_lock);
834 t = chp_cfg_fetch_task(&chpid);
835 spin_unlock(&cfg_lock);
836
837 return t == cfg_none;
838}
839
e5854a58
PO
840static int cfg_wait_idle(void)
841{
4475aeb8 842 if (wait_event_interruptible(cfg_wait_queue, cfg_idle()))
e5854a58
PO
843 return -ERESTARTSYS;
844 return 0;
845}
846
847static int __init chp_init(void)
848{
849 struct chp_id chpid;
179a98cb 850 int state, ret;
e5854a58 851
f5daba1d 852 ret = crw_register_handler(CRW_RSC_CPATH, chp_process_crw);
c1156189
CH
853 if (ret)
854 return ret;
e5854a58 855 INIT_WORK(&cfg_work, cfg_func);
e5854a58
PO
856 if (info_update())
857 return 0;
858 /* Register available channel-paths. */
859 chp_id_for_each(&chpid) {
179a98cb
SO
860 state = chp_info_get_status(chpid);
861 if (state == CHP_STATUS_CONFIGURED ||
862 state == CHP_STATUS_STANDBY)
e5854a58
PO
863 chp_new(chpid);
864 }
865
866 return 0;
867}
868
869subsys_initcall(chp_init);