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