blob: c1f9cc728c2dbf85d18342ed093b024635bf36aa [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02002 * driver for channel subsystem
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Sebastian Ott34aec072010-10-25 16:10:28 +02004 * Copyright IBM Corp. 2002, 2010
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02005 *
6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Michael Ernste6d5a422008-12-25 13:39:36 +01009
10#define KMSG_COMPONENT "cio"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/device.h>
16#include <linux/slab.h>
17#include <linux/errno.h>
18#include <linux/list.h>
Cornelia Hucka55360d2007-10-12 16:11:20 +020019#include <linux/reboot.h>
Sebastian Ottdcbd16d2009-06-16 10:30:22 +020020#include <linux/suspend.h>
Sebastian Ott879acca52010-02-26 22:37:25 +010021#include <linux/proc_fs.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020022#include <asm/isc.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010023#include <asm/crw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "css.h"
26#include "cio.h"
27#include "cio_debug.h"
28#include "ioasm.h"
29#include "chsc.h"
Peter Oberparleiter40154b82006-06-29 14:57:03 +020030#include "device.h"
Peter Oberparleiter83b33702007-04-27 16:01:34 +020031#include "idset.h"
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +020032#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034int css_init_done = 0;
Sebastian Ottb0a285d2009-09-22 22:58:37 +020035int max_ssid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Cornelia Huck7c9f4e32007-10-12 16:11:13 +020037struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
Sebastian Ott3041b6a2011-03-15 17:08:31 +010038static struct bus_type css_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Heiko Carstens4d284ca2007-02-05 21:18:53 +010040int
Cornelia Huckf97a56f2006-01-06 00:19:22 -080041for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
42{
43 struct subchannel_id schid;
44 int ret;
45
46 init_subchannel_id(&schid);
47 ret = -ENODEV;
48 do {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080049 do {
50 ret = fn(schid, data);
51 if (ret)
52 break;
53 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
54 schid.sch_no = 0;
55 } while (schid.ssid++ < max_ssid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080056 return ret;
57}
58
Peter Oberparleitere82a1562008-01-26 14:10:48 +010059struct cb_data {
60 void *data;
61 struct idset *set;
62 int (*fn_known_sch)(struct subchannel *, void *);
63 int (*fn_unknown_sch)(struct subchannel_id, void *);
64};
65
66static int call_fn_known_sch(struct device *dev, void *data)
67{
68 struct subchannel *sch = to_subchannel(dev);
69 struct cb_data *cb = data;
70 int rc = 0;
71
72 idset_sch_del(cb->set, sch->schid);
73 if (cb->fn_known_sch)
74 rc = cb->fn_known_sch(sch, cb->data);
75 return rc;
76}
77
78static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
79{
80 struct cb_data *cb = data;
81 int rc = 0;
82
83 if (idset_sch_contains(cb->set, schid))
84 rc = cb->fn_unknown_sch(schid, cb->data);
85 return rc;
86}
87
Sebastian Ott90ac24a2009-03-26 15:24:11 +010088static int call_fn_all_sch(struct subchannel_id schid, void *data)
89{
90 struct cb_data *cb = data;
91 struct subchannel *sch;
92 int rc = 0;
93
94 sch = get_subchannel_by_schid(schid);
95 if (sch) {
96 if (cb->fn_known_sch)
97 rc = cb->fn_known_sch(sch, cb->data);
98 put_device(&sch->dev);
99 } else {
100 if (cb->fn_unknown_sch)
101 rc = cb->fn_unknown_sch(schid, cb->data);
102 }
103
104 return rc;
105}
106
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100107int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
108 int (*fn_unknown)(struct subchannel_id,
109 void *), void *data)
110{
111 struct cb_data cb;
112 int rc;
113
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100114 cb.data = data;
115 cb.fn_known_sch = fn_known;
116 cb.fn_unknown_sch = fn_unknown;
Sebastian Ott90ac24a2009-03-26 15:24:11 +0100117
118 cb.set = idset_sch_new();
119 if (!cb.set)
120 /* fall back to brute force scanning in case of oom */
121 return for_each_subchannel(call_fn_all_sch, &cb);
122
123 idset_fill(cb.set);
124
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100125 /* Process registered subchannels. */
126 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
127 if (rc)
128 goto out;
129 /* Process unregistered subchannels. */
130 if (fn_unknown)
131 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
132out:
133 idset_free(cb.set);
134
135 return rc;
136}
137
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100138static void css_sch_todo(struct work_struct *work);
139
Sebastian Otte5dcf002013-04-13 13:08:01 +0200140static int css_sch_create_locks(struct subchannel *sch)
141{
142 sch->lock = kmalloc(sizeof(*sch->lock), GFP_KERNEL);
143 if (!sch->lock)
144 return -ENOMEM;
145
146 spin_lock_init(sch->lock);
147 mutex_init(&sch->reg_mutex);
148
149 return 0;
150}
151
Sebastian Ottc135ad12013-04-13 12:58:55 +0200152static void css_subchannel_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Sebastian Ott863fc842013-04-13 13:01:50 +0200154 struct subchannel *sch = to_subchannel(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Sebastian Ott863fc842013-04-13 13:01:50 +0200156 sch->config.intparm = 0;
157 cio_commit_config(sch);
158 kfree(sch->lock);
159 kfree(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
Sebastian Ott863fc842013-04-13 13:01:50 +0200162struct subchannel *css_alloc_subchannel(struct subchannel_id schid)
Sebastian Ottc135ad12013-04-13 12:58:55 +0200163{
164 struct subchannel *sch;
165 int ret;
166
Sebastian Otte5dcf002013-04-13 13:08:01 +0200167 sch = kzalloc(sizeof(*sch), GFP_KERNEL | GFP_DMA);
168 if (!sch)
Sebastian Ottc135ad12013-04-13 12:58:55 +0200169 return ERR_PTR(-ENOMEM);
Sebastian Otte5dcf002013-04-13 13:08:01 +0200170
171 ret = cio_validate_subchannel(sch, schid);
172 if (ret < 0)
173 goto err;
174
175 ret = css_sch_create_locks(sch);
176 if (ret)
177 goto err;
178
Sebastian Ottc135ad12013-04-13 12:58:55 +0200179 INIT_WORK(&sch->todo_work, css_sch_todo);
180 sch->dev.release = &css_subchannel_release;
181 device_initialize(&sch->dev);
182 return sch;
Sebastian Otte5dcf002013-04-13 13:08:01 +0200183
184err:
185 kfree(sch);
186 return ERR_PTR(ret);
Sebastian Ottc135ad12013-04-13 12:58:55 +0200187}
188
Cornelia Huck07c6a332007-07-27 12:29:10 +0200189static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200190{
191 int ret;
192
193 mutex_lock(&sch->reg_mutex);
Sebastian Ott6ee4fec2009-09-11 10:28:25 +0200194 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
195 sch->schid.sch_no);
Sebastian Ottc135ad12013-04-13 12:58:55 +0200196 ret = device_add(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200197 mutex_unlock(&sch->reg_mutex);
198 return ret;
199}
200
Cornelia Huck44a1c192008-07-14 09:58:47 +0200201/**
202 * css_sch_device_unregister - unregister a subchannel
203 * @sch: subchannel to be unregistered
204 */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200205void css_sch_device_unregister(struct subchannel *sch)
206{
207 mutex_lock(&sch->reg_mutex);
Sebastian Ottef60cd12008-07-14 09:59:20 +0200208 if (device_is_registered(&sch->dev))
209 device_unregister(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200210 mutex_unlock(&sch->reg_mutex);
211}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200212EXPORT_SYMBOL_GPL(css_sch_device_unregister);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200213
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200214static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
215{
216 int i;
217 int mask;
218
219 memset(ssd, 0, sizeof(struct chsc_ssd_info));
220 ssd->path_mask = pmcw->pim;
221 for (i = 0; i < 8; i++) {
222 mask = 0x80 >> i;
223 if (pmcw->pim & mask) {
224 chp_id_init(&ssd->chpid[i]);
225 ssd->chpid[i].id = pmcw->chpid[i];
226 }
227 }
228}
229
230static void ssd_register_chpids(struct chsc_ssd_info *ssd)
231{
232 int i;
233 int mask;
234
235 for (i = 0; i < 8; i++) {
236 mask = 0x80 >> i;
237 if (ssd->path_mask & mask)
238 if (!chp_is_registered(ssd->chpid[i]))
239 chp_new(ssd->chpid[i]);
240 }
241}
242
243void css_update_ssd_info(struct subchannel *sch)
244{
245 int ret;
246
Sebastian Ott14556b32013-04-13 13:03:54 +0200247 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
248 if (ret)
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200249 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
Sebastian Ott14556b32013-04-13 13:03:54 +0200250
251 ssd_register_chpids(&sch->ssd_info);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200252}
253
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200254static ssize_t type_show(struct device *dev, struct device_attribute *attr,
255 char *buf)
256{
257 struct subchannel *sch = to_subchannel(dev);
258
259 return sprintf(buf, "%01x\n", sch->st);
260}
261
262static DEVICE_ATTR(type, 0444, type_show, NULL);
263
264static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
265 char *buf)
266{
267 struct subchannel *sch = to_subchannel(dev);
268
269 return sprintf(buf, "css:t%01X\n", sch->st);
270}
271
272static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
273
274static struct attribute *subch_attrs[] = {
275 &dev_attr_type.attr,
276 &dev_attr_modalias.attr,
277 NULL,
278};
279
280static struct attribute_group subch_attr_group = {
281 .attrs = subch_attrs,
282};
283
David Brownella4dbd672009-06-24 10:06:31 -0700284static const struct attribute_group *default_subch_attr_groups[] = {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200285 &subch_attr_group,
286 NULL,
287};
288
Sebastian Ott14556b32013-04-13 13:03:54 +0200289int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290{
291 int ret;
292
293 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200294 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 sch->dev.bus = &css_bus_type;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200296 sch->dev.groups = default_subch_attr_groups;
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200297 /*
298 * We don't want to generate uevents for I/O subchannels that don't
299 * have a working ccw device behind them since they will be
300 * unregistered before they can be used anyway, so we delay the add
301 * uevent until after device recognition was successful.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200302 * Note that we suppress the uevent for all subchannel types;
303 * the subchannel driver can decide itself when it wants to inform
304 * userspace of its existence.
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200305 */
Ming Leif67f1292009-03-01 21:10:49 +0800306 dev_set_uevent_suppress(&sch->dev, 1);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200307 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200309 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100310 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200311 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
312 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100313 return ret;
314 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200315 if (!sch->driver) {
316 /*
317 * No driver matched. Generate the uevent now so that
318 * a fitting driver module may be loaded based on the
319 * modalias.
320 */
Ming Leif67f1292009-03-01 21:10:49 +0800321 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200322 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 return ret;
325}
326
Sebastian Ott4e5ebd52013-04-13 13:04:49 +0200327static int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 struct subchannel *sch;
Sebastian Ott4e5ebd52013-04-13 13:04:49 +0200330 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Sebastian Ott14556b32013-04-13 13:03:54 +0200332 sch = css_alloc_subchannel(schid);
333 if (IS_ERR(sch))
334 return PTR_ERR(sch);
335
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 ret = css_register_subchannel(sch);
Sebastian Ott863fc842013-04-13 13:01:50 +0200337 if (ret)
338 put_device(&sch->dev);
339
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340 return ret;
341}
342
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700343static int
344check_subchannel(struct device * dev, void * data)
345{
346 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800347 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700348
349 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800350 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700351}
352
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800354get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 struct device *dev;
357
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700358 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200359 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700361 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362}
363
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100364/**
365 * css_sch_is_valid() - check if a subchannel is valid
366 * @schib: subchannel information block for the subchannel
367 */
368int css_sch_is_valid(struct schib *schib)
369{
370 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
371 return 0;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200372 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
373 return 0;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100374 return 1;
375}
376EXPORT_SYMBOL_GPL(css_sch_is_valid);
377
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200378static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
379{
380 struct schib schib;
381
382 if (!slow) {
383 /* Will be done on the slow path. */
384 return -EAGAIN;
385 }
Sebastian Ottcec85462012-10-15 12:24:56 +0200386 if (stsch_err(schid, &schib)) {
387 /* Subchannel is not provided. */
388 return -ENXIO;
389 }
390 if (!css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200391 /* Unusable - ignore. */
392 return 0;
393 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100394 CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
395 schid.sch_no);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200396
397 return css_probe_device(schid);
398}
399
Cornelia Huckc820de32008-07-14 09:58:45 +0200400static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
401{
402 int ret = 0;
403
404 if (sch->driver) {
405 if (sch->driver->sch_event)
406 ret = sch->driver->sch_event(sch, slow);
407 else
408 dev_dbg(&sch->dev,
409 "Got subchannel machine check but "
410 "no sch_event handler provided.\n");
411 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100412 if (ret != 0 && ret != -EAGAIN) {
413 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
414 sch->schid.ssid, sch->schid.sch_no, ret);
415 }
Cornelia Huckc820de32008-07-14 09:58:45 +0200416 return ret;
417}
418
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200419static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200420{
421 struct subchannel *sch;
422 int ret;
423
424 sch = get_subchannel_by_schid(schid);
425 if (sch) {
426 ret = css_evaluate_known_subchannel(sch, slow);
427 put_device(&sch->dev);
428 } else
429 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200430 if (ret == -EAGAIN)
431 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
Sebastian Ott817e5002011-12-01 13:32:19 +0100434/**
435 * css_sched_sch_todo - schedule a subchannel operation
436 * @sch: subchannel
437 * @todo: todo
438 *
439 * Schedule the operation identified by @todo to be performed on the slow path
440 * workqueue. Do nothing if another operation with higher priority is already
441 * scheduled. Needs to be called with subchannel lock held.
442 */
443void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
444{
445 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
446 sch->schid.ssid, sch->schid.sch_no, todo);
447 if (sch->todo >= todo)
448 return;
449 /* Get workqueue ref. */
450 if (!get_device(&sch->dev))
451 return;
452 sch->todo = todo;
453 if (!queue_work(cio_work_q, &sch->todo_work)) {
454 /* Already queued, release workqueue ref. */
455 put_device(&sch->dev);
456 }
457}
Sebastian Ott01c5e6d2012-08-28 16:42:37 +0200458EXPORT_SYMBOL_GPL(css_sched_sch_todo);
Sebastian Ott817e5002011-12-01 13:32:19 +0100459
460static void css_sch_todo(struct work_struct *work)
461{
462 struct subchannel *sch;
463 enum sch_todo todo;
464 int ret;
465
466 sch = container_of(work, struct subchannel, todo_work);
467 /* Find out todo. */
468 spin_lock_irq(sch->lock);
469 todo = sch->todo;
470 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
471 sch->schid.sch_no, todo);
472 sch->todo = SCH_TODO_NOTHING;
473 spin_unlock_irq(sch->lock);
474 /* Perform todo. */
475 switch (todo) {
476 case SCH_TODO_NOTHING:
477 break;
478 case SCH_TODO_EVAL:
479 ret = css_evaluate_known_subchannel(sch, 1);
480 if (ret == -EAGAIN) {
481 spin_lock_irq(sch->lock);
482 css_sched_sch_todo(sch, todo);
483 spin_unlock_irq(sch->lock);
484 }
485 break;
486 case SCH_TODO_UNREG:
487 css_sch_device_unregister(sch);
488 break;
489 }
490 /* Release workqueue ref. */
491 put_device(&sch->dev);
492}
493
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200494static struct idset *slow_subchannel_set;
495static spinlock_t slow_subchannel_lock;
Sebastian Ott25530552009-09-22 22:58:34 +0200496static wait_queue_head_t css_eval_wq;
497static atomic_t css_eval_scheduled;
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200498
499static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200501 spin_lock_init(&slow_subchannel_lock);
Sebastian Ott25530552009-09-22 22:58:34 +0200502 atomic_set(&css_eval_scheduled, 0);
503 init_waitqueue_head(&css_eval_wq);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200504 slow_subchannel_set = idset_sch_new();
505 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200506 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200507 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200509 return 0;
510}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100512static int slow_eval_known_fn(struct subchannel *sch, void *data)
513{
514 int eval;
515 int rc;
516
517 spin_lock_irq(&slow_subchannel_lock);
518 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
519 idset_sch_del(slow_subchannel_set, sch->schid);
520 spin_unlock_irq(&slow_subchannel_lock);
521 if (eval) {
522 rc = css_evaluate_known_subchannel(sch, 1);
523 if (rc == -EAGAIN)
524 css_schedule_eval(sch->schid);
525 }
526 return 0;
527}
528
529static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
530{
531 int eval;
532 int rc = 0;
533
534 spin_lock_irq(&slow_subchannel_lock);
535 eval = idset_sch_contains(slow_subchannel_set, schid);
536 idset_sch_del(slow_subchannel_set, schid);
537 spin_unlock_irq(&slow_subchannel_lock);
538 if (eval) {
539 rc = css_evaluate_new_subchannel(schid, 1);
540 switch (rc) {
541 case -EAGAIN:
542 css_schedule_eval(schid);
543 rc = 0;
544 break;
545 case -ENXIO:
546 case -ENOMEM:
547 case -EIO:
548 /* These should abort looping */
Sebastian Otteb072a72013-08-29 20:31:06 +0200549 spin_lock_irq(&slow_subchannel_lock);
Sebastian Ottcec85462012-10-15 12:24:56 +0200550 idset_sch_del_subseq(slow_subchannel_set, schid);
Sebastian Otteb072a72013-08-29 20:31:06 +0200551 spin_unlock_irq(&slow_subchannel_lock);
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100552 break;
553 default:
554 rc = 0;
555 }
Peter Oberparleiterb207f5a2013-11-26 14:57:13 +0100556 /* Allow scheduling here since the containing loop might
557 * take a while. */
558 cond_resched();
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100559 }
560 return rc;
561}
562
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200563static void css_slow_path_func(struct work_struct *unused)
564{
Sebastian Ott25530552009-09-22 22:58:34 +0200565 unsigned long flags;
566
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200567 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100568 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
569 NULL);
Sebastian Ott25530552009-09-22 22:58:34 +0200570 spin_lock_irqsave(&slow_subchannel_lock, flags);
571 if (idset_is_empty(slow_subchannel_set)) {
572 atomic_set(&css_eval_scheduled, 0);
573 wake_up(&css_eval_wq);
574 }
575 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576}
577
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200578static DECLARE_WORK(slow_path_work, css_slow_path_func);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100579struct workqueue_struct *cio_work_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200581void css_schedule_eval(struct subchannel_id schid)
582{
583 unsigned long flags;
584
585 spin_lock_irqsave(&slow_subchannel_lock, flags);
586 idset_sch_add(slow_subchannel_set, schid);
Sebastian Ott25530552009-09-22 22:58:34 +0200587 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100588 queue_work(cio_work_q, &slow_path_work);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200589 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
590}
591
592void css_schedule_eval_all(void)
593{
594 unsigned long flags;
595
596 spin_lock_irqsave(&slow_subchannel_lock, flags);
597 idset_fill(slow_subchannel_set);
Sebastian Ott25530552009-09-22 22:58:34 +0200598 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100599 queue_work(cio_work_q, &slow_path_work);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200600 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
601}
602
Sebastian Ott703e5c92009-09-22 22:58:38 +0200603static int __unset_registered(struct device *dev, void *data)
604{
605 struct idset *set = data;
606 struct subchannel *sch = to_subchannel(dev);
607
608 idset_sch_del(set, sch->schid);
609 return 0;
610}
611
Sebastian Otta8481c22010-10-25 16:10:23 +0200612static void css_schedule_eval_all_unreg(void)
Sebastian Ott703e5c92009-09-22 22:58:38 +0200613{
614 unsigned long flags;
615 struct idset *unreg_set;
616
617 /* Find unregistered subchannels. */
618 unreg_set = idset_sch_new();
619 if (!unreg_set) {
620 /* Fallback. */
621 css_schedule_eval_all();
622 return;
623 }
624 idset_fill(unreg_set);
625 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
626 /* Apply to slow_subchannel_set. */
627 spin_lock_irqsave(&slow_subchannel_lock, flags);
628 idset_add_set(slow_subchannel_set, unreg_set);
629 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100630 queue_work(cio_work_q, &slow_path_work);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200631 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
632 idset_free(unreg_set);
633}
634
Cornelia Huck22806dc2008-04-17 07:45:59 +0200635void css_wait_for_slow_path(void)
636{
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100637 flush_workqueue(cio_work_q);
Cornelia Huck22806dc2008-04-17 07:45:59 +0200638}
639
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200640/* Schedule reprobing of all unregistered subchannels. */
641void css_schedule_reprobe(void)
642{
Sebastian Ott703e5c92009-09-22 22:58:38 +0200643 css_schedule_eval_all_unreg();
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200644}
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200645EXPORT_SYMBOL_GPL(css_schedule_reprobe);
646
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 * Called from the machine check handler for subchannel report words.
649 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200650static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800652 struct subchannel_id mchk_schid;
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100653 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
Cornelia Huckc1156182008-07-14 09:58:46 +0200655 if (overflow) {
656 css_schedule_eval_all();
657 return;
658 }
659 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
660 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
661 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
662 crw0->erc, crw0->rsid);
663 if (crw1)
664 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
665 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
666 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
667 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800668 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200669 mchk_schid.sch_no = crw0->rsid;
670 if (crw1)
Sebastian Ott8d7bfb42010-12-01 10:08:02 +0100671 mchk_schid.ssid = (crw1->rsid >> 4) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800672
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100673 if (crw0->erc == CRW_ERC_PMOD) {
674 sch = get_subchannel_by_schid(mchk_schid);
675 if (sch) {
676 css_update_ssd_info(sch);
677 put_device(&sch->dev);
678 }
679 }
Cornelia Huckc1156182008-07-14 09:58:46 +0200680 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 * Since we are always presented with IPI in the CRW, we have to
682 * use stsch() to find out if the subchannel in question has come
683 * or gone.
684 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200685 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686}
687
688static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800689css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690{
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200691 struct cpuid cpu_id;
692
Cornelia Huck75784c02008-07-14 09:58:57 +0200693 if (css_general_characteristics.mcss) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800694 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
695 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
696 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697#ifdef CONFIG_SMP
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100698 css->global_pgid.pgid_high.cpu_addr = stap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800700 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701#endif
702 }
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200703 get_cpu_id(&cpu_id);
704 css->global_pgid.cpu_id = cpu_id.ident;
705 css->global_pgid.cpu_model = cpu_id.machine;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800706 css->global_pgid.tod_high = tod_high;
707
708}
709
Cornelia Huck3b793062006-01-06 00:19:26 -0800710static void
711channel_subsystem_release(struct device *dev)
712{
713 struct channel_subsystem *css;
714
715 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800716 mutex_destroy(&css->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200717 if (css->pseudo_subchannel) {
718 /* Implies that it has been generated but never registered. */
719 css_subchannel_release(&css->pseudo_subchannel->dev);
720 css->pseudo_subchannel = NULL;
721 }
Cornelia Huck3b793062006-01-06 00:19:26 -0800722 kfree(css);
723}
724
Cornelia Huck495a5b42006-03-24 03:15:14 -0800725static ssize_t
726css_cm_enable_show(struct device *dev, struct device_attribute *attr,
727 char *buf)
728{
729 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200730 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800731
732 if (!css)
733 return 0;
Michael Ernst8284fb192008-04-17 07:46:01 +0200734 mutex_lock(&css->mutex);
735 ret = sprintf(buf, "%x\n", css->cm_enabled);
736 mutex_unlock(&css->mutex);
737 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800738}
739
740static ssize_t
741css_cm_enable_store(struct device *dev, struct device_attribute *attr,
742 const char *buf, size_t count)
743{
744 struct channel_subsystem *css = to_css(dev);
745 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +0200746 unsigned long val;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800747
Jingoo Han01787222013-07-22 10:18:15 +0900748 ret = kstrtoul(buf, 16, &val);
Cornelia Huck2f972202008-04-30 13:38:33 +0200749 if (ret)
750 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200751 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200752 switch (val) {
753 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800754 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
755 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200756 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800757 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
758 break;
759 default:
760 ret = -EINVAL;
761 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200762 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800763 return ret < 0 ? ret : count;
764}
765
766static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
767
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100768static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800769{
770 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100771 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200772 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800773
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200774 css = channel_subsystems[nr];
775 memset(css, 0, sizeof(struct channel_subsystem));
776 css->pseudo_subchannel =
777 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
778 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100779 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200780 css->pseudo_subchannel->dev.parent = &css->device;
781 css->pseudo_subchannel->dev.release = css_subchannel_release;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200782 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100783 mutex_init(&css->pseudo_subchannel->reg_mutex);
Sebastian Otte5dcf002013-04-13 13:08:01 +0200784 ret = css_sch_create_locks(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100785 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200786 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100787 return ret;
788 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200789 mutex_init(&css->mutex);
790 css->valid = 1;
791 css->cssid = nr;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200792 dev_set_name(&css->device, "css%x", nr);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200793 css->device.release = channel_subsystem_release;
Heiko Carstens1aae0562013-01-30 09:49:40 +0100794 tod_high = (u32) (get_tod_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200795 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100796 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797}
798
Cornelia Hucka55360d2007-10-12 16:11:20 +0200799static int css_reboot_event(struct notifier_block *this,
800 unsigned long event,
801 void *ptr)
802{
803 int ret, i;
804
805 ret = NOTIFY_DONE;
806 for (i = 0; i <= __MAX_CSSID; i++) {
807 struct channel_subsystem *css;
808
809 css = channel_subsystems[i];
Michael Ernst8284fb192008-04-17 07:46:01 +0200810 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200811 if (css->cm_enabled)
812 if (chsc_secm(css, 0))
813 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200814 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200815 }
816
817 return ret;
818}
819
820static struct notifier_block css_reboot_notifier = {
821 .notifier_call = css_reboot_event,
822};
823
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200825 * Since the css devices are neither on a bus nor have a class
826 * nor have a special device type, we cannot stop/restart channel
827 * path measurements via the normal suspend/resume callbacks, but have
828 * to use notifiers.
829 */
830static int css_power_event(struct notifier_block *this, unsigned long event,
831 void *ptr)
832{
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200833 int ret, i;
834
835 switch (event) {
836 case PM_HIBERNATION_PREPARE:
837 case PM_SUSPEND_PREPARE:
838 ret = NOTIFY_DONE;
839 for (i = 0; i <= __MAX_CSSID; i++) {
840 struct channel_subsystem *css;
841
842 css = channel_subsystems[i];
843 mutex_lock(&css->mutex);
844 if (!css->cm_enabled) {
845 mutex_unlock(&css->mutex);
846 continue;
847 }
Akinobu Mitaf0c077a2011-07-08 20:53:36 +0200848 ret = __chsc_do_secm(css, 0);
849 ret = notifier_from_errno(ret);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200850 mutex_unlock(&css->mutex);
851 }
852 break;
853 case PM_POST_HIBERNATION:
854 case PM_POST_SUSPEND:
855 ret = NOTIFY_DONE;
856 for (i = 0; i <= __MAX_CSSID; i++) {
857 struct channel_subsystem *css;
858
859 css = channel_subsystems[i];
860 mutex_lock(&css->mutex);
861 if (!css->cm_enabled) {
862 mutex_unlock(&css->mutex);
863 continue;
864 }
Akinobu Mitaf0c077a2011-07-08 20:53:36 +0200865 ret = __chsc_do_secm(css, 1);
866 ret = notifier_from_errno(ret);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200867 mutex_unlock(&css->mutex);
868 }
869 /* search for subchannels, which appeared during hibernation */
870 css_schedule_reprobe();
871 break;
872 default:
873 ret = NOTIFY_DONE;
874 }
875 return ret;
876
877}
878static struct notifier_block css_power_notifier = {
879 .notifier_call = css_power_event,
880};
881
882/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 * Now that the driver core is running, we can setup our channel subsystem.
Sebastian Ott14556b32013-04-13 13:03:54 +0200884 * The struct subchannel's are created during probing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 */
Sebastian Ott2f176442009-09-22 22:58:33 +0200886static int __init css_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800888 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889
Sebastian Ott34aec072010-10-25 16:10:28 +0200890 ret = chsc_init();
891 if (ret)
892 return ret;
893
Sebastian Ott34196f82010-10-25 16:10:29 +0200894 chsc_determine_css_characteristics();
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200895 /* Try to enable MSS. */
896 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott818c2722010-04-22 17:17:03 +0200897 if (ret)
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200898 max_ssid = 0;
Sebastian Ott818c2722010-04-22 17:17:03 +0200899 else /* Success. */
900 max_ssid = __MAX_SSID;
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200901
Cornelia Huck4434a382007-07-27 12:29:21 +0200902 ret = slow_subchannel_init();
903 if (ret)
904 goto out;
905
Heiko Carstensf5daba12009-03-26 15:24:01 +0100906 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
Cornelia Huckc1156182008-07-14 09:58:46 +0200907 if (ret)
908 goto out;
909
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if ((ret = bus_register(&css_bus_type)))
911 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Cornelia Hucka28c6942006-01-06 00:19:23 -0800913 /* Setup css structure. */
914 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200915 struct channel_subsystem *css;
916
917 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
918 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800919 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800920 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800921 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200922 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100923 ret = setup_css(i);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200924 if (ret) {
925 kfree(channel_subsystems[i]);
926 goto out_unregister;
927 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200928 ret = device_register(&css->device);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200929 if (ret) {
930 put_device(&css->device);
931 goto out_unregister;
932 }
Cornelia Huck75784c02008-07-14 09:58:57 +0200933 if (css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200934 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200935 &dev_attr_cm_enable);
936 if (ret)
937 goto out_device;
938 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200939 ret = device_register(&css->pseudo_subchannel->dev);
Sebastian Ottc6304932009-09-11 10:28:38 +0200940 if (ret) {
941 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100942 goto out_file;
Sebastian Ottc6304932009-09-11 10:28:38 +0200943 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800944 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200945 ret = register_reboot_notifier(&css_reboot_notifier);
946 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +0200947 goto out_unregister;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200948 ret = register_pm_notifier(&css_power_notifier);
949 if (ret) {
950 unregister_reboot_notifier(&css_reboot_notifier);
951 goto out_unregister;
952 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 css_init_done = 1;
954
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200955 /* Enable default isc for I/O subchannels. */
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200956 isc_register(IO_SCH_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 return 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100959out_file:
Cornelia Hucka2164b82008-09-09 12:38:57 +0200960 if (css_chsc_characteristics.secm)
961 device_remove_file(&channel_subsystems[i]->device,
962 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200963out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200964 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800965out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800966 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200967 struct channel_subsystem *css;
968
Cornelia Hucka28c6942006-01-06 00:19:23 -0800969 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200970 css = channel_subsystems[i];
971 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200972 css->pseudo_subchannel = NULL;
Cornelia Huck75784c02008-07-14 09:58:57 +0200973 if (css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200974 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800975 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200976 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800977 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 bus_unregister(&css_bus_type);
979out:
Sebastian Ott34aec072010-10-25 16:10:28 +0200980 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c82009-09-22 22:58:36 +0200981 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +0200982 chsc_init_cleanup();
Michael Ernste6d5a422008-12-25 13:39:36 +0100983 pr_alert("The CSS device driver initialization failed with "
984 "errno=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 return ret;
986}
987
Sebastian Ott2f176442009-09-22 22:58:33 +0200988static void __init css_bus_cleanup(void)
989{
990 struct channel_subsystem *css;
991 int i;
992
993 for (i = 0; i <= __MAX_CSSID; i++) {
994 css = channel_subsystems[i];
995 device_unregister(&css->pseudo_subchannel->dev);
996 css->pseudo_subchannel = NULL;
997 if (css_chsc_characteristics.secm)
998 device_remove_file(&css->device, &dev_attr_cm_enable);
999 device_unregister(&css->device);
1000 }
1001 bus_unregister(&css_bus_type);
Sebastian Ott34aec072010-10-25 16:10:28 +02001002 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c82009-09-22 22:58:36 +02001003 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +02001004 chsc_init_cleanup();
Sebastian Ott2f176442009-09-22 22:58:33 +02001005 isc_unregister(IO_SCH_ISC);
1006}
1007
1008static int __init channel_subsystem_init(void)
1009{
1010 int ret;
1011
1012 ret = css_bus_init();
1013 if (ret)
1014 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001015 cio_work_q = create_singlethread_workqueue("cio");
1016 if (!cio_work_q) {
1017 ret = -ENOMEM;
1018 goto out_bus;
1019 }
Sebastian Ott2f176442009-09-22 22:58:33 +02001020 ret = io_subchannel_init();
1021 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001022 goto out_wq;
Sebastian Ott2f176442009-09-22 22:58:33 +02001023
1024 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001025out_wq:
1026 destroy_workqueue(cio_work_q);
1027out_bus:
1028 css_bus_cleanup();
1029 return ret;
Sebastian Ott2f176442009-09-22 22:58:33 +02001030}
1031subsys_initcall(channel_subsystem_init);
1032
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001033static int css_settle(struct device_driver *drv, void *unused)
1034{
1035 struct css_driver *cssdrv = to_cssdriver(drv);
1036
1037 if (cssdrv->settle)
Sebastian Ottb4c70722010-02-26 22:37:27 +01001038 return cssdrv->settle();
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001039 return 0;
1040}
1041
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001042int css_complete_work(void)
Sebastian Ott879acca52010-02-26 22:37:25 +01001043{
1044 int ret;
1045
1046 /* Wait for the evaluation of subchannels to finish. */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001047 ret = wait_event_interruptible(css_eval_wq,
1048 atomic_read(&css_eval_scheduled) == 0);
1049 if (ret)
1050 return -EINTR;
Sebastian Ott879acca52010-02-26 22:37:25 +01001051 flush_workqueue(cio_work_q);
1052 /* Wait for the subchannel type specific initialization to finish */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001053 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
Sebastian Ott879acca52010-02-26 22:37:25 +01001054}
1055
1056
Sebastian Ott2f176442009-09-22 22:58:33 +02001057/*
1058 * Wait for the initialization of devices to finish, to make sure we are
1059 * done with our setup if the search for the root device starts.
1060 */
1061static int __init channel_subsystem_init_sync(void)
1062{
Sebastian Ott14556b32013-04-13 13:03:54 +02001063 /* Register subchannels which are already in use. */
1064 cio_register_early_subchannels();
Sebastian Ott703e5c92009-09-22 22:58:38 +02001065 /* Start initial subchannel evaluation. */
1066 css_schedule_eval_all();
Sebastian Ott879acca52010-02-26 22:37:25 +01001067 css_complete_work();
1068 return 0;
Sebastian Ott2f176442009-09-22 22:58:33 +02001069}
1070subsys_initcall_sync(channel_subsystem_init_sync);
1071
Sebastian Ott889ee952010-04-22 17:17:04 +02001072void channel_subsystem_reinit(void)
1073{
Sebastian Ott62da1772010-10-25 16:10:32 +02001074 struct channel_path *chp;
1075 struct chp_id chpid;
1076
Sebastian Ott889ee952010-04-22 17:17:04 +02001077 chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott62da1772010-10-25 16:10:32 +02001078 chp_id_for_each(&chpid) {
1079 chp = chpid_to_chp(chpid);
Peter Oberparleitercce0eac2013-03-11 12:58:18 +01001080 if (chp)
1081 chp_update_desc(chp);
Sebastian Ott62da1772010-10-25 16:10:32 +02001082 }
Sebastian Ott889ee952010-04-22 17:17:04 +02001083}
1084
Sebastian Ott879acca52010-02-26 22:37:25 +01001085#ifdef CONFIG_PROC_FS
1086static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1087 size_t count, loff_t *ppos)
1088{
Sebastian Ottb4c70722010-02-26 22:37:27 +01001089 int ret;
1090
Sebastian Ott879acca52010-02-26 22:37:25 +01001091 /* Handle pending CRW's. */
1092 crw_wait_for_channel_report();
Sebastian Ottb4c70722010-02-26 22:37:27 +01001093 ret = css_complete_work();
1094
1095 return ret ? ret : count;
Sebastian Ott879acca52010-02-26 22:37:25 +01001096}
1097
1098static const struct file_operations cio_settle_proc_fops = {
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +02001099 .open = nonseekable_open,
Sebastian Ott879acca52010-02-26 22:37:25 +01001100 .write = cio_settle_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001101 .llseek = no_llseek,
Sebastian Ott879acca52010-02-26 22:37:25 +01001102};
1103
1104static int __init cio_settle_init(void)
1105{
1106 struct proc_dir_entry *entry;
1107
1108 entry = proc_create("cio_settle", S_IWUSR, NULL,
1109 &cio_settle_proc_fops);
1110 if (!entry)
1111 return -ENOMEM;
1112 return 0;
1113}
1114device_initcall(cio_settle_init);
1115#endif /*CONFIG_PROC_FS*/
1116
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001117int sch_is_pseudo_sch(struct subchannel *sch)
1118{
1119 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1120}
1121
Cornelia Huckf08adc02008-07-14 09:59:03 +02001122static int css_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
Cornelia Huck084325d2008-01-26 14:10:38 +01001124 struct subchannel *sch = to_subchannel(dev);
1125 struct css_driver *driver = to_cssdriver(drv);
Cornelia Huckf08adc02008-07-14 09:59:03 +02001126 struct css_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127
Cornelia Huckf08adc02008-07-14 09:59:03 +02001128 for (id = driver->subchannel_type; id->match_flags; id++) {
1129 if (sch->st == id->type)
1130 return 1;
1131 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133 return 0;
1134}
1135
Cornelia Huck98c13c22008-01-26 14:10:40 +01001136static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001137{
1138 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +01001139 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001140
1141 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +01001142 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001143 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1144 if (ret)
1145 sch->driver = NULL;
1146 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001147}
1148
Cornelia Huck98c13c22008-01-26 14:10:40 +01001149static int css_remove(struct device *dev)
1150{
1151 struct subchannel *sch;
1152 int ret;
1153
1154 sch = to_subchannel(dev);
1155 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1156 sch->driver = NULL;
1157 return ret;
1158}
1159
1160static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001161{
1162 struct subchannel *sch;
1163
1164 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001165 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001166 sch->driver->shutdown(sch);
1167}
1168
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001169static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1170{
1171 struct subchannel *sch = to_subchannel(dev);
1172 int ret;
1173
1174 ret = add_uevent_var(env, "ST=%01X", sch->st);
1175 if (ret)
1176 return ret;
1177 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1178 return ret;
1179}
1180
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001181static int css_pm_prepare(struct device *dev)
1182{
1183 struct subchannel *sch = to_subchannel(dev);
1184 struct css_driver *drv;
1185
1186 if (mutex_is_locked(&sch->reg_mutex))
1187 return -EAGAIN;
1188 if (!sch->dev.driver)
1189 return 0;
1190 drv = to_cssdriver(sch->dev.driver);
1191 /* Notify drivers that they may not register children. */
1192 return drv->prepare ? drv->prepare(sch) : 0;
1193}
1194
1195static void css_pm_complete(struct device *dev)
1196{
1197 struct subchannel *sch = to_subchannel(dev);
1198 struct css_driver *drv;
1199
1200 if (!sch->dev.driver)
1201 return;
1202 drv = to_cssdriver(sch->dev.driver);
1203 if (drv->complete)
1204 drv->complete(sch);
1205}
1206
1207static int css_pm_freeze(struct device *dev)
1208{
1209 struct subchannel *sch = to_subchannel(dev);
1210 struct css_driver *drv;
1211
1212 if (!sch->dev.driver)
1213 return 0;
1214 drv = to_cssdriver(sch->dev.driver);
1215 return drv->freeze ? drv->freeze(sch) : 0;
1216}
1217
1218static int css_pm_thaw(struct device *dev)
1219{
1220 struct subchannel *sch = to_subchannel(dev);
1221 struct css_driver *drv;
1222
1223 if (!sch->dev.driver)
1224 return 0;
1225 drv = to_cssdriver(sch->dev.driver);
1226 return drv->thaw ? drv->thaw(sch) : 0;
1227}
1228
1229static int css_pm_restore(struct device *dev)
1230{
1231 struct subchannel *sch = to_subchannel(dev);
1232 struct css_driver *drv;
1233
Sebastian Otteb4f5d92010-10-25 16:10:33 +02001234 css_update_ssd_info(sch);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001235 if (!sch->dev.driver)
1236 return 0;
1237 drv = to_cssdriver(sch->dev.driver);
1238 return drv->restore ? drv->restore(sch) : 0;
1239}
1240
Alexey Dobriyan47145212009-12-14 18:00:08 -08001241static const struct dev_pm_ops css_pm_ops = {
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001242 .prepare = css_pm_prepare,
1243 .complete = css_pm_complete,
1244 .freeze = css_pm_freeze,
1245 .thaw = css_pm_thaw,
1246 .restore = css_pm_restore,
1247};
1248
Sebastian Ott3041b6a2011-03-15 17:08:31 +01001249static struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001250 .name = "css",
1251 .match = css_bus_match,
1252 .probe = css_probe,
1253 .remove = css_remove,
1254 .shutdown = css_shutdown,
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001255 .uevent = css_uevent,
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001256 .pm = &css_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257};
1258
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001259/**
1260 * css_driver_register - register a css driver
1261 * @cdrv: css driver to register
1262 *
1263 * This is mainly a wrapper around driver_register that sets name
1264 * and bus_type in the embedded struct device_driver correctly.
1265 */
1266int css_driver_register(struct css_driver *cdrv)
1267{
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001268 cdrv->drv.bus = &css_bus_type;
1269 return driver_register(&cdrv->drv);
1270}
1271EXPORT_SYMBOL_GPL(css_driver_register);
1272
1273/**
1274 * css_driver_unregister - unregister a css driver
1275 * @cdrv: css driver to unregister
1276 *
1277 * This is a wrapper around driver_unregister.
1278 */
1279void css_driver_unregister(struct css_driver *cdrv)
1280{
1281 driver_unregister(&cdrv->drv);
1282}
1283EXPORT_SYMBOL_GPL(css_driver_unregister);
1284
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285MODULE_LICENSE("GPL");