xref: /linux/drivers/s390/cio/chp.c (revision 621cde16e49b3ecf7d59a8106a20aaebfb4a59a9)
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,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 				      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 
chp_measurement_copy_block(void * buf,loff_t off,size_t count,struct kobject * kobj,bool extended)147 static ssize_t chp_measurement_copy_block(void *buf, loff_t off, size_t count,
148 					  struct kobject *kobj, bool extended)
149 {
150 	struct channel_path *chp;
151 	struct channel_subsystem *css;
152 	struct device *device;
153 	unsigned int size;
154 	void *area, *entry;
155 	int id, idx;
156 
157 	device = kobj_to_dev(kobj);
158 	chp = to_channelpath(device);
159 	css = to_css(chp->dev.parent);
160 	id = chp->chpid.id;
161 
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);
176 
177 	/* Only allow single reads. */
178 	if (off || count < size)
179 		return 0;
180 
181 	memcpy(buf, entry, size);
182 
183 	return size;
184 }
185 
measurement_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)186 static 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);
191 }
192 static BIN_ATTR_ADMIN_RO(measurement, sizeof(struct cmg_entry));
193 
ext_measurement_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)194 static 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 }
200 static BIN_ATTR_ADMIN_RO(ext_measurement, sizeof(struct cmg_ext_entry));
201 
202 static struct bin_attribute *measurement_attrs[] = {
203 	&bin_attr_measurement_chars,
204 	&bin_attr_measurement,
205 	&bin_attr_ext_measurement,
206 	NULL,
207 };
208 BIN_ATTRIBUTE_GROUPS(measurement);
209 
chp_remove_cmg_attr(struct channel_path * chp)210 void chp_remove_cmg_attr(struct channel_path *chp)
211 {
212 	device_remove_groups(&chp->dev, measurement_groups);
213 }
214 
chp_add_cmg_attr(struct channel_path * chp)215 int chp_add_cmg_attr(struct channel_path *chp)
216 {
217 	return device_add_groups(&chp->dev, measurement_groups);
218 }
219 
220 /*
221  * Files for the channel path entries.
222  */
chp_status_show(struct device * dev,struct device_attribute * attr,char * buf)223 static ssize_t chp_status_show(struct device *dev,
224 			       struct device_attribute *attr, char *buf)
225 {
226 	struct channel_path *chp = to_channelpath(dev);
227 	int status;
228 
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");
234 }
235 
chp_status_write(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)236 static ssize_t chp_status_write(struct device *dev,
237 				struct device_attribute *attr,
238 				const char *buf, size_t count)
239 {
240 	struct channel_path *cp = to_channelpath(dev);
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 
249 	/* Wait until previous actions have settled. */
250 	css_wait_for_slow_path();
251 
252 	if (!strncasecmp(cmd, "on", 2) || !strcmp(cmd, "1")) {
253 		mutex_lock(&cp->lock);
254 		error = s390_vary_chpid(cp->chpid, 1);
255 		mutex_unlock(&cp->lock);
256 	} else if (!strncasecmp(cmd, "off", 3) || !strcmp(cmd, "0")) {
257 		mutex_lock(&cp->lock);
258 		error = s390_vary_chpid(cp->chpid, 0);
259 		mutex_unlock(&cp->lock);
260 	} else
261 		error = -EINVAL;
262 
263 	return error < 0 ? error : count;
264 }
265 
266 static DEVICE_ATTR(status, 0644, chp_status_show, chp_status_write);
267 
chp_configure_show(struct device * dev,struct device_attribute * attr,char * buf)268 static 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 
274 	cp = to_channelpath(dev);
275 	status = chp_info_get_status(cp->chpid);
276 	if (status < 0)
277 		return status;
278 
279 	return sysfs_emit(buf, "%d\n", status);
280 }
281 
282 static int cfg_wait_idle(void);
283 
chp_configure_write(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)284 static 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;
296 	cp = to_channelpath(dev);
297 	chp_cfg_schedule(cp->chpid, val);
298 	cfg_wait_idle();
299 
300 	return count;
301 }
302 
303 static DEVICE_ATTR(configure, 0644, chp_configure_show, chp_configure_write);
304 
chp_type_show(struct device * dev,struct device_attribute * attr,char * buf)305 static ssize_t chp_type_show(struct device *dev, struct device_attribute *attr,
306 			     char *buf)
307 {
308 	struct channel_path *chp = to_channelpath(dev);
309 	u8 type;
310 
311 	mutex_lock(&chp->lock);
312 	type = chp->desc.desc;
313 	mutex_unlock(&chp->lock);
314 	return sprintf(buf, "%x\n", type);
315 }
316 
317 static DEVICE_ATTR(type, 0444, chp_type_show, NULL);
318 
chp_cmg_show(struct device * dev,struct device_attribute * attr,char * buf)319 static 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");
328 	return sprintf(buf, "%d\n", chp->cmg);
329 }
330 
331 static DEVICE_ATTR(cmg, 0444, chp_cmg_show, NULL);
332 
chp_shared_show(struct device * dev,struct device_attribute * attr,char * buf)333 static 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 
345 static DEVICE_ATTR(shared, 0444, chp_shared_show, NULL);
346 
chp_chid_show(struct device * dev,struct device_attribute * attr,char * buf)347 static 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 }
362 static DEVICE_ATTR(chid, 0444, chp_chid_show, NULL);
363 
chp_chid_external_show(struct device * dev,struct device_attribute * attr,char * buf)364 static 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 }
379 static DEVICE_ATTR(chid_external, 0444, chp_chid_external_show, NULL);
380 
chp_esc_show(struct device * dev,struct device_attribute * attr,char * buf)381 static 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 }
393 static DEVICE_ATTR(esc, 0444, chp_esc_show, NULL);
394 
apply_max_suffix(unsigned long * value,unsigned long base)395 static 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 
speed_bps_show(struct device * dev,struct device_attribute * attr,char * buf)409 static 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 
422 static DEVICE_ATTR_RO(speed_bps);
423 
util_string_read(struct file * filp,struct kobject * kobj,struct bin_attribute * attr,char * buf,loff_t off,size_t count)424 static 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 }
438 static BIN_ATTR_RO(util_string,
439 		   sizeof(((struct channel_path_desc_fmt3 *)0)->util_str));
440 
441 static struct bin_attribute *chp_bin_attrs[] = {
442 	&bin_attr_util_string,
443 	NULL,
444 };
445 
446 static struct attribute *chp_attrs[] = {
447 	&dev_attr_status.attr,
448 	&dev_attr_configure.attr,
449 	&dev_attr_type.attr,
450 	&dev_attr_cmg.attr,
451 	&dev_attr_shared.attr,
452 	&dev_attr_chid.attr,
453 	&dev_attr_chid_external.attr,
454 	&dev_attr_esc.attr,
455 	&dev_attr_speed_bps.attr,
456 	NULL,
457 };
458 static struct attribute_group chp_attr_group = {
459 	.attrs = chp_attrs,
460 	.bin_attrs = chp_bin_attrs,
461 };
462 static const struct attribute_group *chp_attr_groups[] = {
463 	&chp_attr_group,
464 	NULL,
465 };
466 
chp_release(struct device * dev)467 static void chp_release(struct device *dev)
468 {
469 	struct channel_path *cp;
470 
471 	cp = to_channelpath(dev);
472 	kfree(cp);
473 }
474 
475 /**
476  * chp_update_desc - update channel-path description
477  * @chp: channel-path
478  *
479  * Update the channel-path description of the specified channel-path
480  * including channel measurement related information.
481  * Return zero on success, non-zero otherwise.
482  */
chp_update_desc(struct channel_path * chp)483 int chp_update_desc(struct channel_path *chp)
484 {
485 	int rc;
486 
487 	rc = chsc_determine_fmt0_channel_path_desc(chp->chpid, &chp->desc);
488 	if (rc)
489 		return rc;
490 
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);
496 	chsc_determine_fmt3_channel_path_desc(chp->chpid, &chp->desc_fmt3);
497 	chsc_get_channel_measurement_chars(chp);
498 
499 	return 0;
500 }
501 
502 /**
503  * chp_new - register a new channel-path
504  * @chpid: channel-path ID
505  *
506  * Create and register data structure representing new channel-path. Return
507  * zero on success, non-zero otherwise.
508  */
chp_new(struct chp_id chpid)509 int chp_new(struct chp_id chpid)
510 {
511 	struct channel_subsystem *css = css_by_id(chpid.cssid);
512 	struct channel_path *chp;
513 	int ret = 0;
514 
515 	mutex_lock(&css->mutex);
516 	if (chp_is_registered(chpid))
517 		goto out;
518 
519 	chp = kzalloc(sizeof(struct channel_path), GFP_KERNEL);
520 	if (!chp) {
521 		ret = -ENOMEM;
522 		goto out;
523 	}
524 	/* fill in status, etc. */
525 	chp->chpid = chpid;
526 	chp->state = 1;
527 	chp->dev.parent = &css->device;
528 	chp->dev.groups = chp_attr_groups;
529 	chp->dev.release = chp_release;
530 	mutex_init(&chp->lock);
531 
532 	/* Obtain channel path description and fill it in. */
533 	ret = chp_update_desc(chp);
534 	if (ret)
535 		goto out_free;
536 	if ((chp->desc.flags & 0x80) == 0) {
537 		ret = -ENODEV;
538 		goto out_free;
539 	}
540 	dev_set_name(&chp->dev, "chp%x.%02x", chpid.cssid, chpid.id);
541 
542 	/* make it known to the system */
543 	ret = device_register(&chp->dev);
544 	if (ret) {
545 		CIO_MSG_EVENT(0, "Could not register chp%x.%02x: %d\n",
546 			      chpid.cssid, chpid.id, ret);
547 		put_device(&chp->dev);
548 		goto out;
549 	}
550 
551 	if (css->cm_enabled) {
552 		ret = chp_add_cmg_attr(chp);
553 		if (ret) {
554 			device_unregister(&chp->dev);
555 			goto out;
556 		}
557 	}
558 	css->chps[chpid.id] = chp;
559 	goto out;
560 out_free:
561 	kfree(chp);
562 out:
563 	mutex_unlock(&css->mutex);
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  */
chp_get_chp_desc(struct chp_id chpid)574 struct channel_path_desc_fmt0 *chp_get_chp_desc(struct chp_id chpid)
575 {
576 	struct channel_path *chp;
577 	struct channel_path_desc_fmt0 *desc;
578 
579 	chp = chpid_to_chp(chpid);
580 	if (!chp)
581 		return NULL;
582 	desc = kmalloc(sizeof(*desc), GFP_KERNEL);
583 	if (!desc)
584 		return NULL;
585 
586 	mutex_lock(&chp->lock);
587 	memcpy(desc, &chp->desc, sizeof(*desc));
588 	mutex_unlock(&chp->lock);
589 	return desc;
590 }
591 
592 /**
593  * chp_process_crw - process channel-path status change
594  * @crw0: channel report-word to handler
595  * @crw1: second channel-report word (always NULL)
596  * @overflow: crw overflow indication
597  *
598  * Handle channel-report-words indicating that the status of a channel-path
599  * has changed.
600  */
chp_process_crw(struct crw * crw0,struct crw * crw1,int overflow)601 static void chp_process_crw(struct crw *crw0, struct crw *crw1,
602 			    int overflow)
603 {
604 	struct chp_id chpid;
605 
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 	}
624 	chp_id_init(&chpid);
625 	chpid.id = crw0->rsid;
626 	switch (crw0->erc) {
627 	case CRW_ERC_IPARM: /* Path has come. */
628 	case CRW_ERC_INIT:
629 		chp_new(chpid);
630 		chsc_chp_online(chpid);
631 		break;
632 	case CRW_ERC_PERRI: /* Path has gone. */
633 	case CRW_ERC_PERRN:
634 		chsc_chp_offline(chpid);
635 		break;
636 	default:
637 		CIO_CRW_EVENT(2, "Don't know how to handle erc=%x\n",
638 			      crw0->erc);
639 	}
640 }
641 
chp_ssd_get_mask(struct chsc_ssd_info * ssd,struct chp_link * link)642 int chp_ssd_get_mask(struct chsc_ssd_info *ssd, struct chp_link *link)
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;
651 		if (!chp_id_is_equal(&ssd->chpid[i], &link->chpid))
652 			continue;
653 		if ((ssd->fla_valid_mask & mask) &&
654 		    ((ssd->fla[i] & link->fla_mask) != link->fla))
655 			continue;
656 		return mask;
657 	}
658 	return 0;
659 }
660 EXPORT_SYMBOL_GPL(chp_ssd_get_mask);
661 
info_bit_num(struct chp_id id)662 static 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(). */
info_expire(void)668 static 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. */
info_update(void)676 static 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  */
chp_info_get_status(struct chp_id chpid)699 int 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. */
cfg_get_task(struct chp_id chpid)724 static 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. */
cfg_set_task(struct chp_id chpid,enum cfg_task_t cfg)730 static 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 
735 /* Fetch the first configure task. Set chpid accordingly. */
chp_cfg_fetch_task(struct chp_id * chpid)736 static 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 
749 /* Perform one configure/deconfigure request. Reschedule work function until
750  * last request. */
cfg_func(struct work_struct * work)751 static void cfg_func(struct work_struct *work)
752 {
753 	struct chp_id chpid;
754 	enum cfg_task_t t;
755 	int rc;
756 
757 	spin_lock(&cfg_lock);
758 	t = chp_cfg_fetch_task(&chpid);
759 	spin_unlock(&cfg_lock);
760 
761 	switch (t) {
762 	case cfg_configure:
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 		}
771 		break;
772 	case cfg_deconfigure:
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 		}
781 		break;
782 	case cfg_none:
783 		/* Get updated information after last change. */
784 		info_update();
785 		wake_up_interruptible(&cfg_wait_queue);
786 		return;
787 	}
788 	spin_lock(&cfg_lock);
789 	if (t == cfg_get_task(chpid))
790 		cfg_set_task(chpid, cfg_none);
791 	spin_unlock(&cfg_lock);
792 	schedule_work(&cfg_work);
793 }
794 
795 /**
796  * chp_cfg_schedule - schedule chpid configuration request
797  * @chpid: channel-path ID
798  * @configure: Non-zero for configure, zero for deconfigure
799  *
800  * Schedule a channel-path configuration/deconfiguration request.
801  */
chp_cfg_schedule(struct chp_id chpid,int configure)802 void 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);
806 	spin_lock(&cfg_lock);
807 	cfg_set_task(chpid, configure ? cfg_configure : cfg_deconfigure);
808 	spin_unlock(&cfg_lock);
809 	schedule_work(&cfg_work);
810 }
811 
812 /**
813  * chp_cfg_cancel_deconfigure - cancel chpid deconfiguration request
814  * @chpid: channel-path ID
815  *
816  * Cancel an active channel-path deconfiguration request if it has not yet
817  * been performed.
818  */
chp_cfg_cancel_deconfigure(struct chp_id chpid)819 void chp_cfg_cancel_deconfigure(struct chp_id chpid)
820 {
821 	CIO_MSG_EVENT(2, "chp_cfg_cancel:%x.%02x\n", chpid.cssid, chpid.id);
822 	spin_lock(&cfg_lock);
823 	if (cfg_get_task(chpid) == cfg_deconfigure)
824 		cfg_set_task(chpid, cfg_none);
825 	spin_unlock(&cfg_lock);
826 }
827 
cfg_idle(void)828 static 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 
cfg_wait_idle(void)840 static int cfg_wait_idle(void)
841 {
842 	if (wait_event_interruptible(cfg_wait_queue, cfg_idle()))
843 		return -ERESTARTSYS;
844 	return 0;
845 }
846 
chp_init(void)847 static int __init chp_init(void)
848 {
849 	struct chp_id chpid;
850 	int state, ret;
851 
852 	ret = crw_register_handler(CRW_RSC_CPATH, chp_process_crw);
853 	if (ret)
854 		return ret;
855 	INIT_WORK(&cfg_work, cfg_func);
856 	if (info_update())
857 		return 0;
858 	/* Register available channel-paths. */
859 	chp_id_for_each(&chpid) {
860 		state = chp_info_get_status(chpid);
861 		if (state == CHP_STATUS_CONFIGURED ||
862 		    state == CHP_STATUS_STANDBY)
863 			chp_new(chpid);
864 	}
865 
866 	return 0;
867 }
868 
869 subsys_initcall(chp_init);
870