xref: /linux/drivers/s390/cio/chp.c (revision f3d8b0ebaec4e755317e2e8b863a4004d6505288)
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 	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  */
chp_is_registered(struct chp_id chpid)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  */
s390_vary_chpid(struct chp_id chpid,int on)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  */
measurement_chars_read(struct file * filp,struct kobject * kobj,const struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)130 static ssize_t measurement_chars_read(struct file *filp, struct kobject *kobj,
131 				      const 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 const BIN_ATTR_ADMIN_RO(measurement_chars, sizeof(struct cmg_chars));
146 
measurement_chars_full_read(struct file * filp,struct kobject * kobj,const struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)147 static ssize_t measurement_chars_full_read(struct file *filp,
148 					   struct kobject *kobj,
149 					   const 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 
chp_measurement_copy_block(void * buf,loff_t off,size_t count,struct kobject * kobj,bool extended)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 
measurement_read(struct file * filp,struct kobject * kobj,const struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)198 static ssize_t measurement_read(struct file *filp, struct kobject *kobj,
199 				const 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 const BIN_ATTR_ADMIN_RO(measurement, sizeof(struct cmg_entry));
205 
ext_measurement_read(struct file * filp,struct kobject * kobj,const struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)206 static ssize_t ext_measurement_read(struct file *filp, struct kobject *kobj,
207 				    const 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 const BIN_ATTR_ADMIN_RO(ext_measurement, sizeof(struct cmg_ext_entry));
213 
214 static const 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 
chp_remove_cmg_attr(struct channel_path * chp)223 void chp_remove_cmg_attr(struct channel_path *chp)
224 {
225 	device_remove_groups(&chp->dev, measurement_groups);
226 }
227 
chp_add_cmg_attr(struct channel_path * chp)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  */
chp_status_show(struct device * dev,struct device_attribute * attr,char * buf)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 
chp_status_write(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)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 
chp_configure_show(struct device * dev,struct device_attribute * attr,char * buf)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 
chp_configure_write(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)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 
chp_type_show(struct device * dev,struct device_attribute * attr,char * buf)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 
chp_cmg_show(struct device * dev,struct device_attribute * attr,char * buf)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 
chp_shared_show(struct device * dev,struct device_attribute * attr,char * buf)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 
chp_chid_show(struct device * dev,struct device_attribute * attr,char * buf)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 
chp_chid_external_show(struct device * dev,struct device_attribute * attr,char * buf)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 
chp_esc_show(struct device * dev,struct device_attribute * attr,char * buf)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 
apply_max_suffix(unsigned long * value,unsigned long base)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 
speed_bps_show(struct device * dev,struct device_attribute * attr,char * buf)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 
util_string_read(struct file * filp,struct kobject * kobj,const struct bin_attribute * attr,char * buf,loff_t off,size_t count)437 static ssize_t util_string_read(struct file *filp, struct kobject *kobj,
438 				const 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 const BIN_ATTR_RO(util_string,
452 			 sizeof(((struct channel_path_desc_fmt3 *)0)->util_str));
453 
454 static const struct bin_attribute *const 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 const struct attribute_group chp_attr_group = {
472 	.attrs = chp_attrs,
473 	.bin_attrs_new = chp_bin_attrs,
474 };
475 static const struct attribute_group *chp_attr_groups[] = {
476 	&chp_attr_group,
477 	NULL,
478 };
479 
chp_release(struct device * dev)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  */
chp_update_desc(struct channel_path * chp)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  */
chp_new(struct chp_id chpid)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  */
chp_get_chp_desc(struct chp_id chpid)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  */
chp_process_crw(struct crw * crw0,struct crw * crw1,int overflow)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 
chp_ssd_get_mask(struct chsc_ssd_info * ssd,struct chp_link * link)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 
info_bit_num(struct chp_id id)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(). */
info_expire(void)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. */
info_update(void)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 		if (!rc)
699 			chp_info_expires = jiffies + CHP_INFO_UPDATE_INTERVAL;
700 	}
701 	mutex_unlock(&info_lock);
702 
703 	return rc;
704 }
705 
706 /**
707  * chp_info_get_status - retrieve configure status of a channel-path
708  * @chpid: channel-path ID
709  *
710  * On success, return 0 for standby, 1 for configured, 2 for reserved,
711  * 3 for not recognized. Return negative error code on error.
712  */
chp_info_get_status(struct chp_id chpid)713 int chp_info_get_status(struct chp_id chpid)
714 {
715 	int rc;
716 	int bit;
717 
718 	rc = info_update();
719 	if (rc)
720 		return rc;
721 
722 	bit = info_bit_num(chpid);
723 	mutex_lock(&info_lock);
724 	if (!chp_test_bit(chp_info.recognized, bit))
725 		rc = CHP_STATUS_NOT_RECOGNIZED;
726 	else if (chp_test_bit(chp_info.configured, bit))
727 		rc = CHP_STATUS_CONFIGURED;
728 	else if (chp_test_bit(chp_info.standby, bit))
729 		rc = CHP_STATUS_STANDBY;
730 	else
731 		rc = CHP_STATUS_RESERVED;
732 	mutex_unlock(&info_lock);
733 
734 	return rc;
735 }
736 
737 /* Return configure task for chpid. */
cfg_get_task(struct chp_id chpid)738 static enum cfg_task_t cfg_get_task(struct chp_id chpid)
739 {
740 	return chp_cfg_task[chpid.cssid][chpid.id];
741 }
742 
743 /* Set configure task for chpid. */
cfg_set_task(struct chp_id chpid,enum cfg_task_t cfg)744 static void cfg_set_task(struct chp_id chpid, enum cfg_task_t cfg)
745 {
746 	chp_cfg_task[chpid.cssid][chpid.id] = cfg;
747 }
748 
749 /* Fetch the first configure task. Set chpid accordingly. */
chp_cfg_fetch_task(struct chp_id * chpid)750 static enum cfg_task_t chp_cfg_fetch_task(struct chp_id *chpid)
751 {
752 	enum cfg_task_t t = cfg_none;
753 
754 	chp_id_for_each(chpid) {
755 		t = cfg_get_task(*chpid);
756 		if (t != cfg_none)
757 			break;
758 	}
759 
760 	return t;
761 }
762 
763 /* Perform one configure/deconfigure request. Reschedule work function until
764  * last request. */
cfg_func(struct work_struct * work)765 static void cfg_func(struct work_struct *work)
766 {
767 	struct chp_id chpid;
768 	enum cfg_task_t t;
769 	int rc;
770 
771 	spin_lock(&cfg_lock);
772 	t = chp_cfg_fetch_task(&chpid);
773 	spin_unlock(&cfg_lock);
774 
775 	switch (t) {
776 	case cfg_configure:
777 		rc = sclp_chp_configure(chpid);
778 		if (rc)
779 			CIO_MSG_EVENT(2, "chp: sclp_chp_configure(%x.%02x)="
780 				      "%d\n", chpid.cssid, chpid.id, rc);
781 		else {
782 			info_expire();
783 			chsc_chp_online(chpid);
784 		}
785 		break;
786 	case cfg_deconfigure:
787 		rc = sclp_chp_deconfigure(chpid);
788 		if (rc)
789 			CIO_MSG_EVENT(2, "chp: sclp_chp_deconfigure(%x.%02x)="
790 				      "%d\n", chpid.cssid, chpid.id, rc);
791 		else {
792 			info_expire();
793 			chsc_chp_offline(chpid);
794 		}
795 		break;
796 	case cfg_none:
797 		/* Get updated information after last change. */
798 		info_update();
799 		wake_up_interruptible(&cfg_wait_queue);
800 		return;
801 	}
802 	spin_lock(&cfg_lock);
803 	if (t == cfg_get_task(chpid))
804 		cfg_set_task(chpid, cfg_none);
805 	spin_unlock(&cfg_lock);
806 	schedule_work(&cfg_work);
807 }
808 
809 /**
810  * chp_cfg_schedule - schedule chpid configuration request
811  * @chpid: channel-path ID
812  * @configure: Non-zero for configure, zero for deconfigure
813  *
814  * Schedule a channel-path configuration/deconfiguration request.
815  */
chp_cfg_schedule(struct chp_id chpid,int configure)816 void chp_cfg_schedule(struct chp_id chpid, int configure)
817 {
818 	CIO_MSG_EVENT(2, "chp_cfg_sched%x.%02x=%d\n", chpid.cssid, chpid.id,
819 		      configure);
820 	spin_lock(&cfg_lock);
821 	cfg_set_task(chpid, configure ? cfg_configure : cfg_deconfigure);
822 	spin_unlock(&cfg_lock);
823 	schedule_work(&cfg_work);
824 }
825 
826 /**
827  * chp_cfg_cancel_deconfigure - cancel chpid deconfiguration request
828  * @chpid: channel-path ID
829  *
830  * Cancel an active channel-path deconfiguration request if it has not yet
831  * been performed.
832  */
chp_cfg_cancel_deconfigure(struct chp_id chpid)833 void chp_cfg_cancel_deconfigure(struct chp_id chpid)
834 {
835 	CIO_MSG_EVENT(2, "chp_cfg_cancel:%x.%02x\n", chpid.cssid, chpid.id);
836 	spin_lock(&cfg_lock);
837 	if (cfg_get_task(chpid) == cfg_deconfigure)
838 		cfg_set_task(chpid, cfg_none);
839 	spin_unlock(&cfg_lock);
840 }
841 
cfg_idle(void)842 static bool cfg_idle(void)
843 {
844 	struct chp_id chpid;
845 	enum cfg_task_t t;
846 
847 	spin_lock(&cfg_lock);
848 	t = chp_cfg_fetch_task(&chpid);
849 	spin_unlock(&cfg_lock);
850 
851 	return t == cfg_none;
852 }
853 
cfg_wait_idle(void)854 static int cfg_wait_idle(void)
855 {
856 	if (wait_event_interruptible(cfg_wait_queue, cfg_idle()))
857 		return -ERESTARTSYS;
858 	return 0;
859 }
860 
chp_init(void)861 static int __init chp_init(void)
862 {
863 	struct chp_id chpid;
864 	int state, ret;
865 
866 	ret = crw_register_handler(CRW_RSC_CPATH, chp_process_crw);
867 	if (ret)
868 		return ret;
869 	INIT_WORK(&cfg_work, cfg_func);
870 	if (info_update())
871 		return 0;
872 	/* Register available channel-paths. */
873 	chp_id_for_each(&chpid) {
874 		state = chp_info_get_status(chpid);
875 		if (state == CHP_STATUS_CONFIGURED ||
876 		    state == CHP_STATUS_STANDBY)
877 			chp_new(chpid);
878 	}
879 
880 	return 0;
881 }
882 
883 subsys_initcall(chp_init);
884