blob: c47b25fd3f438b32cc19c5cdeeb2dd63287d56a2 [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 Ott879acca2010-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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140static struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800141css_alloc_subchannel(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142{
143 struct subchannel *sch;
144 int ret;
145
146 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
147 if (sch == NULL)
148 return ERR_PTR(-ENOMEM);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800149 ret = cio_validate_subchannel (sch, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (ret < 0) {
151 kfree(sch);
152 return ERR_PTR(ret);
153 }
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100154 INIT_WORK(&sch->todo_work, css_sch_todo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 return sch;
156}
157
158static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159css_subchannel_release(struct device *dev)
160{
161 struct subchannel *sch;
162
163 sch = to_subchannel(dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100164 if (!cio_is_console(sch->schid)) {
Peter Oberparleiter1da73bc2009-09-11 10:28:16 +0200165 /* Reset intparm to zeroes. */
166 sch->config.intparm = 0;
167 cio_commit_config(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100168 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 kfree(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100170 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Cornelia Huck07c6a332007-07-27 12:29:10 +0200173static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200174{
175 int ret;
176
177 mutex_lock(&sch->reg_mutex);
Sebastian Ott6ee4fec2009-09-11 10:28:25 +0200178 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
179 sch->schid.sch_no);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200180 ret = device_register(&sch->dev);
181 mutex_unlock(&sch->reg_mutex);
182 return ret;
183}
184
Cornelia Huck44a1c192008-07-14 09:58:47 +0200185/**
186 * css_sch_device_unregister - unregister a subchannel
187 * @sch: subchannel to be unregistered
188 */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200189void css_sch_device_unregister(struct subchannel *sch)
190{
191 mutex_lock(&sch->reg_mutex);
Sebastian Ottef60cd12008-07-14 09:59:20 +0200192 if (device_is_registered(&sch->dev))
193 device_unregister(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200194 mutex_unlock(&sch->reg_mutex);
195}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200196EXPORT_SYMBOL_GPL(css_sch_device_unregister);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200197
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100198static void css_sch_todo(struct work_struct *work)
199{
200 struct subchannel *sch;
201 enum sch_todo todo;
202
203 sch = container_of(work, struct subchannel, todo_work);
204 /* Find out todo. */
205 spin_lock_irq(sch->lock);
206 todo = sch->todo;
207 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
208 sch->schid.sch_no, todo);
209 sch->todo = SCH_TODO_NOTHING;
210 spin_unlock_irq(sch->lock);
211 /* Perform todo. */
212 if (todo == SCH_TODO_UNREG)
213 css_sch_device_unregister(sch);
214 /* Release workqueue ref. */
215 put_device(&sch->dev);
216}
217
218/**
219 * css_sched_sch_todo - schedule a subchannel operation
220 * @sch: subchannel
221 * @todo: todo
222 *
223 * Schedule the operation identified by @todo to be performed on the slow path
224 * workqueue. Do nothing if another operation with higher priority is already
225 * scheduled. Needs to be called with subchannel lock held.
226 */
227void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
228{
229 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
230 sch->schid.ssid, sch->schid.sch_no, todo);
231 if (sch->todo >= todo)
232 return;
233 /* Get workqueue ref. */
234 if (!get_device(&sch->dev))
235 return;
236 sch->todo = todo;
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100237 if (!queue_work(cio_work_q, &sch->todo_work)) {
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100238 /* Already queued, release workqueue ref. */
239 put_device(&sch->dev);
240 }
241}
242
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200243static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
244{
245 int i;
246 int mask;
247
248 memset(ssd, 0, sizeof(struct chsc_ssd_info));
249 ssd->path_mask = pmcw->pim;
250 for (i = 0; i < 8; i++) {
251 mask = 0x80 >> i;
252 if (pmcw->pim & mask) {
253 chp_id_init(&ssd->chpid[i]);
254 ssd->chpid[i].id = pmcw->chpid[i];
255 }
256 }
257}
258
259static void ssd_register_chpids(struct chsc_ssd_info *ssd)
260{
261 int i;
262 int mask;
263
264 for (i = 0; i < 8; i++) {
265 mask = 0x80 >> i;
266 if (ssd->path_mask & mask)
267 if (!chp_is_registered(ssd->chpid[i]))
268 chp_new(ssd->chpid[i]);
269 }
270}
271
272void css_update_ssd_info(struct subchannel *sch)
273{
274 int ret;
275
276 if (cio_is_console(sch->schid)) {
277 /* Console is initialized too early for functions requiring
278 * memory allocation. */
279 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
280 } else {
281 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
282 if (ret)
283 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
284 ssd_register_chpids(&sch->ssd_info);
285 }
286}
287
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200288static ssize_t type_show(struct device *dev, struct device_attribute *attr,
289 char *buf)
290{
291 struct subchannel *sch = to_subchannel(dev);
292
293 return sprintf(buf, "%01x\n", sch->st);
294}
295
296static DEVICE_ATTR(type, 0444, type_show, NULL);
297
298static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
299 char *buf)
300{
301 struct subchannel *sch = to_subchannel(dev);
302
303 return sprintf(buf, "css:t%01X\n", sch->st);
304}
305
306static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
307
308static struct attribute *subch_attrs[] = {
309 &dev_attr_type.attr,
310 &dev_attr_modalias.attr,
311 NULL,
312};
313
314static struct attribute_group subch_attr_group = {
315 .attrs = subch_attrs,
316};
317
David Brownella4dbd672009-06-24 10:06:31 -0700318static const struct attribute_group *default_subch_attr_groups[] = {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200319 &subch_attr_group,
320 NULL,
321};
322
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200323static int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
325 int ret;
326
327 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200328 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 sch->dev.bus = &css_bus_type;
330 sch->dev.release = &css_subchannel_release;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200331 sch->dev.groups = default_subch_attr_groups;
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200332 /*
333 * We don't want to generate uevents for I/O subchannels that don't
334 * have a working ccw device behind them since they will be
335 * unregistered before they can be used anyway, so we delay the add
336 * uevent until after device recognition was successful.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200337 * Note that we suppress the uevent for all subchannel types;
338 * the subchannel driver can decide itself when it wants to inform
339 * userspace of its existence.
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200340 */
Ming Leif67f1292009-03-01 21:10:49 +0800341 dev_set_uevent_suppress(&sch->dev, 1);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200342 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200344 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100345 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200346 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
347 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100348 return ret;
349 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200350 if (!sch->driver) {
351 /*
352 * No driver matched. Generate the uevent now so that
353 * a fitting driver module may be loaded based on the
354 * modalias.
355 */
Ming Leif67f1292009-03-01 21:10:49 +0800356 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200357 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 return ret;
360}
361
Cornelia Huckc820de32008-07-14 09:58:45 +0200362int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
364 int ret;
365 struct subchannel *sch;
366
Sebastian Ott703e5c92009-09-22 22:58:38 +0200367 if (cio_is_console(schid))
368 sch = cio_get_console_subchannel();
369 else {
370 sch = css_alloc_subchannel(schid);
371 if (IS_ERR(sch))
372 return PTR_ERR(sch);
373 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 ret = css_register_subchannel(sch);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200375 if (ret) {
376 if (!cio_is_console(schid))
377 put_device(&sch->dev);
378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 return ret;
380}
381
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700382static int
383check_subchannel(struct device * dev, void * data)
384{
385 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800386 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700387
388 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800389 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700390}
391
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800393get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct device *dev;
396
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700397 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200398 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700400 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
402
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100403/**
404 * css_sch_is_valid() - check if a subchannel is valid
405 * @schib: subchannel information block for the subchannel
406 */
407int css_sch_is_valid(struct schib *schib)
408{
409 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
410 return 0;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200411 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
412 return 0;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100413 return 1;
414}
415EXPORT_SYMBOL_GPL(css_sch_is_valid);
416
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200417static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
418{
419 struct schib schib;
420
421 if (!slow) {
422 /* Will be done on the slow path. */
423 return -EAGAIN;
424 }
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100425 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200426 /* Unusable - ignore. */
427 return 0;
428 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100429 CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
430 schid.sch_no);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200431
432 return css_probe_device(schid);
433}
434
Cornelia Huckc820de32008-07-14 09:58:45 +0200435static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
436{
437 int ret = 0;
438
439 if (sch->driver) {
440 if (sch->driver->sch_event)
441 ret = sch->driver->sch_event(sch, slow);
442 else
443 dev_dbg(&sch->dev,
444 "Got subchannel machine check but "
445 "no sch_event handler provided.\n");
446 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100447 if (ret != 0 && ret != -EAGAIN) {
448 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
449 sch->schid.ssid, sch->schid.sch_no, ret);
450 }
Cornelia Huckc820de32008-07-14 09:58:45 +0200451 return ret;
452}
453
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200454static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200455{
456 struct subchannel *sch;
457 int ret;
458
459 sch = get_subchannel_by_schid(schid);
460 if (sch) {
461 ret = css_evaluate_known_subchannel(sch, slow);
462 put_device(&sch->dev);
463 } else
464 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200465 if (ret == -EAGAIN)
466 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467}
468
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200469static struct idset *slow_subchannel_set;
470static spinlock_t slow_subchannel_lock;
Sebastian Ott25530552009-09-22 22:58:34 +0200471static wait_queue_head_t css_eval_wq;
472static atomic_t css_eval_scheduled;
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200473
474static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200476 spin_lock_init(&slow_subchannel_lock);
Sebastian Ott25530552009-09-22 22:58:34 +0200477 atomic_set(&css_eval_scheduled, 0);
478 init_waitqueue_head(&css_eval_wq);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200479 slow_subchannel_set = idset_sch_new();
480 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200481 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200482 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200484 return 0;
485}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100487static int slow_eval_known_fn(struct subchannel *sch, void *data)
488{
489 int eval;
490 int rc;
491
492 spin_lock_irq(&slow_subchannel_lock);
493 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
494 idset_sch_del(slow_subchannel_set, sch->schid);
495 spin_unlock_irq(&slow_subchannel_lock);
496 if (eval) {
497 rc = css_evaluate_known_subchannel(sch, 1);
498 if (rc == -EAGAIN)
499 css_schedule_eval(sch->schid);
500 }
501 return 0;
502}
503
504static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
505{
506 int eval;
507 int rc = 0;
508
509 spin_lock_irq(&slow_subchannel_lock);
510 eval = idset_sch_contains(slow_subchannel_set, schid);
511 idset_sch_del(slow_subchannel_set, schid);
512 spin_unlock_irq(&slow_subchannel_lock);
513 if (eval) {
514 rc = css_evaluate_new_subchannel(schid, 1);
515 switch (rc) {
516 case -EAGAIN:
517 css_schedule_eval(schid);
518 rc = 0;
519 break;
520 case -ENXIO:
521 case -ENOMEM:
522 case -EIO:
523 /* These should abort looping */
524 break;
525 default:
526 rc = 0;
527 }
528 }
529 return rc;
530}
531
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200532static void css_slow_path_func(struct work_struct *unused)
533{
Sebastian Ott25530552009-09-22 22:58:34 +0200534 unsigned long flags;
535
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200536 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100537 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
538 NULL);
Sebastian Ott25530552009-09-22 22:58:34 +0200539 spin_lock_irqsave(&slow_subchannel_lock, flags);
540 if (idset_is_empty(slow_subchannel_set)) {
541 atomic_set(&css_eval_scheduled, 0);
542 wake_up(&css_eval_wq);
543 }
544 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545}
546
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200547static DECLARE_WORK(slow_path_work, css_slow_path_func);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100548struct workqueue_struct *cio_work_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200550void css_schedule_eval(struct subchannel_id schid)
551{
552 unsigned long flags;
553
554 spin_lock_irqsave(&slow_subchannel_lock, flags);
555 idset_sch_add(slow_subchannel_set, schid);
Sebastian Ott25530552009-09-22 22:58:34 +0200556 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100557 queue_work(cio_work_q, &slow_path_work);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200558 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
559}
560
561void css_schedule_eval_all(void)
562{
563 unsigned long flags;
564
565 spin_lock_irqsave(&slow_subchannel_lock, flags);
566 idset_fill(slow_subchannel_set);
Sebastian Ott25530552009-09-22 22:58:34 +0200567 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100568 queue_work(cio_work_q, &slow_path_work);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200569 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
570}
571
Sebastian Ott703e5c92009-09-22 22:58:38 +0200572static int __unset_registered(struct device *dev, void *data)
573{
574 struct idset *set = data;
575 struct subchannel *sch = to_subchannel(dev);
576
577 idset_sch_del(set, sch->schid);
578 return 0;
579}
580
Sebastian Otta8481c22010-10-25 16:10:23 +0200581static void css_schedule_eval_all_unreg(void)
Sebastian Ott703e5c92009-09-22 22:58:38 +0200582{
583 unsigned long flags;
584 struct idset *unreg_set;
585
586 /* Find unregistered subchannels. */
587 unreg_set = idset_sch_new();
588 if (!unreg_set) {
589 /* Fallback. */
590 css_schedule_eval_all();
591 return;
592 }
593 idset_fill(unreg_set);
594 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
595 /* Apply to slow_subchannel_set. */
596 spin_lock_irqsave(&slow_subchannel_lock, flags);
597 idset_add_set(slow_subchannel_set, unreg_set);
598 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100599 queue_work(cio_work_q, &slow_path_work);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200600 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
601 idset_free(unreg_set);
602}
603
Cornelia Huck22806dc2008-04-17 07:45:59 +0200604void css_wait_for_slow_path(void)
605{
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100606 flush_workqueue(cio_work_q);
Cornelia Huck22806dc2008-04-17 07:45:59 +0200607}
608
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200609/* Schedule reprobing of all unregistered subchannels. */
610void css_schedule_reprobe(void)
611{
Sebastian Ott703e5c92009-09-22 22:58:38 +0200612 css_schedule_eval_all_unreg();
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200613}
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200614EXPORT_SYMBOL_GPL(css_schedule_reprobe);
615
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 * Called from the machine check handler for subchannel report words.
618 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200619static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800621 struct subchannel_id mchk_schid;
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100622 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Cornelia Huckc1156182008-07-14 09:58:46 +0200624 if (overflow) {
625 css_schedule_eval_all();
626 return;
627 }
628 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
629 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
630 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
631 crw0->erc, crw0->rsid);
632 if (crw1)
633 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
634 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
635 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
636 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800637 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200638 mchk_schid.sch_no = crw0->rsid;
639 if (crw1)
Sebastian Ott8d7bfb42010-12-01 10:08:02 +0100640 mchk_schid.ssid = (crw1->rsid >> 4) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800641
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100642 if (crw0->erc == CRW_ERC_PMOD) {
643 sch = get_subchannel_by_schid(mchk_schid);
644 if (sch) {
645 css_update_ssd_info(sch);
646 put_device(&sch->dev);
647 }
648 }
Cornelia Huckc1156182008-07-14 09:58:46 +0200649 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 * Since we are always presented with IPI in the CRW, we have to
651 * use stsch() to find out if the subchannel in question has come
652 * or gone.
653 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200654 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655}
656
657static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800658css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200660 struct cpuid cpu_id;
661
Cornelia Huck75784c02008-07-14 09:58:57 +0200662 if (css_general_characteristics.mcss) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800663 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
664 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
665 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666#ifdef CONFIG_SMP
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100667 css->global_pgid.pgid_high.cpu_addr = stap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800669 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670#endif
671 }
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200672 get_cpu_id(&cpu_id);
673 css->global_pgid.cpu_id = cpu_id.ident;
674 css->global_pgid.cpu_model = cpu_id.machine;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800675 css->global_pgid.tod_high = tod_high;
676
677}
678
Cornelia Huck3b793062006-01-06 00:19:26 -0800679static void
680channel_subsystem_release(struct device *dev)
681{
682 struct channel_subsystem *css;
683
684 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800685 mutex_destroy(&css->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200686 if (css->pseudo_subchannel) {
687 /* Implies that it has been generated but never registered. */
688 css_subchannel_release(&css->pseudo_subchannel->dev);
689 css->pseudo_subchannel = NULL;
690 }
Cornelia Huck3b793062006-01-06 00:19:26 -0800691 kfree(css);
692}
693
Cornelia Huck495a5b42006-03-24 03:15:14 -0800694static ssize_t
695css_cm_enable_show(struct device *dev, struct device_attribute *attr,
696 char *buf)
697{
698 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200699 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800700
701 if (!css)
702 return 0;
Michael Ernst8284fb192008-04-17 07:46:01 +0200703 mutex_lock(&css->mutex);
704 ret = sprintf(buf, "%x\n", css->cm_enabled);
705 mutex_unlock(&css->mutex);
706 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800707}
708
709static ssize_t
710css_cm_enable_store(struct device *dev, struct device_attribute *attr,
711 const char *buf, size_t count)
712{
713 struct channel_subsystem *css = to_css(dev);
714 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +0200715 unsigned long val;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800716
Cornelia Huck2f972202008-04-30 13:38:33 +0200717 ret = strict_strtoul(buf, 16, &val);
718 if (ret)
719 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200720 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200721 switch (val) {
722 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800723 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
724 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200725 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800726 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
727 break;
728 default:
729 ret = -EINVAL;
730 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200731 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800732 return ret < 0 ? ret : count;
733}
734
735static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
736
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100737static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800738{
739 u32 tod_high;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100740 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200741 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800742
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200743 css = channel_subsystems[nr];
744 memset(css, 0, sizeof(struct channel_subsystem));
745 css->pseudo_subchannel =
746 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
747 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100748 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200749 css->pseudo_subchannel->dev.parent = &css->device;
750 css->pseudo_subchannel->dev.release = css_subchannel_release;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200751 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100752 mutex_init(&css->pseudo_subchannel->reg_mutex);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200753 ret = cio_create_sch_lock(css->pseudo_subchannel);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100754 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200755 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100756 return ret;
757 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200758 mutex_init(&css->mutex);
759 css->valid = 1;
760 css->cssid = nr;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200761 dev_set_name(&css->device, "css%x", nr);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200762 css->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800763 tod_high = (u32) (get_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200764 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100765 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766}
767
Cornelia Hucka55360d2007-10-12 16:11:20 +0200768static int css_reboot_event(struct notifier_block *this,
769 unsigned long event,
770 void *ptr)
771{
772 int ret, i;
773
774 ret = NOTIFY_DONE;
775 for (i = 0; i <= __MAX_CSSID; i++) {
776 struct channel_subsystem *css;
777
778 css = channel_subsystems[i];
Michael Ernst8284fb192008-04-17 07:46:01 +0200779 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200780 if (css->cm_enabled)
781 if (chsc_secm(css, 0))
782 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200783 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200784 }
785
786 return ret;
787}
788
789static struct notifier_block css_reboot_notifier = {
790 .notifier_call = css_reboot_event,
791};
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200794 * Since the css devices are neither on a bus nor have a class
795 * nor have a special device type, we cannot stop/restart channel
796 * path measurements via the normal suspend/resume callbacks, but have
797 * to use notifiers.
798 */
799static int css_power_event(struct notifier_block *this, unsigned long event,
800 void *ptr)
801{
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200802 int ret, i;
803
804 switch (event) {
805 case PM_HIBERNATION_PREPARE:
806 case PM_SUSPEND_PREPARE:
807 ret = NOTIFY_DONE;
808 for (i = 0; i <= __MAX_CSSID; i++) {
809 struct channel_subsystem *css;
810
811 css = channel_subsystems[i];
812 mutex_lock(&css->mutex);
813 if (!css->cm_enabled) {
814 mutex_unlock(&css->mutex);
815 continue;
816 }
Sebastian Ott34196f82010-10-25 16:10:29 +0200817 if (__chsc_do_secm(css, 0))
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200818 ret = NOTIFY_BAD;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200819 mutex_unlock(&css->mutex);
820 }
821 break;
822 case PM_POST_HIBERNATION:
823 case PM_POST_SUSPEND:
824 ret = NOTIFY_DONE;
825 for (i = 0; i <= __MAX_CSSID; i++) {
826 struct channel_subsystem *css;
827
828 css = channel_subsystems[i];
829 mutex_lock(&css->mutex);
830 if (!css->cm_enabled) {
831 mutex_unlock(&css->mutex);
832 continue;
833 }
Sebastian Ott34196f82010-10-25 16:10:29 +0200834 if (__chsc_do_secm(css, 1))
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200835 ret = NOTIFY_BAD;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200836 mutex_unlock(&css->mutex);
837 }
838 /* search for subchannels, which appeared during hibernation */
839 css_schedule_reprobe();
840 break;
841 default:
842 ret = NOTIFY_DONE;
843 }
844 return ret;
845
846}
847static struct notifier_block css_power_notifier = {
848 .notifier_call = css_power_event,
849};
850
851/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 * Now that the driver core is running, we can setup our channel subsystem.
853 * The struct subchannel's are created during probing (except for the
854 * static console subchannel).
855 */
Sebastian Ott2f176442009-09-22 22:58:33 +0200856static int __init css_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800858 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Sebastian Ott34aec072010-10-25 16:10:28 +0200860 ret = chsc_init();
861 if (ret)
862 return ret;
863
Sebastian Ott34196f82010-10-25 16:10:29 +0200864 chsc_determine_css_characteristics();
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200865 /* Try to enable MSS. */
866 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott818c2722010-04-22 17:17:03 +0200867 if (ret)
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200868 max_ssid = 0;
Sebastian Ott818c2722010-04-22 17:17:03 +0200869 else /* Success. */
870 max_ssid = __MAX_SSID;
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200871
Cornelia Huck4434a382007-07-27 12:29:21 +0200872 ret = slow_subchannel_init();
873 if (ret)
874 goto out;
875
Heiko Carstensf5daba12009-03-26 15:24:01 +0100876 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
Cornelia Huckc1156182008-07-14 09:58:46 +0200877 if (ret)
878 goto out;
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 if ((ret = bus_register(&css_bus_type)))
881 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Cornelia Hucka28c6942006-01-06 00:19:23 -0800883 /* Setup css structure. */
884 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200885 struct channel_subsystem *css;
886
887 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
888 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800889 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800890 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800891 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200892 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100893 ret = setup_css(i);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200894 if (ret) {
895 kfree(channel_subsystems[i]);
896 goto out_unregister;
897 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200898 ret = device_register(&css->device);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200899 if (ret) {
900 put_device(&css->device);
901 goto out_unregister;
902 }
Cornelia Huck75784c02008-07-14 09:58:57 +0200903 if (css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200904 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200905 &dev_attr_cm_enable);
906 if (ret)
907 goto out_device;
908 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200909 ret = device_register(&css->pseudo_subchannel->dev);
Sebastian Ottc6304932009-09-11 10:28:38 +0200910 if (ret) {
911 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100912 goto out_file;
Sebastian Ottc6304932009-09-11 10:28:38 +0200913 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800914 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200915 ret = register_reboot_notifier(&css_reboot_notifier);
916 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +0200917 goto out_unregister;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200918 ret = register_pm_notifier(&css_power_notifier);
919 if (ret) {
920 unregister_reboot_notifier(&css_reboot_notifier);
921 goto out_unregister;
922 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 css_init_done = 1;
924
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200925 /* Enable default isc for I/O subchannels. */
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200926 isc_register(IO_SCH_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 return 0;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100929out_file:
Cornelia Hucka2164b82008-09-09 12:38:57 +0200930 if (css_chsc_characteristics.secm)
931 device_remove_file(&channel_subsystems[i]->device,
932 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200933out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200934 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800935out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800936 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200937 struct channel_subsystem *css;
938
Cornelia Hucka28c6942006-01-06 00:19:23 -0800939 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200940 css = channel_subsystems[i];
941 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200942 css->pseudo_subchannel = NULL;
Cornelia Huck75784c02008-07-14 09:58:57 +0200943 if (css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200944 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800945 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200946 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800947 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 bus_unregister(&css_bus_type);
949out:
Sebastian Ott34aec072010-10-25 16:10:28 +0200950 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c2009-09-22 22:58:36 +0200951 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +0200952 chsc_init_cleanup();
Michael Ernste6d5a422008-12-25 13:39:36 +0100953 pr_alert("The CSS device driver initialization failed with "
954 "errno=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 return ret;
956}
957
Sebastian Ott2f176442009-09-22 22:58:33 +0200958static void __init css_bus_cleanup(void)
959{
960 struct channel_subsystem *css;
961 int i;
962
963 for (i = 0; i <= __MAX_CSSID; i++) {
964 css = channel_subsystems[i];
965 device_unregister(&css->pseudo_subchannel->dev);
966 css->pseudo_subchannel = NULL;
967 if (css_chsc_characteristics.secm)
968 device_remove_file(&css->device, &dev_attr_cm_enable);
969 device_unregister(&css->device);
970 }
971 bus_unregister(&css_bus_type);
Sebastian Ott34aec072010-10-25 16:10:28 +0200972 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c2009-09-22 22:58:36 +0200973 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +0200974 chsc_init_cleanup();
Sebastian Ott2f176442009-09-22 22:58:33 +0200975 isc_unregister(IO_SCH_ISC);
976}
977
978static int __init channel_subsystem_init(void)
979{
980 int ret;
981
982 ret = css_bus_init();
983 if (ret)
984 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100985 cio_work_q = create_singlethread_workqueue("cio");
986 if (!cio_work_q) {
987 ret = -ENOMEM;
988 goto out_bus;
989 }
Sebastian Ott2f176442009-09-22 22:58:33 +0200990 ret = io_subchannel_init();
991 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100992 goto out_wq;
Sebastian Ott2f176442009-09-22 22:58:33 +0200993
994 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100995out_wq:
996 destroy_workqueue(cio_work_q);
997out_bus:
998 css_bus_cleanup();
999 return ret;
Sebastian Ott2f176442009-09-22 22:58:33 +02001000}
1001subsys_initcall(channel_subsystem_init);
1002
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001003static int css_settle(struct device_driver *drv, void *unused)
1004{
1005 struct css_driver *cssdrv = to_cssdriver(drv);
1006
1007 if (cssdrv->settle)
Sebastian Ottb4c70722010-02-26 22:37:27 +01001008 return cssdrv->settle();
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001009 return 0;
1010}
1011
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001012int css_complete_work(void)
Sebastian Ott879acca2010-02-26 22:37:25 +01001013{
1014 int ret;
1015
1016 /* Wait for the evaluation of subchannels to finish. */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001017 ret = wait_event_interruptible(css_eval_wq,
1018 atomic_read(&css_eval_scheduled) == 0);
1019 if (ret)
1020 return -EINTR;
Sebastian Ott879acca2010-02-26 22:37:25 +01001021 flush_workqueue(cio_work_q);
1022 /* Wait for the subchannel type specific initialization to finish */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001023 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
Sebastian Ott879acca2010-02-26 22:37:25 +01001024}
1025
1026
Sebastian Ott2f176442009-09-22 22:58:33 +02001027/*
1028 * Wait for the initialization of devices to finish, to make sure we are
1029 * done with our setup if the search for the root device starts.
1030 */
1031static int __init channel_subsystem_init_sync(void)
1032{
Sebastian Ott703e5c92009-09-22 22:58:38 +02001033 /* Start initial subchannel evaluation. */
1034 css_schedule_eval_all();
Sebastian Ott879acca2010-02-26 22:37:25 +01001035 css_complete_work();
1036 return 0;
Sebastian Ott2f176442009-09-22 22:58:33 +02001037}
1038subsys_initcall_sync(channel_subsystem_init_sync);
1039
Sebastian Ott889ee952010-04-22 17:17:04 +02001040void channel_subsystem_reinit(void)
1041{
Sebastian Ott62da1772010-10-25 16:10:32 +02001042 struct channel_path *chp;
1043 struct chp_id chpid;
1044
Sebastian Ott889ee952010-04-22 17:17:04 +02001045 chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott62da1772010-10-25 16:10:32 +02001046 chp_id_for_each(&chpid) {
1047 chp = chpid_to_chp(chpid);
1048 if (!chp)
1049 continue;
1050 chsc_determine_base_channel_path_desc(chpid, &chp->desc);
1051 }
Sebastian Ott889ee952010-04-22 17:17:04 +02001052}
1053
Sebastian Ott879acca2010-02-26 22:37:25 +01001054#ifdef CONFIG_PROC_FS
1055static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1056 size_t count, loff_t *ppos)
1057{
Sebastian Ottb4c70722010-02-26 22:37:27 +01001058 int ret;
1059
Sebastian Ott879acca2010-02-26 22:37:25 +01001060 /* Handle pending CRW's. */
1061 crw_wait_for_channel_report();
Sebastian Ottb4c70722010-02-26 22:37:27 +01001062 ret = css_complete_work();
1063
1064 return ret ? ret : count;
Sebastian Ott879acca2010-02-26 22:37:25 +01001065}
1066
1067static const struct file_operations cio_settle_proc_fops = {
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +02001068 .open = nonseekable_open,
Sebastian Ott879acca2010-02-26 22:37:25 +01001069 .write = cio_settle_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001070 .llseek = no_llseek,
Sebastian Ott879acca2010-02-26 22:37:25 +01001071};
1072
1073static int __init cio_settle_init(void)
1074{
1075 struct proc_dir_entry *entry;
1076
1077 entry = proc_create("cio_settle", S_IWUSR, NULL,
1078 &cio_settle_proc_fops);
1079 if (!entry)
1080 return -ENOMEM;
1081 return 0;
1082}
1083device_initcall(cio_settle_init);
1084#endif /*CONFIG_PROC_FS*/
1085
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001086int sch_is_pseudo_sch(struct subchannel *sch)
1087{
1088 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1089}
1090
Cornelia Huckf08adc02008-07-14 09:59:03 +02001091static int css_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092{
Cornelia Huck084325d2008-01-26 14:10:38 +01001093 struct subchannel *sch = to_subchannel(dev);
1094 struct css_driver *driver = to_cssdriver(drv);
Cornelia Huckf08adc02008-07-14 09:59:03 +02001095 struct css_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
Cornelia Huckf08adc02008-07-14 09:59:03 +02001097 for (id = driver->subchannel_type; id->match_flags; id++) {
1098 if (sch->st == id->type)
1099 return 1;
1100 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101
1102 return 0;
1103}
1104
Cornelia Huck98c13c22008-01-26 14:10:40 +01001105static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001106{
1107 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +01001108 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001109
1110 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +01001111 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001112 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1113 if (ret)
1114 sch->driver = NULL;
1115 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001116}
1117
Cornelia Huck98c13c22008-01-26 14:10:40 +01001118static int css_remove(struct device *dev)
1119{
1120 struct subchannel *sch;
1121 int ret;
1122
1123 sch = to_subchannel(dev);
1124 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1125 sch->driver = NULL;
1126 return ret;
1127}
1128
1129static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001130{
1131 struct subchannel *sch;
1132
1133 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001134 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001135 sch->driver->shutdown(sch);
1136}
1137
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001138static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1139{
1140 struct subchannel *sch = to_subchannel(dev);
1141 int ret;
1142
1143 ret = add_uevent_var(env, "ST=%01X", sch->st);
1144 if (ret)
1145 return ret;
1146 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1147 return ret;
1148}
1149
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001150static int css_pm_prepare(struct device *dev)
1151{
1152 struct subchannel *sch = to_subchannel(dev);
1153 struct css_driver *drv;
1154
1155 if (mutex_is_locked(&sch->reg_mutex))
1156 return -EAGAIN;
1157 if (!sch->dev.driver)
1158 return 0;
1159 drv = to_cssdriver(sch->dev.driver);
1160 /* Notify drivers that they may not register children. */
1161 return drv->prepare ? drv->prepare(sch) : 0;
1162}
1163
1164static void css_pm_complete(struct device *dev)
1165{
1166 struct subchannel *sch = to_subchannel(dev);
1167 struct css_driver *drv;
1168
1169 if (!sch->dev.driver)
1170 return;
1171 drv = to_cssdriver(sch->dev.driver);
1172 if (drv->complete)
1173 drv->complete(sch);
1174}
1175
1176static int css_pm_freeze(struct device *dev)
1177{
1178 struct subchannel *sch = to_subchannel(dev);
1179 struct css_driver *drv;
1180
1181 if (!sch->dev.driver)
1182 return 0;
1183 drv = to_cssdriver(sch->dev.driver);
1184 return drv->freeze ? drv->freeze(sch) : 0;
1185}
1186
1187static int css_pm_thaw(struct device *dev)
1188{
1189 struct subchannel *sch = to_subchannel(dev);
1190 struct css_driver *drv;
1191
1192 if (!sch->dev.driver)
1193 return 0;
1194 drv = to_cssdriver(sch->dev.driver);
1195 return drv->thaw ? drv->thaw(sch) : 0;
1196}
1197
1198static int css_pm_restore(struct device *dev)
1199{
1200 struct subchannel *sch = to_subchannel(dev);
1201 struct css_driver *drv;
1202
Sebastian Otteb4f5d92010-10-25 16:10:33 +02001203 css_update_ssd_info(sch);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001204 if (!sch->dev.driver)
1205 return 0;
1206 drv = to_cssdriver(sch->dev.driver);
1207 return drv->restore ? drv->restore(sch) : 0;
1208}
1209
Alexey Dobriyan47145212009-12-14 18:00:08 -08001210static const struct dev_pm_ops css_pm_ops = {
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001211 .prepare = css_pm_prepare,
1212 .complete = css_pm_complete,
1213 .freeze = css_pm_freeze,
1214 .thaw = css_pm_thaw,
1215 .restore = css_pm_restore,
1216};
1217
Sebastian Ott3041b6a2011-03-15 17:08:31 +01001218static struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001219 .name = "css",
1220 .match = css_bus_match,
1221 .probe = css_probe,
1222 .remove = css_remove,
1223 .shutdown = css_shutdown,
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001224 .uevent = css_uevent,
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001225 .pm = &css_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226};
1227
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001228/**
1229 * css_driver_register - register a css driver
1230 * @cdrv: css driver to register
1231 *
1232 * This is mainly a wrapper around driver_register that sets name
1233 * and bus_type in the embedded struct device_driver correctly.
1234 */
1235int css_driver_register(struct css_driver *cdrv)
1236{
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001237 cdrv->drv.bus = &css_bus_type;
1238 return driver_register(&cdrv->drv);
1239}
1240EXPORT_SYMBOL_GPL(css_driver_register);
1241
1242/**
1243 * css_driver_unregister - unregister a css driver
1244 * @cdrv: css driver to unregister
1245 *
1246 * This is a wrapper around driver_unregister.
1247 */
1248void css_driver_unregister(struct css_driver *cdrv)
1249{
1250 driver_unregister(&cdrv->drv);
1251}
1252EXPORT_SYMBOL_GPL(css_driver_unregister);
1253
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254MODULE_LICENSE("GPL");