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