blob: fb0e64f1845ab54aae38b928fa5b1ae538668a71 [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 Ottc135ad12013-04-13 12:58:55 +0200140static void css_subchannel_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Sebastian Ott863fc842013-04-13 13:01:50 +0200142 struct subchannel *sch = to_subchannel(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143
Sebastian Ott863fc842013-04-13 13:01:50 +0200144 sch->config.intparm = 0;
145 cio_commit_config(sch);
146 kfree(sch->lock);
147 kfree(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Sebastian Ott863fc842013-04-13 13:01:50 +0200150struct subchannel *css_alloc_subchannel(struct subchannel_id schid)
Sebastian Ottc135ad12013-04-13 12:58:55 +0200151{
152 struct subchannel *sch;
153 int ret;
154
155 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
156 if (sch == NULL)
157 return ERR_PTR(-ENOMEM);
158 ret = cio_validate_subchannel (sch, schid);
159 if (ret < 0) {
160 kfree(sch);
161 return ERR_PTR(ret);
162 }
163 INIT_WORK(&sch->todo_work, css_sch_todo);
164 sch->dev.release = &css_subchannel_release;
165 device_initialize(&sch->dev);
166 return sch;
167}
168
Cornelia Huck07c6a332007-07-27 12:29:10 +0200169static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200170{
171 int ret;
172
173 mutex_lock(&sch->reg_mutex);
Sebastian Ott6ee4fec2009-09-11 10:28:25 +0200174 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
175 sch->schid.sch_no);
Sebastian Ottc135ad12013-04-13 12:58:55 +0200176 ret = device_add(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200177 mutex_unlock(&sch->reg_mutex);
178 return ret;
179}
180
Cornelia Huck44a1c192008-07-14 09:58:47 +0200181/**
182 * css_sch_device_unregister - unregister a subchannel
183 * @sch: subchannel to be unregistered
184 */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200185void css_sch_device_unregister(struct subchannel *sch)
186{
187 mutex_lock(&sch->reg_mutex);
Sebastian Ottef60cd12008-07-14 09:59:20 +0200188 if (device_is_registered(&sch->dev))
189 device_unregister(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200190 mutex_unlock(&sch->reg_mutex);
191}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200192EXPORT_SYMBOL_GPL(css_sch_device_unregister);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200193
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200194static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
195{
196 int i;
197 int mask;
198
199 memset(ssd, 0, sizeof(struct chsc_ssd_info));
200 ssd->path_mask = pmcw->pim;
201 for (i = 0; i < 8; i++) {
202 mask = 0x80 >> i;
203 if (pmcw->pim & mask) {
204 chp_id_init(&ssd->chpid[i]);
205 ssd->chpid[i].id = pmcw->chpid[i];
206 }
207 }
208}
209
210static void ssd_register_chpids(struct chsc_ssd_info *ssd)
211{
212 int i;
213 int mask;
214
215 for (i = 0; i < 8; i++) {
216 mask = 0x80 >> i;
217 if (ssd->path_mask & mask)
218 if (!chp_is_registered(ssd->chpid[i]))
219 chp_new(ssd->chpid[i]);
220 }
221}
222
223void css_update_ssd_info(struct subchannel *sch)
224{
225 int ret;
226
227 if (cio_is_console(sch->schid)) {
228 /* Console is initialized too early for functions requiring
229 * memory allocation. */
230 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
231 } else {
232 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
233 if (ret)
234 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
235 ssd_register_chpids(&sch->ssd_info);
236 }
237}
238
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200239static ssize_t type_show(struct device *dev, struct device_attribute *attr,
240 char *buf)
241{
242 struct subchannel *sch = to_subchannel(dev);
243
244 return sprintf(buf, "%01x\n", sch->st);
245}
246
247static DEVICE_ATTR(type, 0444, type_show, NULL);
248
249static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
250 char *buf)
251{
252 struct subchannel *sch = to_subchannel(dev);
253
254 return sprintf(buf, "css:t%01X\n", sch->st);
255}
256
257static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
258
259static struct attribute *subch_attrs[] = {
260 &dev_attr_type.attr,
261 &dev_attr_modalias.attr,
262 NULL,
263};
264
265static struct attribute_group subch_attr_group = {
266 .attrs = subch_attrs,
267};
268
David Brownella4dbd672009-06-24 10:06:31 -0700269static const struct attribute_group *default_subch_attr_groups[] = {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200270 &subch_attr_group,
271 NULL,
272};
273
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200274static int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275{
276 int ret;
277
278 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200279 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 sch->dev.bus = &css_bus_type;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200281 sch->dev.groups = default_subch_attr_groups;
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200282 /*
283 * We don't want to generate uevents for I/O subchannels that don't
284 * have a working ccw device behind them since they will be
285 * unregistered before they can be used anyway, so we delay the add
286 * uevent until after device recognition was successful.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200287 * Note that we suppress the uevent for all subchannel types;
288 * the subchannel driver can decide itself when it wants to inform
289 * userspace of its existence.
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200290 */
Ming Leif67f1292009-03-01 21:10:49 +0800291 dev_set_uevent_suppress(&sch->dev, 1);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200292 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200294 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100295 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200296 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
297 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100298 return ret;
299 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200300 if (!sch->driver) {
301 /*
302 * No driver matched. Generate the uevent now so that
303 * a fitting driver module may be loaded based on the
304 * modalias.
305 */
Ming Leif67f1292009-03-01 21:10:49 +0800306 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200307 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
308 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 return ret;
310}
311
Cornelia Huckc820de32008-07-14 09:58:45 +0200312int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313{
314 int ret;
315 struct subchannel *sch;
316
Sebastian Ott703e5c92009-09-22 22:58:38 +0200317 if (cio_is_console(schid))
318 sch = cio_get_console_subchannel();
319 else {
320 sch = css_alloc_subchannel(schid);
321 if (IS_ERR(sch))
322 return PTR_ERR(sch);
323 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 ret = css_register_subchannel(sch);
Sebastian Ott863fc842013-04-13 13:01:50 +0200325 if (ret)
326 put_device(&sch->dev);
327
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 return ret;
329}
330
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700331static int
332check_subchannel(struct device * dev, void * data)
333{
334 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800335 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700336
337 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800338 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700339}
340
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800342get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 struct device *dev;
345
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700346 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200347 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700349 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350}
351
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100352/**
353 * css_sch_is_valid() - check if a subchannel is valid
354 * @schib: subchannel information block for the subchannel
355 */
356int css_sch_is_valid(struct schib *schib)
357{
358 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
359 return 0;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200360 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
361 return 0;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100362 return 1;
363}
364EXPORT_SYMBOL_GPL(css_sch_is_valid);
365
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200366static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
367{
368 struct schib schib;
369
370 if (!slow) {
371 /* Will be done on the slow path. */
372 return -EAGAIN;
373 }
Sebastian Ottcec85462012-10-15 12:24:56 +0200374 if (stsch_err(schid, &schib)) {
375 /* Subchannel is not provided. */
376 return -ENXIO;
377 }
378 if (!css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200379 /* Unusable - ignore. */
380 return 0;
381 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100382 CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
383 schid.sch_no);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200384
385 return css_probe_device(schid);
386}
387
Cornelia Huckc820de32008-07-14 09:58:45 +0200388static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
389{
390 int ret = 0;
391
392 if (sch->driver) {
393 if (sch->driver->sch_event)
394 ret = sch->driver->sch_event(sch, slow);
395 else
396 dev_dbg(&sch->dev,
397 "Got subchannel machine check but "
398 "no sch_event handler provided.\n");
399 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100400 if (ret != 0 && ret != -EAGAIN) {
401 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
402 sch->schid.ssid, sch->schid.sch_no, ret);
403 }
Cornelia Huckc820de32008-07-14 09:58:45 +0200404 return ret;
405}
406
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200407static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200408{
409 struct subchannel *sch;
410 int ret;
411
412 sch = get_subchannel_by_schid(schid);
413 if (sch) {
414 ret = css_evaluate_known_subchannel(sch, slow);
415 put_device(&sch->dev);
416 } else
417 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200418 if (ret == -EAGAIN)
419 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420}
421
Sebastian Ott817e5002011-12-01 13:32:19 +0100422/**
423 * css_sched_sch_todo - schedule a subchannel operation
424 * @sch: subchannel
425 * @todo: todo
426 *
427 * Schedule the operation identified by @todo to be performed on the slow path
428 * workqueue. Do nothing if another operation with higher priority is already
429 * scheduled. Needs to be called with subchannel lock held.
430 */
431void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
432{
433 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
434 sch->schid.ssid, sch->schid.sch_no, todo);
435 if (sch->todo >= todo)
436 return;
437 /* Get workqueue ref. */
438 if (!get_device(&sch->dev))
439 return;
440 sch->todo = todo;
441 if (!queue_work(cio_work_q, &sch->todo_work)) {
442 /* Already queued, release workqueue ref. */
443 put_device(&sch->dev);
444 }
445}
Sebastian Ott01c5e6d2012-08-28 16:42:37 +0200446EXPORT_SYMBOL_GPL(css_sched_sch_todo);
Sebastian Ott817e5002011-12-01 13:32:19 +0100447
448static void css_sch_todo(struct work_struct *work)
449{
450 struct subchannel *sch;
451 enum sch_todo todo;
452 int ret;
453
454 sch = container_of(work, struct subchannel, todo_work);
455 /* Find out todo. */
456 spin_lock_irq(sch->lock);
457 todo = sch->todo;
458 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
459 sch->schid.sch_no, todo);
460 sch->todo = SCH_TODO_NOTHING;
461 spin_unlock_irq(sch->lock);
462 /* Perform todo. */
463 switch (todo) {
464 case SCH_TODO_NOTHING:
465 break;
466 case SCH_TODO_EVAL:
467 ret = css_evaluate_known_subchannel(sch, 1);
468 if (ret == -EAGAIN) {
469 spin_lock_irq(sch->lock);
470 css_sched_sch_todo(sch, todo);
471 spin_unlock_irq(sch->lock);
472 }
473 break;
474 case SCH_TODO_UNREG:
475 css_sch_device_unregister(sch);
476 break;
477 }
478 /* Release workqueue ref. */
479 put_device(&sch->dev);
480}
481
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200482static struct idset *slow_subchannel_set;
483static spinlock_t slow_subchannel_lock;
Sebastian Ott25530552009-09-22 22:58:34 +0200484static wait_queue_head_t css_eval_wq;
485static atomic_t css_eval_scheduled;
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200486
487static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200489 spin_lock_init(&slow_subchannel_lock);
Sebastian Ott25530552009-09-22 22:58:34 +0200490 atomic_set(&css_eval_scheduled, 0);
491 init_waitqueue_head(&css_eval_wq);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200492 slow_subchannel_set = idset_sch_new();
493 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200494 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200495 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200497 return 0;
498}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100500static int slow_eval_known_fn(struct subchannel *sch, void *data)
501{
502 int eval;
503 int rc;
504
505 spin_lock_irq(&slow_subchannel_lock);
506 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
507 idset_sch_del(slow_subchannel_set, sch->schid);
508 spin_unlock_irq(&slow_subchannel_lock);
509 if (eval) {
510 rc = css_evaluate_known_subchannel(sch, 1);
511 if (rc == -EAGAIN)
512 css_schedule_eval(sch->schid);
513 }
514 return 0;
515}
516
517static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
518{
519 int eval;
520 int rc = 0;
521
522 spin_lock_irq(&slow_subchannel_lock);
523 eval = idset_sch_contains(slow_subchannel_set, schid);
524 idset_sch_del(slow_subchannel_set, schid);
525 spin_unlock_irq(&slow_subchannel_lock);
526 if (eval) {
527 rc = css_evaluate_new_subchannel(schid, 1);
528 switch (rc) {
529 case -EAGAIN:
530 css_schedule_eval(schid);
531 rc = 0;
532 break;
533 case -ENXIO:
534 case -ENOMEM:
535 case -EIO:
536 /* These should abort looping */
Sebastian Ottcec85462012-10-15 12:24:56 +0200537 idset_sch_del_subseq(slow_subchannel_set, schid);
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100538 break;
539 default:
540 rc = 0;
541 }
542 }
543 return rc;
544}
545
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200546static void css_slow_path_func(struct work_struct *unused)
547{
Sebastian Ott25530552009-09-22 22:58:34 +0200548 unsigned long flags;
549
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200550 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100551 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
552 NULL);
Sebastian Ott25530552009-09-22 22:58:34 +0200553 spin_lock_irqsave(&slow_subchannel_lock, flags);
554 if (idset_is_empty(slow_subchannel_set)) {
555 atomic_set(&css_eval_scheduled, 0);
556 wake_up(&css_eval_wq);
557 }
558 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559}
560
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200561static DECLARE_WORK(slow_path_work, css_slow_path_func);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100562struct workqueue_struct *cio_work_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200564void css_schedule_eval(struct subchannel_id schid)
565{
566 unsigned long flags;
567
568 spin_lock_irqsave(&slow_subchannel_lock, flags);
569 idset_sch_add(slow_subchannel_set, schid);
Sebastian Ott25530552009-09-22 22:58:34 +0200570 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100571 queue_work(cio_work_q, &slow_path_work);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200572 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
573}
574
575void css_schedule_eval_all(void)
576{
577 unsigned long flags;
578
579 spin_lock_irqsave(&slow_subchannel_lock, flags);
580 idset_fill(slow_subchannel_set);
Sebastian Ott25530552009-09-22 22:58:34 +0200581 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100582 queue_work(cio_work_q, &slow_path_work);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200583 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
584}
585
Sebastian Ott703e5c92009-09-22 22:58:38 +0200586static int __unset_registered(struct device *dev, void *data)
587{
588 struct idset *set = data;
589 struct subchannel *sch = to_subchannel(dev);
590
591 idset_sch_del(set, sch->schid);
592 return 0;
593}
594
Sebastian Otta8481c22010-10-25 16:10:23 +0200595static void css_schedule_eval_all_unreg(void)
Sebastian Ott703e5c92009-09-22 22:58:38 +0200596{
597 unsigned long flags;
598 struct idset *unreg_set;
599
600 /* Find unregistered subchannels. */
601 unreg_set = idset_sch_new();
602 if (!unreg_set) {
603 /* Fallback. */
604 css_schedule_eval_all();
605 return;
606 }
607 idset_fill(unreg_set);
608 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
609 /* Apply to slow_subchannel_set. */
610 spin_lock_irqsave(&slow_subchannel_lock, flags);
611 idset_add_set(slow_subchannel_set, unreg_set);
612 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100613 queue_work(cio_work_q, &slow_path_work);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200614 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
615 idset_free(unreg_set);
616}
617
Cornelia Huck22806dc2008-04-17 07:45:59 +0200618void css_wait_for_slow_path(void)
619{
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100620 flush_workqueue(cio_work_q);
Cornelia Huck22806dc2008-04-17 07:45:59 +0200621}
622
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200623/* Schedule reprobing of all unregistered subchannels. */
624void css_schedule_reprobe(void)
625{
Sebastian Ott703e5c92009-09-22 22:58:38 +0200626 css_schedule_eval_all_unreg();
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200627}
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200628EXPORT_SYMBOL_GPL(css_schedule_reprobe);
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 * Called from the machine check handler for subchannel report words.
632 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200633static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800635 struct subchannel_id mchk_schid;
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100636 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Cornelia Huckc1156182008-07-14 09:58:46 +0200638 if (overflow) {
639 css_schedule_eval_all();
640 return;
641 }
642 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
643 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
644 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
645 crw0->erc, crw0->rsid);
646 if (crw1)
647 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
648 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
649 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
650 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800651 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200652 mchk_schid.sch_no = crw0->rsid;
653 if (crw1)
Sebastian Ott8d7bfb42010-12-01 10:08:02 +0100654 mchk_schid.ssid = (crw1->rsid >> 4) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800655
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100656 if (crw0->erc == CRW_ERC_PMOD) {
657 sch = get_subchannel_by_schid(mchk_schid);
658 if (sch) {
659 css_update_ssd_info(sch);
660 put_device(&sch->dev);
661 }
662 }
Cornelia Huckc1156182008-07-14 09:58:46 +0200663 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 * Since we are always presented with IPI in the CRW, we have to
665 * use stsch() to find out if the subchannel in question has come
666 * or gone.
667 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200668 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669}
670
671static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800672css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673{
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200674 struct cpuid cpu_id;
675
Cornelia Huck75784c02008-07-14 09:58:57 +0200676 if (css_general_characteristics.mcss) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800677 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
678 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
679 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680#ifdef CONFIG_SMP
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100681 css->global_pgid.pgid_high.cpu_addr = stap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800683 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684#endif
685 }
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200686 get_cpu_id(&cpu_id);
687 css->global_pgid.cpu_id = cpu_id.ident;
688 css->global_pgid.cpu_model = cpu_id.machine;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800689 css->global_pgid.tod_high = tod_high;
690
691}
692
Cornelia Huck3b793062006-01-06 00:19:26 -0800693static void
694channel_subsystem_release(struct device *dev)
695{
696 struct channel_subsystem *css;
697
698 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800699 mutex_destroy(&css->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200700 if (css->pseudo_subchannel) {
701 /* Implies that it has been generated but never registered. */
702 css_subchannel_release(&css->pseudo_subchannel->dev);
703 css->pseudo_subchannel = NULL;
704 }
Cornelia Huck3b793062006-01-06 00:19:26 -0800705 kfree(css);
706}
707
Cornelia Huck495a5b42006-03-24 03:15:14 -0800708static ssize_t
709css_cm_enable_show(struct device *dev, struct device_attribute *attr,
710 char *buf)
711{
712 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200713 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800714
715 if (!css)
716 return 0;
Michael Ernst8284fb192008-04-17 07:46:01 +0200717 mutex_lock(&css->mutex);
718 ret = sprintf(buf, "%x\n", css->cm_enabled);
719 mutex_unlock(&css->mutex);
720 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800721}
722
723static ssize_t
724css_cm_enable_store(struct device *dev, struct device_attribute *attr,
725 const char *buf, size_t count)
726{
727 struct channel_subsystem *css = to_css(dev);
728 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +0200729 unsigned long val;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800730
Cornelia Huck2f972202008-04-30 13:38:33 +0200731 ret = strict_strtoul(buf, 16, &val);
732 if (ret)
733 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200734 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200735 switch (val) {
736 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800737 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
738 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200739 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800740 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
741 break;
742 default:
743 ret = -EINVAL;
744 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200745 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800746 return ret < 0 ? ret : count;
747}
748
749static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
750
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100751static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800752{
753 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100754 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200755 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800756
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200757 css = channel_subsystems[nr];
758 memset(css, 0, sizeof(struct channel_subsystem));
759 css->pseudo_subchannel =
760 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
761 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100762 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200763 css->pseudo_subchannel->dev.parent = &css->device;
764 css->pseudo_subchannel->dev.release = css_subchannel_release;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200765 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100766 mutex_init(&css->pseudo_subchannel->reg_mutex);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200767 ret = cio_create_sch_lock(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100768 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200769 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100770 return ret;
771 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200772 mutex_init(&css->mutex);
773 css->valid = 1;
774 css->cssid = nr;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200775 dev_set_name(&css->device, "css%x", nr);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200776 css->device.release = channel_subsystem_release;
Heiko Carstens1aae0562013-01-30 09:49:40 +0100777 tod_high = (u32) (get_tod_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200778 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100779 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780}
781
Cornelia Hucka55360d2007-10-12 16:11:20 +0200782static int css_reboot_event(struct notifier_block *this,
783 unsigned long event,
784 void *ptr)
785{
786 int ret, i;
787
788 ret = NOTIFY_DONE;
789 for (i = 0; i <= __MAX_CSSID; i++) {
790 struct channel_subsystem *css;
791
792 css = channel_subsystems[i];
Michael Ernst8284fb192008-04-17 07:46:01 +0200793 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200794 if (css->cm_enabled)
795 if (chsc_secm(css, 0))
796 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200797 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200798 }
799
800 return ret;
801}
802
803static struct notifier_block css_reboot_notifier = {
804 .notifier_call = css_reboot_event,
805};
806
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200808 * Since the css devices are neither on a bus nor have a class
809 * nor have a special device type, we cannot stop/restart channel
810 * path measurements via the normal suspend/resume callbacks, but have
811 * to use notifiers.
812 */
813static int css_power_event(struct notifier_block *this, unsigned long event,
814 void *ptr)
815{
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200816 int ret, i;
817
818 switch (event) {
819 case PM_HIBERNATION_PREPARE:
820 case PM_SUSPEND_PREPARE:
821 ret = NOTIFY_DONE;
822 for (i = 0; i <= __MAX_CSSID; i++) {
823 struct channel_subsystem *css;
824
825 css = channel_subsystems[i];
826 mutex_lock(&css->mutex);
827 if (!css->cm_enabled) {
828 mutex_unlock(&css->mutex);
829 continue;
830 }
Akinobu Mitaf0c077a2011-07-08 20:53:36 +0200831 ret = __chsc_do_secm(css, 0);
832 ret = notifier_from_errno(ret);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200833 mutex_unlock(&css->mutex);
834 }
835 break;
836 case PM_POST_HIBERNATION:
837 case PM_POST_SUSPEND:
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, 1);
849 ret = notifier_from_errno(ret);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200850 mutex_unlock(&css->mutex);
851 }
852 /* search for subchannels, which appeared during hibernation */
853 css_schedule_reprobe();
854 break;
855 default:
856 ret = NOTIFY_DONE;
857 }
858 return ret;
859
860}
861static struct notifier_block css_power_notifier = {
862 .notifier_call = css_power_event,
863};
864
865/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 * Now that the driver core is running, we can setup our channel subsystem.
867 * The struct subchannel's are created during probing (except for the
868 * static console subchannel).
869 */
Sebastian Ott2f176442009-09-22 22:58:33 +0200870static int __init css_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800872 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Sebastian Ott34aec072010-10-25 16:10:28 +0200874 ret = chsc_init();
875 if (ret)
876 return ret;
877
Sebastian Ott34196f82010-10-25 16:10:29 +0200878 chsc_determine_css_characteristics();
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200879 /* Try to enable MSS. */
880 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott818c2722010-04-22 17:17:03 +0200881 if (ret)
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200882 max_ssid = 0;
Sebastian Ott818c2722010-04-22 17:17:03 +0200883 else /* Success. */
884 max_ssid = __MAX_SSID;
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200885
Cornelia Huck4434a382007-07-27 12:29:21 +0200886 ret = slow_subchannel_init();
887 if (ret)
888 goto out;
889
Heiko Carstensf5daba12009-03-26 15:24:01 +0100890 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
Cornelia Huckc1156182008-07-14 09:58:46 +0200891 if (ret)
892 goto out;
893
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 if ((ret = bus_register(&css_bus_type)))
895 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896
Cornelia Hucka28c6942006-01-06 00:19:23 -0800897 /* Setup css structure. */
898 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200899 struct channel_subsystem *css;
900
901 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
902 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800903 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800904 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800905 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200906 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100907 ret = setup_css(i);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200908 if (ret) {
909 kfree(channel_subsystems[i]);
910 goto out_unregister;
911 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200912 ret = device_register(&css->device);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200913 if (ret) {
914 put_device(&css->device);
915 goto out_unregister;
916 }
Cornelia Huck75784c02008-07-14 09:58:57 +0200917 if (css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200918 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200919 &dev_attr_cm_enable);
920 if (ret)
921 goto out_device;
922 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200923 ret = device_register(&css->pseudo_subchannel->dev);
Sebastian Ottc6304932009-09-11 10:28:38 +0200924 if (ret) {
925 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100926 goto out_file;
Sebastian Ottc6304932009-09-11 10:28:38 +0200927 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800928 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200929 ret = register_reboot_notifier(&css_reboot_notifier);
930 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +0200931 goto out_unregister;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200932 ret = register_pm_notifier(&css_power_notifier);
933 if (ret) {
934 unregister_reboot_notifier(&css_reboot_notifier);
935 goto out_unregister;
936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937 css_init_done = 1;
938
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200939 /* Enable default isc for I/O subchannels. */
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200940 isc_register(IO_SCH_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942 return 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100943out_file:
Cornelia Hucka2164b82008-09-09 12:38:57 +0200944 if (css_chsc_characteristics.secm)
945 device_remove_file(&channel_subsystems[i]->device,
946 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200947out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200948 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800949out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800950 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200951 struct channel_subsystem *css;
952
Cornelia Hucka28c6942006-01-06 00:19:23 -0800953 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200954 css = channel_subsystems[i];
955 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200956 css->pseudo_subchannel = NULL;
Cornelia Huck75784c02008-07-14 09:58:57 +0200957 if (css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200958 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800959 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200960 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 bus_unregister(&css_bus_type);
963out:
Sebastian Ott34aec072010-10-25 16:10:28 +0200964 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c82009-09-22 22:58:36 +0200965 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +0200966 chsc_init_cleanup();
Michael Ernste6d5a422008-12-25 13:39:36 +0100967 pr_alert("The CSS device driver initialization failed with "
968 "errno=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 return ret;
970}
971
Sebastian Ott2f176442009-09-22 22:58:33 +0200972static void __init css_bus_cleanup(void)
973{
974 struct channel_subsystem *css;
975 int i;
976
977 for (i = 0; i <= __MAX_CSSID; i++) {
978 css = channel_subsystems[i];
979 device_unregister(&css->pseudo_subchannel->dev);
980 css->pseudo_subchannel = NULL;
981 if (css_chsc_characteristics.secm)
982 device_remove_file(&css->device, &dev_attr_cm_enable);
983 device_unregister(&css->device);
984 }
985 bus_unregister(&css_bus_type);
Sebastian Ott34aec072010-10-25 16:10:28 +0200986 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c82009-09-22 22:58:36 +0200987 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +0200988 chsc_init_cleanup();
Sebastian Ott2f176442009-09-22 22:58:33 +0200989 isc_unregister(IO_SCH_ISC);
990}
991
992static int __init channel_subsystem_init(void)
993{
994 int ret;
995
996 ret = css_bus_init();
997 if (ret)
998 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100999 cio_work_q = create_singlethread_workqueue("cio");
1000 if (!cio_work_q) {
1001 ret = -ENOMEM;
1002 goto out_bus;
1003 }
Sebastian Ott2f176442009-09-22 22:58:33 +02001004 ret = io_subchannel_init();
1005 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001006 goto out_wq;
Sebastian Ott2f176442009-09-22 22:58:33 +02001007
1008 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001009out_wq:
1010 destroy_workqueue(cio_work_q);
1011out_bus:
1012 css_bus_cleanup();
1013 return ret;
Sebastian Ott2f176442009-09-22 22:58:33 +02001014}
1015subsys_initcall(channel_subsystem_init);
1016
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001017static int css_settle(struct device_driver *drv, void *unused)
1018{
1019 struct css_driver *cssdrv = to_cssdriver(drv);
1020
1021 if (cssdrv->settle)
Sebastian Ottb4c70722010-02-26 22:37:27 +01001022 return cssdrv->settle();
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001023 return 0;
1024}
1025
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001026int css_complete_work(void)
Sebastian Ott879acca52010-02-26 22:37:25 +01001027{
1028 int ret;
1029
1030 /* Wait for the evaluation of subchannels to finish. */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001031 ret = wait_event_interruptible(css_eval_wq,
1032 atomic_read(&css_eval_scheduled) == 0);
1033 if (ret)
1034 return -EINTR;
Sebastian Ott879acca52010-02-26 22:37:25 +01001035 flush_workqueue(cio_work_q);
1036 /* Wait for the subchannel type specific initialization to finish */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001037 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
Sebastian Ott879acca52010-02-26 22:37:25 +01001038}
1039
1040
Sebastian Ott2f176442009-09-22 22:58:33 +02001041/*
1042 * Wait for the initialization of devices to finish, to make sure we are
1043 * done with our setup if the search for the root device starts.
1044 */
1045static int __init channel_subsystem_init_sync(void)
1046{
Sebastian Ott703e5c92009-09-22 22:58:38 +02001047 /* Start initial subchannel evaluation. */
1048 css_schedule_eval_all();
Sebastian Ott879acca52010-02-26 22:37:25 +01001049 css_complete_work();
1050 return 0;
Sebastian Ott2f176442009-09-22 22:58:33 +02001051}
1052subsys_initcall_sync(channel_subsystem_init_sync);
1053
Sebastian Ott889ee952010-04-22 17:17:04 +02001054void channel_subsystem_reinit(void)
1055{
Sebastian Ott62da1772010-10-25 16:10:32 +02001056 struct channel_path *chp;
1057 struct chp_id chpid;
1058
Sebastian Ott889ee952010-04-22 17:17:04 +02001059 chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott62da1772010-10-25 16:10:32 +02001060 chp_id_for_each(&chpid) {
1061 chp = chpid_to_chp(chpid);
Peter Oberparleitercce0eac2013-03-11 12:58:18 +01001062 if (chp)
1063 chp_update_desc(chp);
Sebastian Ott62da1772010-10-25 16:10:32 +02001064 }
Sebastian Ott889ee952010-04-22 17:17:04 +02001065}
1066
Sebastian Ott879acca52010-02-26 22:37:25 +01001067#ifdef CONFIG_PROC_FS
1068static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1069 size_t count, loff_t *ppos)
1070{
Sebastian Ottb4c70722010-02-26 22:37:27 +01001071 int ret;
1072
Sebastian Ott879acca52010-02-26 22:37:25 +01001073 /* Handle pending CRW's. */
1074 crw_wait_for_channel_report();
Sebastian Ottb4c70722010-02-26 22:37:27 +01001075 ret = css_complete_work();
1076
1077 return ret ? ret : count;
Sebastian Ott879acca52010-02-26 22:37:25 +01001078}
1079
1080static const struct file_operations cio_settle_proc_fops = {
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +02001081 .open = nonseekable_open,
Sebastian Ott879acca52010-02-26 22:37:25 +01001082 .write = cio_settle_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001083 .llseek = no_llseek,
Sebastian Ott879acca52010-02-26 22:37:25 +01001084};
1085
1086static int __init cio_settle_init(void)
1087{
1088 struct proc_dir_entry *entry;
1089
1090 entry = proc_create("cio_settle", S_IWUSR, NULL,
1091 &cio_settle_proc_fops);
1092 if (!entry)
1093 return -ENOMEM;
1094 return 0;
1095}
1096device_initcall(cio_settle_init);
1097#endif /*CONFIG_PROC_FS*/
1098
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001099int sch_is_pseudo_sch(struct subchannel *sch)
1100{
1101 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1102}
1103
Cornelia Huckf08adc02008-07-14 09:59:03 +02001104static int css_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105{
Cornelia Huck084325d2008-01-26 14:10:38 +01001106 struct subchannel *sch = to_subchannel(dev);
1107 struct css_driver *driver = to_cssdriver(drv);
Cornelia Huckf08adc02008-07-14 09:59:03 +02001108 struct css_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109
Cornelia Huckf08adc02008-07-14 09:59:03 +02001110 for (id = driver->subchannel_type; id->match_flags; id++) {
1111 if (sch->st == id->type)
1112 return 1;
1113 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001114
1115 return 0;
1116}
1117
Cornelia Huck98c13c22008-01-26 14:10:40 +01001118static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001119{
1120 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +01001121 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001122
1123 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +01001124 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001125 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1126 if (ret)
1127 sch->driver = NULL;
1128 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001129}
1130
Cornelia Huck98c13c22008-01-26 14:10:40 +01001131static int css_remove(struct device *dev)
1132{
1133 struct subchannel *sch;
1134 int ret;
1135
1136 sch = to_subchannel(dev);
1137 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1138 sch->driver = NULL;
1139 return ret;
1140}
1141
1142static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001143{
1144 struct subchannel *sch;
1145
1146 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001147 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001148 sch->driver->shutdown(sch);
1149}
1150
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001151static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1152{
1153 struct subchannel *sch = to_subchannel(dev);
1154 int ret;
1155
1156 ret = add_uevent_var(env, "ST=%01X", sch->st);
1157 if (ret)
1158 return ret;
1159 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1160 return ret;
1161}
1162
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001163static int css_pm_prepare(struct device *dev)
1164{
1165 struct subchannel *sch = to_subchannel(dev);
1166 struct css_driver *drv;
1167
1168 if (mutex_is_locked(&sch->reg_mutex))
1169 return -EAGAIN;
1170 if (!sch->dev.driver)
1171 return 0;
1172 drv = to_cssdriver(sch->dev.driver);
1173 /* Notify drivers that they may not register children. */
1174 return drv->prepare ? drv->prepare(sch) : 0;
1175}
1176
1177static void css_pm_complete(struct device *dev)
1178{
1179 struct subchannel *sch = to_subchannel(dev);
1180 struct css_driver *drv;
1181
1182 if (!sch->dev.driver)
1183 return;
1184 drv = to_cssdriver(sch->dev.driver);
1185 if (drv->complete)
1186 drv->complete(sch);
1187}
1188
1189static int css_pm_freeze(struct device *dev)
1190{
1191 struct subchannel *sch = to_subchannel(dev);
1192 struct css_driver *drv;
1193
1194 if (!sch->dev.driver)
1195 return 0;
1196 drv = to_cssdriver(sch->dev.driver);
1197 return drv->freeze ? drv->freeze(sch) : 0;
1198}
1199
1200static int css_pm_thaw(struct device *dev)
1201{
1202 struct subchannel *sch = to_subchannel(dev);
1203 struct css_driver *drv;
1204
1205 if (!sch->dev.driver)
1206 return 0;
1207 drv = to_cssdriver(sch->dev.driver);
1208 return drv->thaw ? drv->thaw(sch) : 0;
1209}
1210
1211static int css_pm_restore(struct device *dev)
1212{
1213 struct subchannel *sch = to_subchannel(dev);
1214 struct css_driver *drv;
1215
Sebastian Otteb4f5d92010-10-25 16:10:33 +02001216 css_update_ssd_info(sch);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001217 if (!sch->dev.driver)
1218 return 0;
1219 drv = to_cssdriver(sch->dev.driver);
1220 return drv->restore ? drv->restore(sch) : 0;
1221}
1222
Alexey Dobriyan47145212009-12-14 18:00:08 -08001223static const struct dev_pm_ops css_pm_ops = {
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001224 .prepare = css_pm_prepare,
1225 .complete = css_pm_complete,
1226 .freeze = css_pm_freeze,
1227 .thaw = css_pm_thaw,
1228 .restore = css_pm_restore,
1229};
1230
Sebastian Ott3041b6a2011-03-15 17:08:31 +01001231static struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001232 .name = "css",
1233 .match = css_bus_match,
1234 .probe = css_probe,
1235 .remove = css_remove,
1236 .shutdown = css_shutdown,
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001237 .uevent = css_uevent,
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001238 .pm = &css_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239};
1240
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001241/**
1242 * css_driver_register - register a css driver
1243 * @cdrv: css driver to register
1244 *
1245 * This is mainly a wrapper around driver_register that sets name
1246 * and bus_type in the embedded struct device_driver correctly.
1247 */
1248int css_driver_register(struct css_driver *cdrv)
1249{
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001250 cdrv->drv.bus = &css_bus_type;
1251 return driver_register(&cdrv->drv);
1252}
1253EXPORT_SYMBOL_GPL(css_driver_register);
1254
1255/**
1256 * css_driver_unregister - unregister a css driver
1257 * @cdrv: css driver to unregister
1258 *
1259 * This is a wrapper around driver_unregister.
1260 */
1261void css_driver_unregister(struct css_driver *cdrv)
1262{
1263 driver_unregister(&cdrv->drv);
1264}
1265EXPORT_SYMBOL_GPL(css_driver_unregister);
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267MODULE_LICENSE("GPL");