blob: 9263a0fb385845588ceb7a80a5f6a2edc9449556 [file] [log] [blame]
Greg Kroah-Hartman724117b2017-11-14 18:38:02 +01001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02003 * driver for channel subsystem
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Sebastian Ott34aec072010-10-25 16:10:28 +02005 * Copyright IBM Corp. 2002, 2010
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02006 *
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
8 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
Michael Ernste6d5a422008-12-25 13:39:36 +010010
11#define KMSG_COMPONENT "cio"
12#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13
Paul Gortmakera00f7612016-10-30 16:37:24 -040014#include <linux/export.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/init.h>
16#include <linux/device.h>
17#include <linux/slab.h>
18#include <linux/errno.h>
19#include <linux/list.h>
Cornelia Hucka55360d2007-10-12 16:11:20 +020020#include <linux/reboot.h>
Sebastian Ottdcbd16d2009-06-16 10:30:22 +020021#include <linux/suspend.h>
Sebastian Ott879acca52010-02-26 22:37:25 +010022#include <linux/proc_fs.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020023#include <asm/isc.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010024#include <asm/crw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "css.h"
27#include "cio.h"
28#include "cio_debug.h"
29#include "ioasm.h"
30#include "chsc.h"
Peter Oberparleiter40154b82006-06-29 14:57:03 +020031#include "device.h"
Peter Oberparleiter83b33702007-04-27 16:01:34 +020032#include "idset.h"
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +020033#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035int css_init_done = 0;
Sebastian Ottb0a285d2009-09-22 22:58:37 +020036int max_ssid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Sebastian Ott98cc43a2016-11-08 14:28:03 +010038#define MAX_CSS_IDX 0
39struct channel_subsystem *channel_subsystems[MAX_CSS_IDX + 1];
Sebastian Ott3041b6a2011-03-15 17:08:31 +010040static struct bus_type css_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041
Heiko Carstens4d284ca2007-02-05 21:18:53 +010042int
Cornelia Huckf97a56f2006-01-06 00:19:22 -080043for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
44{
45 struct subchannel_id schid;
46 int ret;
47
48 init_subchannel_id(&schid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080049 do {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080050 do {
51 ret = fn(schid, data);
52 if (ret)
53 break;
54 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
55 schid.sch_no = 0;
56 } while (schid.ssid++ < max_ssid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080057 return ret;
58}
59
Peter Oberparleitere82a1562008-01-26 14:10:48 +010060struct cb_data {
61 void *data;
62 struct idset *set;
63 int (*fn_known_sch)(struct subchannel *, void *);
64 int (*fn_unknown_sch)(struct subchannel_id, void *);
65};
66
67static int call_fn_known_sch(struct device *dev, void *data)
68{
69 struct subchannel *sch = to_subchannel(dev);
70 struct cb_data *cb = data;
71 int rc = 0;
72
Peter Oberparleiter47d30672013-11-26 14:59:21 +010073 if (cb->set)
74 idset_sch_del(cb->set, sch->schid);
Peter Oberparleitere82a1562008-01-26 14:10:48 +010075 if (cb->fn_known_sch)
76 rc = cb->fn_known_sch(sch, cb->data);
77 return rc;
78}
79
80static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
81{
82 struct cb_data *cb = data;
83 int rc = 0;
84
85 if (idset_sch_contains(cb->set, schid))
86 rc = cb->fn_unknown_sch(schid, cb->data);
87 return rc;
88}
89
Sebastian Ott90ac24a2009-03-26 15:24:11 +010090static int call_fn_all_sch(struct subchannel_id schid, void *data)
91{
92 struct cb_data *cb = data;
93 struct subchannel *sch;
94 int rc = 0;
95
96 sch = get_subchannel_by_schid(schid);
97 if (sch) {
98 if (cb->fn_known_sch)
99 rc = cb->fn_known_sch(sch, cb->data);
100 put_device(&sch->dev);
101 } else {
102 if (cb->fn_unknown_sch)
103 rc = cb->fn_unknown_sch(schid, cb->data);
104 }
105
106 return rc;
107}
108
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100109int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
110 int (*fn_unknown)(struct subchannel_id,
111 void *), void *data)
112{
113 struct cb_data cb;
114 int rc;
115
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100116 cb.data = data;
117 cb.fn_known_sch = fn_known;
118 cb.fn_unknown_sch = fn_unknown;
Sebastian Ott90ac24a2009-03-26 15:24:11 +0100119
Peter Oberparleiter47d30672013-11-26 14:59:21 +0100120 if (fn_known && !fn_unknown) {
121 /* Skip idset allocation in case of known-only loop. */
122 cb.set = NULL;
123 return bus_for_each_dev(&css_bus_type, NULL, &cb,
124 call_fn_known_sch);
125 }
126
Sebastian Ott90ac24a2009-03-26 15:24:11 +0100127 cb.set = idset_sch_new();
128 if (!cb.set)
129 /* fall back to brute force scanning in case of oom */
130 return for_each_subchannel(call_fn_all_sch, &cb);
131
132 idset_fill(cb.set);
133
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100134 /* Process registered subchannels. */
135 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
136 if (rc)
137 goto out;
138 /* Process unregistered subchannels. */
139 if (fn_unknown)
140 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
141out:
142 idset_free(cb.set);
143
144 return rc;
145}
146
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100147static void css_sch_todo(struct work_struct *work);
148
Sebastian Otte5dcf002013-04-13 13:08:01 +0200149static int css_sch_create_locks(struct subchannel *sch)
150{
151 sch->lock = kmalloc(sizeof(*sch->lock), GFP_KERNEL);
152 if (!sch->lock)
153 return -ENOMEM;
154
155 spin_lock_init(sch->lock);
156 mutex_init(&sch->reg_mutex);
157
158 return 0;
159}
160
Sebastian Ottc135ad12013-04-13 12:58:55 +0200161static void css_subchannel_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
Sebastian Ott863fc842013-04-13 13:01:50 +0200163 struct subchannel *sch = to_subchannel(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
Sebastian Ott863fc842013-04-13 13:01:50 +0200165 sch->config.intparm = 0;
166 cio_commit_config(sch);
167 kfree(sch->lock);
168 kfree(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Sebastian Ott863fc842013-04-13 13:01:50 +0200171struct subchannel *css_alloc_subchannel(struct subchannel_id schid)
Sebastian Ottc135ad12013-04-13 12:58:55 +0200172{
173 struct subchannel *sch;
174 int ret;
175
Sebastian Otte5dcf002013-04-13 13:08:01 +0200176 sch = kzalloc(sizeof(*sch), GFP_KERNEL | GFP_DMA);
177 if (!sch)
Sebastian Ottc135ad12013-04-13 12:58:55 +0200178 return ERR_PTR(-ENOMEM);
Sebastian Otte5dcf002013-04-13 13:08:01 +0200179
180 ret = cio_validate_subchannel(sch, schid);
181 if (ret < 0)
182 goto err;
183
184 ret = css_sch_create_locks(sch);
185 if (ret)
186 goto err;
187
Sebastian Ottc135ad12013-04-13 12:58:55 +0200188 INIT_WORK(&sch->todo_work, css_sch_todo);
189 sch->dev.release = &css_subchannel_release;
190 device_initialize(&sch->dev);
191 return sch;
Sebastian Otte5dcf002013-04-13 13:08:01 +0200192
193err:
194 kfree(sch);
195 return ERR_PTR(ret);
Sebastian Ottc135ad12013-04-13 12:58:55 +0200196}
197
Cornelia Huck07c6a332007-07-27 12:29:10 +0200198static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200199{
200 int ret;
201
202 mutex_lock(&sch->reg_mutex);
Sebastian Ott6ee4fec2009-09-11 10:28:25 +0200203 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
204 sch->schid.sch_no);
Sebastian Ottc135ad12013-04-13 12:58:55 +0200205 ret = device_add(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200206 mutex_unlock(&sch->reg_mutex);
207 return ret;
208}
209
Cornelia Huck44a1c192008-07-14 09:58:47 +0200210/**
211 * css_sch_device_unregister - unregister a subchannel
212 * @sch: subchannel to be unregistered
213 */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200214void css_sch_device_unregister(struct subchannel *sch)
215{
216 mutex_lock(&sch->reg_mutex);
Sebastian Ottef60cd12008-07-14 09:59:20 +0200217 if (device_is_registered(&sch->dev))
218 device_unregister(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200219 mutex_unlock(&sch->reg_mutex);
220}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200221EXPORT_SYMBOL_GPL(css_sch_device_unregister);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200222
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200223static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
224{
225 int i;
226 int mask;
227
228 memset(ssd, 0, sizeof(struct chsc_ssd_info));
229 ssd->path_mask = pmcw->pim;
230 for (i = 0; i < 8; i++) {
231 mask = 0x80 >> i;
232 if (pmcw->pim & mask) {
233 chp_id_init(&ssd->chpid[i]);
234 ssd->chpid[i].id = pmcw->chpid[i];
235 }
236 }
237}
238
239static void ssd_register_chpids(struct chsc_ssd_info *ssd)
240{
241 int i;
242 int mask;
243
244 for (i = 0; i < 8; i++) {
245 mask = 0x80 >> i;
246 if (ssd->path_mask & mask)
247 if (!chp_is_registered(ssd->chpid[i]))
248 chp_new(ssd->chpid[i]);
249 }
250}
251
252void css_update_ssd_info(struct subchannel *sch)
253{
254 int ret;
255
Sebastian Ott14556b32013-04-13 13:03:54 +0200256 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
257 if (ret)
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200258 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
Sebastian Ott14556b32013-04-13 13:03:54 +0200259
260 ssd_register_chpids(&sch->ssd_info);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200261}
262
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200263static ssize_t type_show(struct device *dev, struct device_attribute *attr,
264 char *buf)
265{
266 struct subchannel *sch = to_subchannel(dev);
267
268 return sprintf(buf, "%01x\n", sch->st);
269}
270
Joe Perchesc828a892017-12-19 10:15:08 -0800271static DEVICE_ATTR_RO(type);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200272
273static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
274 char *buf)
275{
276 struct subchannel *sch = to_subchannel(dev);
277
278 return sprintf(buf, "css:t%01X\n", sch->st);
279}
280
Joe Perchesc828a892017-12-19 10:15:08 -0800281static DEVICE_ATTR_RO(modalias);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200282
283static struct attribute *subch_attrs[] = {
284 &dev_attr_type.attr,
285 &dev_attr_modalias.attr,
286 NULL,
287};
288
289static struct attribute_group subch_attr_group = {
290 .attrs = subch_attrs,
291};
292
David Brownella4dbd672009-06-24 10:06:31 -0700293static const struct attribute_group *default_subch_attr_groups[] = {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200294 &subch_attr_group,
295 NULL,
296};
297
Sebastian Ott36f62372017-05-15 15:49:07 +0200298static ssize_t chpids_show(struct device *dev,
299 struct device_attribute *attr,
300 char *buf)
301{
302 struct subchannel *sch = to_subchannel(dev);
303 struct chsc_ssd_info *ssd = &sch->ssd_info;
304 ssize_t ret = 0;
305 int mask;
306 int chp;
307
308 for (chp = 0; chp < 8; chp++) {
309 mask = 0x80 >> chp;
310 if (ssd->path_mask & mask)
311 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
312 else
313 ret += sprintf(buf + ret, "00 ");
314 }
315 ret += sprintf(buf + ret, "\n");
316 return ret;
317}
Joe Perchesc828a892017-12-19 10:15:08 -0800318static DEVICE_ATTR_RO(chpids);
Sebastian Ott36f62372017-05-15 15:49:07 +0200319
320static ssize_t pimpampom_show(struct device *dev,
321 struct device_attribute *attr,
322 char *buf)
323{
324 struct subchannel *sch = to_subchannel(dev);
325 struct pmcw *pmcw = &sch->schib.pmcw;
326
327 return sprintf(buf, "%02x %02x %02x\n",
328 pmcw->pim, pmcw->pam, pmcw->pom);
329}
Joe Perchesc828a892017-12-19 10:15:08 -0800330static DEVICE_ATTR_RO(pimpampom);
Sebastian Ott36f62372017-05-15 15:49:07 +0200331
332static struct attribute *io_subchannel_type_attrs[] = {
333 &dev_attr_chpids.attr,
334 &dev_attr_pimpampom.attr,
335 NULL,
336};
337ATTRIBUTE_GROUPS(io_subchannel_type);
338
339static const struct device_type io_subchannel_type = {
340 .groups = io_subchannel_type_groups,
341};
342
Sebastian Ott14556b32013-04-13 13:03:54 +0200343int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344{
345 int ret;
346
347 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200348 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 sch->dev.bus = &css_bus_type;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200350 sch->dev.groups = default_subch_attr_groups;
Sebastian Ott36f62372017-05-15 15:49:07 +0200351
352 if (sch->st == SUBCHANNEL_TYPE_IO)
353 sch->dev.type = &io_subchannel_type;
354
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200355 /*
356 * We don't want to generate uevents for I/O subchannels that don't
357 * have a working ccw device behind them since they will be
358 * unregistered before they can be used anyway, so we delay the add
359 * uevent until after device recognition was successful.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200360 * Note that we suppress the uevent for all subchannel types;
361 * the subchannel driver can decide itself when it wants to inform
362 * userspace of its existence.
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200363 */
Ming Leif67f1292009-03-01 21:10:49 +0800364 dev_set_uevent_suppress(&sch->dev, 1);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200365 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200367 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100368 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200369 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
370 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100371 return ret;
372 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200373 if (!sch->driver) {
374 /*
375 * No driver matched. Generate the uevent now so that
376 * a fitting driver module may be loaded based on the
377 * modalias.
378 */
Ming Leif67f1292009-03-01 21:10:49 +0800379 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200380 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
381 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 return ret;
383}
384
Sebastian Ott4e5ebd52013-04-13 13:04:49 +0200385static int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 struct subchannel *sch;
Sebastian Ott4e5ebd52013-04-13 13:04:49 +0200388 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Sebastian Ott14556b32013-04-13 13:03:54 +0200390 sch = css_alloc_subchannel(schid);
391 if (IS_ERR(sch))
392 return PTR_ERR(sch);
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 ret = css_register_subchannel(sch);
Sebastian Ott863fc842013-04-13 13:01:50 +0200395 if (ret)
396 put_device(&sch->dev);
397
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 return ret;
399}
400
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700401static int
402check_subchannel(struct device * dev, void * data)
403{
404 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800405 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700406
407 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800408 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700409}
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800412get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 struct device *dev;
415
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700416 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200417 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700419 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100422/**
423 * css_sch_is_valid() - check if a subchannel is valid
424 * @schib: subchannel information block for the subchannel
425 */
426int css_sch_is_valid(struct schib *schib)
427{
428 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
429 return 0;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200430 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
431 return 0;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100432 return 1;
433}
434EXPORT_SYMBOL_GPL(css_sch_is_valid);
435
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200436static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
437{
438 struct schib schib;
439
440 if (!slow) {
441 /* Will be done on the slow path. */
442 return -EAGAIN;
443 }
Peter Oberparleiter62e65da2015-12-18 12:58:47 +0100444 if (stsch(schid, &schib)) {
Sebastian Ottcec85462012-10-15 12:24:56 +0200445 /* Subchannel is not provided. */
446 return -ENXIO;
447 }
448 if (!css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200449 /* Unusable - ignore. */
450 return 0;
451 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100452 CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
453 schid.sch_no);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200454
455 return css_probe_device(schid);
456}
457
Cornelia Huckc820de32008-07-14 09:58:45 +0200458static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
459{
460 int ret = 0;
461
462 if (sch->driver) {
463 if (sch->driver->sch_event)
464 ret = sch->driver->sch_event(sch, slow);
465 else
466 dev_dbg(&sch->dev,
467 "Got subchannel machine check but "
468 "no sch_event handler provided.\n");
469 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100470 if (ret != 0 && ret != -EAGAIN) {
471 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
472 sch->schid.ssid, sch->schid.sch_no, ret);
473 }
Cornelia Huckc820de32008-07-14 09:58:45 +0200474 return ret;
475}
476
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200477static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200478{
479 struct subchannel *sch;
480 int ret;
481
482 sch = get_subchannel_by_schid(schid);
483 if (sch) {
484 ret = css_evaluate_known_subchannel(sch, slow);
485 put_device(&sch->dev);
486 } else
487 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200488 if (ret == -EAGAIN)
489 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490}
491
Sebastian Ott817e5002011-12-01 13:32:19 +0100492/**
493 * css_sched_sch_todo - schedule a subchannel operation
494 * @sch: subchannel
495 * @todo: todo
496 *
497 * Schedule the operation identified by @todo to be performed on the slow path
498 * workqueue. Do nothing if another operation with higher priority is already
499 * scheduled. Needs to be called with subchannel lock held.
500 */
501void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
502{
503 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
504 sch->schid.ssid, sch->schid.sch_no, todo);
505 if (sch->todo >= todo)
506 return;
507 /* Get workqueue ref. */
508 if (!get_device(&sch->dev))
509 return;
510 sch->todo = todo;
511 if (!queue_work(cio_work_q, &sch->todo_work)) {
512 /* Already queued, release workqueue ref. */
513 put_device(&sch->dev);
514 }
515}
Sebastian Ott01c5e6d2012-08-28 16:42:37 +0200516EXPORT_SYMBOL_GPL(css_sched_sch_todo);
Sebastian Ott817e5002011-12-01 13:32:19 +0100517
518static void css_sch_todo(struct work_struct *work)
519{
520 struct subchannel *sch;
521 enum sch_todo todo;
522 int ret;
523
524 sch = container_of(work, struct subchannel, todo_work);
525 /* Find out todo. */
526 spin_lock_irq(sch->lock);
527 todo = sch->todo;
528 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
529 sch->schid.sch_no, todo);
530 sch->todo = SCH_TODO_NOTHING;
531 spin_unlock_irq(sch->lock);
532 /* Perform todo. */
533 switch (todo) {
534 case SCH_TODO_NOTHING:
535 break;
536 case SCH_TODO_EVAL:
537 ret = css_evaluate_known_subchannel(sch, 1);
538 if (ret == -EAGAIN) {
539 spin_lock_irq(sch->lock);
540 css_sched_sch_todo(sch, todo);
541 spin_unlock_irq(sch->lock);
542 }
543 break;
544 case SCH_TODO_UNREG:
545 css_sch_device_unregister(sch);
546 break;
547 }
548 /* Release workqueue ref. */
549 put_device(&sch->dev);
550}
551
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200552static struct idset *slow_subchannel_set;
553static spinlock_t slow_subchannel_lock;
Sebastian Ott25530552009-09-22 22:58:34 +0200554static wait_queue_head_t css_eval_wq;
555static atomic_t css_eval_scheduled;
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200556
557static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200559 spin_lock_init(&slow_subchannel_lock);
Sebastian Ott25530552009-09-22 22:58:34 +0200560 atomic_set(&css_eval_scheduled, 0);
561 init_waitqueue_head(&css_eval_wq);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200562 slow_subchannel_set = idset_sch_new();
563 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200564 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200565 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200567 return 0;
568}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100570static int slow_eval_known_fn(struct subchannel *sch, void *data)
571{
572 int eval;
573 int rc;
574
575 spin_lock_irq(&slow_subchannel_lock);
576 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
577 idset_sch_del(slow_subchannel_set, sch->schid);
578 spin_unlock_irq(&slow_subchannel_lock);
579 if (eval) {
580 rc = css_evaluate_known_subchannel(sch, 1);
581 if (rc == -EAGAIN)
582 css_schedule_eval(sch->schid);
583 }
584 return 0;
585}
586
587static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
588{
589 int eval;
590 int rc = 0;
591
592 spin_lock_irq(&slow_subchannel_lock);
593 eval = idset_sch_contains(slow_subchannel_set, schid);
594 idset_sch_del(slow_subchannel_set, schid);
595 spin_unlock_irq(&slow_subchannel_lock);
596 if (eval) {
597 rc = css_evaluate_new_subchannel(schid, 1);
598 switch (rc) {
599 case -EAGAIN:
600 css_schedule_eval(schid);
601 rc = 0;
602 break;
603 case -ENXIO:
604 case -ENOMEM:
605 case -EIO:
606 /* These should abort looping */
Sebastian Otteb072a72013-08-29 20:31:06 +0200607 spin_lock_irq(&slow_subchannel_lock);
Sebastian Ottcec85462012-10-15 12:24:56 +0200608 idset_sch_del_subseq(slow_subchannel_set, schid);
Sebastian Otteb072a72013-08-29 20:31:06 +0200609 spin_unlock_irq(&slow_subchannel_lock);
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100610 break;
611 default:
612 rc = 0;
613 }
Peter Oberparleiterb207f5a2013-11-26 14:57:13 +0100614 /* Allow scheduling here since the containing loop might
615 * take a while. */
616 cond_resched();
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100617 }
618 return rc;
619}
620
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200621static void css_slow_path_func(struct work_struct *unused)
622{
Sebastian Ott25530552009-09-22 22:58:34 +0200623 unsigned long flags;
624
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200625 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100626 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
627 NULL);
Sebastian Ott25530552009-09-22 22:58:34 +0200628 spin_lock_irqsave(&slow_subchannel_lock, flags);
629 if (idset_is_empty(slow_subchannel_set)) {
630 atomic_set(&css_eval_scheduled, 0);
631 wake_up(&css_eval_wq);
632 }
633 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634}
635
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100636static DECLARE_DELAYED_WORK(slow_path_work, css_slow_path_func);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100637struct workqueue_struct *cio_work_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200639void css_schedule_eval(struct subchannel_id schid)
640{
641 unsigned long flags;
642
643 spin_lock_irqsave(&slow_subchannel_lock, flags);
644 idset_sch_add(slow_subchannel_set, schid);
Sebastian Ott25530552009-09-22 22:58:34 +0200645 atomic_set(&css_eval_scheduled, 1);
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100646 queue_delayed_work(cio_work_q, &slow_path_work, 0);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200647 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
648}
649
650void css_schedule_eval_all(void)
651{
652 unsigned long flags;
653
654 spin_lock_irqsave(&slow_subchannel_lock, flags);
655 idset_fill(slow_subchannel_set);
Sebastian Ott25530552009-09-22 22:58:34 +0200656 atomic_set(&css_eval_scheduled, 1);
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100657 queue_delayed_work(cio_work_q, &slow_path_work, 0);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200658 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
659}
660
Sebastian Ott703e5c92009-09-22 22:58:38 +0200661static int __unset_registered(struct device *dev, void *data)
662{
663 struct idset *set = data;
664 struct subchannel *sch = to_subchannel(dev);
665
666 idset_sch_del(set, sch->schid);
667 return 0;
668}
669
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100670void css_schedule_eval_all_unreg(unsigned long delay)
Sebastian Ott703e5c92009-09-22 22:58:38 +0200671{
672 unsigned long flags;
673 struct idset *unreg_set;
674
675 /* Find unregistered subchannels. */
676 unreg_set = idset_sch_new();
677 if (!unreg_set) {
678 /* Fallback. */
679 css_schedule_eval_all();
680 return;
681 }
682 idset_fill(unreg_set);
683 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
684 /* Apply to slow_subchannel_set. */
685 spin_lock_irqsave(&slow_subchannel_lock, flags);
686 idset_add_set(slow_subchannel_set, unreg_set);
687 atomic_set(&css_eval_scheduled, 1);
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100688 queue_delayed_work(cio_work_q, &slow_path_work, delay);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200689 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
690 idset_free(unreg_set);
691}
692
Cornelia Huck22806dc2008-04-17 07:45:59 +0200693void css_wait_for_slow_path(void)
694{
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100695 flush_workqueue(cio_work_q);
Cornelia Huck22806dc2008-04-17 07:45:59 +0200696}
697
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200698/* Schedule reprobing of all unregistered subchannels. */
699void css_schedule_reprobe(void)
700{
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100701 /* Schedule with a delay to allow merging of subsequent calls. */
702 css_schedule_eval_all_unreg(1 * HZ);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200703}
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200704EXPORT_SYMBOL_GPL(css_schedule_reprobe);
705
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 * Called from the machine check handler for subchannel report words.
708 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200709static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800711 struct subchannel_id mchk_schid;
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100712 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
Cornelia Huckc1156182008-07-14 09:58:46 +0200714 if (overflow) {
715 css_schedule_eval_all();
716 return;
717 }
718 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
719 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
720 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
721 crw0->erc, crw0->rsid);
722 if (crw1)
723 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
724 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
725 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
726 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800727 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200728 mchk_schid.sch_no = crw0->rsid;
729 if (crw1)
Sebastian Ott8d7bfb42010-12-01 10:08:02 +0100730 mchk_schid.ssid = (crw1->rsid >> 4) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800731
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100732 if (crw0->erc == CRW_ERC_PMOD) {
733 sch = get_subchannel_by_schid(mchk_schid);
734 if (sch) {
735 css_update_ssd_info(sch);
736 put_device(&sch->dev);
737 }
738 }
Cornelia Huckc1156182008-07-14 09:58:46 +0200739 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 * Since we are always presented with IPI in the CRW, we have to
741 * use stsch() to find out if the subchannel in question has come
742 * or gone.
743 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200744 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745}
746
747static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800748css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749{
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200750 struct cpuid cpu_id;
751
Cornelia Huck75784c02008-07-14 09:58:57 +0200752 if (css_general_characteristics.mcss) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800753 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
Sebastian Otte2e0de92016-06-17 19:45:23 +0200754 css->global_pgid.pgid_high.ext_cssid.cssid =
755 (css->cssid < 0) ? 0 : css->cssid;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800756 } else {
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100757 css->global_pgid.pgid_high.cpu_addr = stap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 }
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200759 get_cpu_id(&cpu_id);
760 css->global_pgid.cpu_id = cpu_id.ident;
761 css->global_pgid.cpu_model = cpu_id.machine;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800762 css->global_pgid.tod_high = tod_high;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800763}
764
Sebastian Ott15a20442016-10-25 14:05:08 +0200765static void channel_subsystem_release(struct device *dev)
Cornelia Huck3b793062006-01-06 00:19:26 -0800766{
Sebastian Ott15a20442016-10-25 14:05:08 +0200767 struct channel_subsystem *css = to_css(dev);
Cornelia Huck3b793062006-01-06 00:19:26 -0800768
Cornelia Huck495a5b42006-03-24 03:15:14 -0800769 mutex_destroy(&css->mutex);
Cornelia Huck3b793062006-01-06 00:19:26 -0800770 kfree(css);
771}
772
Sebastian Ott64dfdd42016-10-11 18:21:36 +0200773static ssize_t real_cssid_show(struct device *dev, struct device_attribute *a,
774 char *buf)
775{
776 struct channel_subsystem *css = to_css(dev);
777
778 if (css->cssid < 0)
779 return -EINVAL;
780
781 return sprintf(buf, "%x\n", css->cssid);
782}
783static DEVICE_ATTR_RO(real_cssid);
784
Sebastian Ott6c7012682016-10-11 16:37:43 +0200785static ssize_t cm_enable_show(struct device *dev, struct device_attribute *a,
786 char *buf)
Cornelia Huck495a5b42006-03-24 03:15:14 -0800787{
788 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200789 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800790
Michael Ernst8284fb192008-04-17 07:46:01 +0200791 mutex_lock(&css->mutex);
792 ret = sprintf(buf, "%x\n", css->cm_enabled);
793 mutex_unlock(&css->mutex);
794 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800795}
796
Sebastian Ott6c7012682016-10-11 16:37:43 +0200797static ssize_t cm_enable_store(struct device *dev, struct device_attribute *a,
798 const char *buf, size_t count)
Cornelia Huck495a5b42006-03-24 03:15:14 -0800799{
800 struct channel_subsystem *css = to_css(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200801 unsigned long val;
Sebastian Ott6c7012682016-10-11 16:37:43 +0200802 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800803
Jingoo Han01787222013-07-22 10:18:15 +0900804 ret = kstrtoul(buf, 16, &val);
Cornelia Huck2f972202008-04-30 13:38:33 +0200805 if (ret)
806 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200807 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200808 switch (val) {
809 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800810 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
811 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200812 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800813 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
814 break;
815 default:
816 ret = -EINVAL;
817 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200818 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800819 return ret < 0 ? ret : count;
820}
Sebastian Ott6c7012682016-10-11 16:37:43 +0200821static DEVICE_ATTR_RW(cm_enable);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800822
Sebastian Ott6c7012682016-10-11 16:37:43 +0200823static umode_t cm_enable_mode(struct kobject *kobj, struct attribute *attr,
824 int index)
825{
826 return css_chsc_characteristics.secm ? attr->mode : 0;
827}
828
Sebastian Ott64dfdd42016-10-11 18:21:36 +0200829static struct attribute *cssdev_attrs[] = {
830 &dev_attr_real_cssid.attr,
831 NULL,
832};
833
834static struct attribute_group cssdev_attr_group = {
835 .attrs = cssdev_attrs,
836};
837
Sebastian Ott6c7012682016-10-11 16:37:43 +0200838static struct attribute *cssdev_cm_attrs[] = {
839 &dev_attr_cm_enable.attr,
840 NULL,
841};
842
843static struct attribute_group cssdev_cm_attr_group = {
844 .attrs = cssdev_cm_attrs,
845 .is_visible = cm_enable_mode,
846};
847
848static const struct attribute_group *cssdev_attr_groups[] = {
Sebastian Ott64dfdd42016-10-11 18:21:36 +0200849 &cssdev_attr_group,
Sebastian Ott6c7012682016-10-11 16:37:43 +0200850 &cssdev_cm_attr_group,
851 NULL,
852};
Cornelia Huck495a5b42006-03-24 03:15:14 -0800853
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100854static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800855{
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200856 struct channel_subsystem *css;
Sebastian Ott15a20442016-10-25 14:05:08 +0200857 int ret;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800858
Sebastian Ott15a20442016-10-25 14:05:08 +0200859 css = kzalloc(sizeof(*css), GFP_KERNEL);
860 if (!css)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100861 return -ENOMEM;
Sebastian Ott15a20442016-10-25 14:05:08 +0200862
863 channel_subsystems[nr] = css;
864 dev_set_name(&css->device, "css%x", nr);
865 css->device.groups = cssdev_attr_groups;
866 css->device.release = channel_subsystem_release;
867
868 mutex_init(&css->mutex);
Sebastian Ott15a20442016-10-25 14:05:08 +0200869 css->cssid = chsc_get_cssid(nr);
870 css_generate_pgid(css, (u32) (get_tod_clock() >> 32));
871
872 ret = device_register(&css->device);
873 if (ret) {
874 put_device(&css->device);
875 goto out_err;
876 }
877
878 css->pseudo_subchannel = kzalloc(sizeof(*css->pseudo_subchannel),
879 GFP_KERNEL);
880 if (!css->pseudo_subchannel) {
881 device_unregister(&css->device);
882 ret = -ENOMEM;
883 goto out_err;
884 }
885
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200886 css->pseudo_subchannel->dev.parent = &css->device;
887 css->pseudo_subchannel->dev.release = css_subchannel_release;
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100888 mutex_init(&css->pseudo_subchannel->reg_mutex);
Sebastian Otte5dcf002013-04-13 13:08:01 +0200889 ret = css_sch_create_locks(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100890 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200891 kfree(css->pseudo_subchannel);
Sebastian Ott15a20442016-10-25 14:05:08 +0200892 device_unregister(&css->device);
893 goto out_err;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100894 }
Sebastian Otte2e0de92016-06-17 19:45:23 +0200895
Sebastian Ott15a20442016-10-25 14:05:08 +0200896 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
897 ret = device_register(&css->pseudo_subchannel->dev);
898 if (ret) {
899 put_device(&css->pseudo_subchannel->dev);
900 device_unregister(&css->device);
901 goto out_err;
902 }
903
904 return ret;
905out_err:
906 channel_subsystems[nr] = NULL;
907 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908}
909
Cornelia Hucka55360d2007-10-12 16:11:20 +0200910static int css_reboot_event(struct notifier_block *this,
911 unsigned long event,
912 void *ptr)
913{
Sebastian Ott98cc43a2016-11-08 14:28:03 +0100914 struct channel_subsystem *css;
915 int ret;
Cornelia Hucka55360d2007-10-12 16:11:20 +0200916
917 ret = NOTIFY_DONE;
Sebastian Ott98cc43a2016-11-08 14:28:03 +0100918 for_each_css(css) {
Michael Ernst8284fb192008-04-17 07:46:01 +0200919 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200920 if (css->cm_enabled)
921 if (chsc_secm(css, 0))
922 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200923 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200924 }
925
926 return ret;
927}
928
929static struct notifier_block css_reboot_notifier = {
930 .notifier_call = css_reboot_event,
931};
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200934 * Since the css devices are neither on a bus nor have a class
935 * nor have a special device type, we cannot stop/restart channel
936 * path measurements via the normal suspend/resume callbacks, but have
937 * to use notifiers.
938 */
939static int css_power_event(struct notifier_block *this, unsigned long event,
940 void *ptr)
941{
Sebastian Ott98cc43a2016-11-08 14:28:03 +0100942 struct channel_subsystem *css;
943 int ret;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200944
945 switch (event) {
946 case PM_HIBERNATION_PREPARE:
947 case PM_SUSPEND_PREPARE:
948 ret = NOTIFY_DONE;
Sebastian Ott98cc43a2016-11-08 14:28:03 +0100949 for_each_css(css) {
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200950 mutex_lock(&css->mutex);
951 if (!css->cm_enabled) {
952 mutex_unlock(&css->mutex);
953 continue;
954 }
Akinobu Mitaf0c077a2011-07-08 20:53:36 +0200955 ret = __chsc_do_secm(css, 0);
956 ret = notifier_from_errno(ret);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200957 mutex_unlock(&css->mutex);
958 }
959 break;
960 case PM_POST_HIBERNATION:
961 case PM_POST_SUSPEND:
962 ret = NOTIFY_DONE;
Sebastian Ott98cc43a2016-11-08 14:28:03 +0100963 for_each_css(css) {
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200964 mutex_lock(&css->mutex);
965 if (!css->cm_enabled) {
966 mutex_unlock(&css->mutex);
967 continue;
968 }
Akinobu Mitaf0c077a2011-07-08 20:53:36 +0200969 ret = __chsc_do_secm(css, 1);
970 ret = notifier_from_errno(ret);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200971 mutex_unlock(&css->mutex);
972 }
973 /* search for subchannels, which appeared during hibernation */
974 css_schedule_reprobe();
975 break;
976 default:
977 ret = NOTIFY_DONE;
978 }
979 return ret;
980
981}
982static struct notifier_block css_power_notifier = {
983 .notifier_call = css_power_event,
984};
985
986/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 * Now that the driver core is running, we can setup our channel subsystem.
Sebastian Ott14556b32013-04-13 13:03:54 +0200988 * The struct subchannel's are created during probing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989 */
Sebastian Ott2f176442009-09-22 22:58:33 +0200990static int __init css_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800992 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Sebastian Ott34aec072010-10-25 16:10:28 +0200994 ret = chsc_init();
995 if (ret)
996 return ret;
997
Sebastian Ott34196f82010-10-25 16:10:29 +0200998 chsc_determine_css_characteristics();
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200999 /* Try to enable MSS. */
1000 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott818c2722010-04-22 17:17:03 +02001001 if (ret)
Sebastian Ottb0a285d2009-09-22 22:58:37 +02001002 max_ssid = 0;
Sebastian Ott818c2722010-04-22 17:17:03 +02001003 else /* Success. */
1004 max_ssid = __MAX_SSID;
Sebastian Ottb0a285d2009-09-22 22:58:37 +02001005
Cornelia Huck4434a382007-07-27 12:29:21 +02001006 ret = slow_subchannel_init();
1007 if (ret)
1008 goto out;
1009
Heiko Carstensf5daba12009-03-26 15:24:01 +01001010 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
Cornelia Huckc1156182008-07-14 09:58:46 +02001011 if (ret)
1012 goto out;
1013
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 if ((ret = bus_register(&css_bus_type)))
1015 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Cornelia Hucka28c6942006-01-06 00:19:23 -08001017 /* Setup css structure. */
Sebastian Ott98cc43a2016-11-08 14:28:03 +01001018 for (i = 0; i <= MAX_CSS_IDX; i++) {
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001019 ret = setup_css(i);
Sebastian Ott15a20442016-10-25 14:05:08 +02001020 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +02001021 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -08001022 }
Cornelia Hucka55360d2007-10-12 16:11:20 +02001023 ret = register_reboot_notifier(&css_reboot_notifier);
1024 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +02001025 goto out_unregister;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001026 ret = register_pm_notifier(&css_power_notifier);
1027 if (ret) {
1028 unregister_reboot_notifier(&css_reboot_notifier);
1029 goto out_unregister;
1030 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 css_init_done = 1;
1032
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001033 /* Enable default isc for I/O subchannels. */
Cornelia Huck6ef556c2008-07-14 09:59:01 +02001034 isc_register(IO_SCH_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 return 0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001037out_unregister:
Sebastian Ott15a20442016-10-25 14:05:08 +02001038 while (i-- > 0) {
1039 struct channel_subsystem *css = channel_subsystems[i];
Cornelia Huck7c9f4e32007-10-12 16:11:13 +02001040 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +02001041 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -08001042 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 bus_unregister(&css_bus_type);
1044out:
Sebastian Ott34aec072010-10-25 16:10:28 +02001045 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c82009-09-22 22:58:36 +02001046 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +02001047 chsc_init_cleanup();
Michael Ernste6d5a422008-12-25 13:39:36 +01001048 pr_alert("The CSS device driver initialization failed with "
1049 "errno=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 return ret;
1051}
1052
Sebastian Ott2f176442009-09-22 22:58:33 +02001053static void __init css_bus_cleanup(void)
1054{
1055 struct channel_subsystem *css;
Sebastian Ott2f176442009-09-22 22:58:33 +02001056
Sebastian Ott98cc43a2016-11-08 14:28:03 +01001057 for_each_css(css) {
Sebastian Ott2f176442009-09-22 22:58:33 +02001058 device_unregister(&css->pseudo_subchannel->dev);
Sebastian Ott2f176442009-09-22 22:58:33 +02001059 device_unregister(&css->device);
1060 }
1061 bus_unregister(&css_bus_type);
Sebastian Ott34aec072010-10-25 16:10:28 +02001062 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c82009-09-22 22:58:36 +02001063 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +02001064 chsc_init_cleanup();
Sebastian Ott2f176442009-09-22 22:58:33 +02001065 isc_unregister(IO_SCH_ISC);
1066}
1067
1068static int __init channel_subsystem_init(void)
1069{
1070 int ret;
1071
1072 ret = css_bus_init();
1073 if (ret)
1074 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001075 cio_work_q = create_singlethread_workqueue("cio");
1076 if (!cio_work_q) {
1077 ret = -ENOMEM;
1078 goto out_bus;
1079 }
Sebastian Ott2f176442009-09-22 22:58:33 +02001080 ret = io_subchannel_init();
1081 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001082 goto out_wq;
Sebastian Ott2f176442009-09-22 22:58:33 +02001083
1084 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001085out_wq:
1086 destroy_workqueue(cio_work_q);
1087out_bus:
1088 css_bus_cleanup();
1089 return ret;
Sebastian Ott2f176442009-09-22 22:58:33 +02001090}
1091subsys_initcall(channel_subsystem_init);
1092
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001093static int css_settle(struct device_driver *drv, void *unused)
1094{
1095 struct css_driver *cssdrv = to_cssdriver(drv);
1096
1097 if (cssdrv->settle)
Sebastian Ottb4c70722010-02-26 22:37:27 +01001098 return cssdrv->settle();
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001099 return 0;
1100}
1101
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001102int css_complete_work(void)
Sebastian Ott879acca52010-02-26 22:37:25 +01001103{
1104 int ret;
1105
1106 /* Wait for the evaluation of subchannels to finish. */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001107 ret = wait_event_interruptible(css_eval_wq,
1108 atomic_read(&css_eval_scheduled) == 0);
1109 if (ret)
1110 return -EINTR;
Sebastian Ott879acca52010-02-26 22:37:25 +01001111 flush_workqueue(cio_work_q);
1112 /* Wait for the subchannel type specific initialization to finish */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001113 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
Sebastian Ott879acca52010-02-26 22:37:25 +01001114}
1115
1116
Sebastian Ott2f176442009-09-22 22:58:33 +02001117/*
1118 * Wait for the initialization of devices to finish, to make sure we are
1119 * done with our setup if the search for the root device starts.
1120 */
1121static int __init channel_subsystem_init_sync(void)
1122{
Sebastian Ott14556b32013-04-13 13:03:54 +02001123 /* Register subchannels which are already in use. */
1124 cio_register_early_subchannels();
Sebastian Ott703e5c92009-09-22 22:58:38 +02001125 /* Start initial subchannel evaluation. */
1126 css_schedule_eval_all();
Sebastian Ott879acca52010-02-26 22:37:25 +01001127 css_complete_work();
1128 return 0;
Sebastian Ott2f176442009-09-22 22:58:33 +02001129}
1130subsys_initcall_sync(channel_subsystem_init_sync);
1131
Sebastian Ott889ee952010-04-22 17:17:04 +02001132void channel_subsystem_reinit(void)
1133{
Sebastian Ott62da1772010-10-25 16:10:32 +02001134 struct channel_path *chp;
1135 struct chp_id chpid;
1136
Sebastian Ott889ee952010-04-22 17:17:04 +02001137 chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott62da1772010-10-25 16:10:32 +02001138 chp_id_for_each(&chpid) {
1139 chp = chpid_to_chp(chpid);
Peter Oberparleitercce0eac2013-03-11 12:58:18 +01001140 if (chp)
1141 chp_update_desc(chp);
Sebastian Ott62da1772010-10-25 16:10:32 +02001142 }
Sebastian Ottab97d212015-09-09 10:29:59 +02001143 cmf_reactivate();
Sebastian Ott889ee952010-04-22 17:17:04 +02001144}
1145
Sebastian Ott879acca52010-02-26 22:37:25 +01001146#ifdef CONFIG_PROC_FS
1147static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1148 size_t count, loff_t *ppos)
1149{
Sebastian Ottb4c70722010-02-26 22:37:27 +01001150 int ret;
1151
Sebastian Ott879acca52010-02-26 22:37:25 +01001152 /* Handle pending CRW's. */
1153 crw_wait_for_channel_report();
Sebastian Ottb4c70722010-02-26 22:37:27 +01001154 ret = css_complete_work();
1155
1156 return ret ? ret : count;
Sebastian Ott879acca52010-02-26 22:37:25 +01001157}
1158
1159static const struct file_operations cio_settle_proc_fops = {
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +02001160 .open = nonseekable_open,
Sebastian Ott879acca52010-02-26 22:37:25 +01001161 .write = cio_settle_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001162 .llseek = no_llseek,
Sebastian Ott879acca52010-02-26 22:37:25 +01001163};
1164
1165static int __init cio_settle_init(void)
1166{
1167 struct proc_dir_entry *entry;
1168
1169 entry = proc_create("cio_settle", S_IWUSR, NULL,
1170 &cio_settle_proc_fops);
1171 if (!entry)
1172 return -ENOMEM;
1173 return 0;
1174}
1175device_initcall(cio_settle_init);
1176#endif /*CONFIG_PROC_FS*/
1177
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001178int sch_is_pseudo_sch(struct subchannel *sch)
1179{
1180 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1181}
1182
Cornelia Huckf08adc02008-07-14 09:59:03 +02001183static int css_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184{
Cornelia Huck084325d2008-01-26 14:10:38 +01001185 struct subchannel *sch = to_subchannel(dev);
1186 struct css_driver *driver = to_cssdriver(drv);
Cornelia Huckf08adc02008-07-14 09:59:03 +02001187 struct css_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188
Cornelia Huckf08adc02008-07-14 09:59:03 +02001189 for (id = driver->subchannel_type; id->match_flags; id++) {
1190 if (sch->st == id->type)
1191 return 1;
1192 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 return 0;
1195}
1196
Cornelia Huck98c13c22008-01-26 14:10:40 +01001197static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001198{
1199 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +01001200 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001201
1202 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +01001203 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001204 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1205 if (ret)
1206 sch->driver = NULL;
1207 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001208}
1209
Cornelia Huck98c13c22008-01-26 14:10:40 +01001210static int css_remove(struct device *dev)
1211{
1212 struct subchannel *sch;
1213 int ret;
1214
1215 sch = to_subchannel(dev);
1216 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1217 sch->driver = NULL;
1218 return ret;
1219}
1220
1221static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001222{
1223 struct subchannel *sch;
1224
1225 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001226 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001227 sch->driver->shutdown(sch);
1228}
1229
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001230static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1231{
1232 struct subchannel *sch = to_subchannel(dev);
1233 int ret;
1234
1235 ret = add_uevent_var(env, "ST=%01X", sch->st);
1236 if (ret)
1237 return ret;
1238 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1239 return ret;
1240}
1241
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001242static int css_pm_prepare(struct device *dev)
1243{
1244 struct subchannel *sch = to_subchannel(dev);
1245 struct css_driver *drv;
1246
1247 if (mutex_is_locked(&sch->reg_mutex))
1248 return -EAGAIN;
1249 if (!sch->dev.driver)
1250 return 0;
1251 drv = to_cssdriver(sch->dev.driver);
1252 /* Notify drivers that they may not register children. */
1253 return drv->prepare ? drv->prepare(sch) : 0;
1254}
1255
1256static void css_pm_complete(struct device *dev)
1257{
1258 struct subchannel *sch = to_subchannel(dev);
1259 struct css_driver *drv;
1260
1261 if (!sch->dev.driver)
1262 return;
1263 drv = to_cssdriver(sch->dev.driver);
1264 if (drv->complete)
1265 drv->complete(sch);
1266}
1267
1268static int css_pm_freeze(struct device *dev)
1269{
1270 struct subchannel *sch = to_subchannel(dev);
1271 struct css_driver *drv;
1272
1273 if (!sch->dev.driver)
1274 return 0;
1275 drv = to_cssdriver(sch->dev.driver);
1276 return drv->freeze ? drv->freeze(sch) : 0;
1277}
1278
1279static int css_pm_thaw(struct device *dev)
1280{
1281 struct subchannel *sch = to_subchannel(dev);
1282 struct css_driver *drv;
1283
1284 if (!sch->dev.driver)
1285 return 0;
1286 drv = to_cssdriver(sch->dev.driver);
1287 return drv->thaw ? drv->thaw(sch) : 0;
1288}
1289
1290static int css_pm_restore(struct device *dev)
1291{
1292 struct subchannel *sch = to_subchannel(dev);
1293 struct css_driver *drv;
1294
Sebastian Otteb4f5d92010-10-25 16:10:33 +02001295 css_update_ssd_info(sch);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001296 if (!sch->dev.driver)
1297 return 0;
1298 drv = to_cssdriver(sch->dev.driver);
1299 return drv->restore ? drv->restore(sch) : 0;
1300}
1301
Alexey Dobriyan47145212009-12-14 18:00:08 -08001302static const struct dev_pm_ops css_pm_ops = {
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001303 .prepare = css_pm_prepare,
1304 .complete = css_pm_complete,
1305 .freeze = css_pm_freeze,
1306 .thaw = css_pm_thaw,
1307 .restore = css_pm_restore,
1308};
1309
Sebastian Ott3041b6a2011-03-15 17:08:31 +01001310static struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001311 .name = "css",
1312 .match = css_bus_match,
1313 .probe = css_probe,
1314 .remove = css_remove,
1315 .shutdown = css_shutdown,
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001316 .uevent = css_uevent,
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001317 .pm = &css_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318};
1319
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001320/**
1321 * css_driver_register - register a css driver
1322 * @cdrv: css driver to register
1323 *
1324 * This is mainly a wrapper around driver_register that sets name
1325 * and bus_type in the embedded struct device_driver correctly.
1326 */
1327int css_driver_register(struct css_driver *cdrv)
1328{
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001329 cdrv->drv.bus = &css_bus_type;
1330 return driver_register(&cdrv->drv);
1331}
1332EXPORT_SYMBOL_GPL(css_driver_register);
1333
1334/**
1335 * css_driver_unregister - unregister a css driver
1336 * @cdrv: css driver to unregister
1337 *
1338 * This is a wrapper around driver_unregister.
1339 */
1340void css_driver_unregister(struct css_driver *cdrv)
1341{
1342 driver_unregister(&cdrv->drv);
1343}
1344EXPORT_SYMBOL_GPL(css_driver_unregister);