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