blob: 2769da54f2b96ebf9f26f86ff76c049ff71c5e82 [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 Ottdcbd16d2009-06-16 10:30:22 +02004 * Copyright IBM Corp. 2002, 2009
5 *
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];
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
Heiko Carstens4d284ca2007-02-05 21:18:53 +010039int
Cornelia Huckf97a56f2006-01-06 00:19:22 -080040for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
41{
42 struct subchannel_id schid;
43 int ret;
44
45 init_subchannel_id(&schid);
46 ret = -ENODEV;
47 do {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080048 do {
49 ret = fn(schid, data);
50 if (ret)
51 break;
52 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
53 schid.sch_no = 0;
54 } while (schid.ssid++ < max_ssid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080055 return ret;
56}
57
Peter Oberparleitere82a1562008-01-26 14:10:48 +010058struct cb_data {
59 void *data;
60 struct idset *set;
61 int (*fn_known_sch)(struct subchannel *, void *);
62 int (*fn_unknown_sch)(struct subchannel_id, void *);
63};
64
65static int call_fn_known_sch(struct device *dev, void *data)
66{
67 struct subchannel *sch = to_subchannel(dev);
68 struct cb_data *cb = data;
69 int rc = 0;
70
71 idset_sch_del(cb->set, sch->schid);
72 if (cb->fn_known_sch)
73 rc = cb->fn_known_sch(sch, cb->data);
74 return rc;
75}
76
77static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
78{
79 struct cb_data *cb = data;
80 int rc = 0;
81
82 if (idset_sch_contains(cb->set, schid))
83 rc = cb->fn_unknown_sch(schid, cb->data);
84 return rc;
85}
86
Sebastian Ott90ac24a2009-03-26 15:24:11 +010087static int call_fn_all_sch(struct subchannel_id schid, void *data)
88{
89 struct cb_data *cb = data;
90 struct subchannel *sch;
91 int rc = 0;
92
93 sch = get_subchannel_by_schid(schid);
94 if (sch) {
95 if (cb->fn_known_sch)
96 rc = cb->fn_known_sch(sch, cb->data);
97 put_device(&sch->dev);
98 } else {
99 if (cb->fn_unknown_sch)
100 rc = cb->fn_unknown_sch(schid, cb->data);
101 }
102
103 return rc;
104}
105
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100106int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
107 int (*fn_unknown)(struct subchannel_id,
108 void *), void *data)
109{
110 struct cb_data cb;
111 int rc;
112
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100113 cb.data = data;
114 cb.fn_known_sch = fn_known;
115 cb.fn_unknown_sch = fn_unknown;
Sebastian Ott90ac24a2009-03-26 15:24:11 +0100116
117 cb.set = idset_sch_new();
118 if (!cb.set)
119 /* fall back to brute force scanning in case of oom */
120 return for_each_subchannel(call_fn_all_sch, &cb);
121
122 idset_fill(cb.set);
123
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100124 /* Process registered subchannels. */
125 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
126 if (rc)
127 goto out;
128 /* Process unregistered subchannels. */
129 if (fn_unknown)
130 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
131out:
132 idset_free(cb.set);
133
134 return rc;
135}
136
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100137static void css_sch_todo(struct work_struct *work);
138
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139static struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800140css_alloc_subchannel(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
142 struct subchannel *sch;
143 int ret;
144
145 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
146 if (sch == NULL)
147 return ERR_PTR(-ENOMEM);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800148 ret = cio_validate_subchannel (sch, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 if (ret < 0) {
150 kfree(sch);
151 return ERR_PTR(ret);
152 }
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100153 INIT_WORK(&sch->todo_work, css_sch_todo);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 return sch;
155}
156
157static void
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158css_subchannel_release(struct device *dev)
159{
160 struct subchannel *sch;
161
162 sch = to_subchannel(dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100163 if (!cio_is_console(sch->schid)) {
Peter Oberparleiter1da73bc2009-09-11 10:28:16 +0200164 /* Reset intparm to zeroes. */
165 sch->config.intparm = 0;
166 cio_commit_config(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100167 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 kfree(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100169 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Cornelia Huck07c6a332007-07-27 12:29:10 +0200172static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200173{
174 int ret;
175
176 mutex_lock(&sch->reg_mutex);
Sebastian Ott6ee4fec2009-09-11 10:28:25 +0200177 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
178 sch->schid.sch_no);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200179 ret = device_register(&sch->dev);
180 mutex_unlock(&sch->reg_mutex);
181 return ret;
182}
183
Cornelia Huck44a1c192008-07-14 09:58:47 +0200184/**
185 * css_sch_device_unregister - unregister a subchannel
186 * @sch: subchannel to be unregistered
187 */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200188void css_sch_device_unregister(struct subchannel *sch)
189{
190 mutex_lock(&sch->reg_mutex);
Sebastian Ottef60cd12008-07-14 09:59:20 +0200191 if (device_is_registered(&sch->dev))
192 device_unregister(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200193 mutex_unlock(&sch->reg_mutex);
194}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200195EXPORT_SYMBOL_GPL(css_sch_device_unregister);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200196
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100197static void css_sch_todo(struct work_struct *work)
198{
199 struct subchannel *sch;
200 enum sch_todo todo;
201
202 sch = container_of(work, struct subchannel, todo_work);
203 /* Find out todo. */
204 spin_lock_irq(sch->lock);
205 todo = sch->todo;
206 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
207 sch->schid.sch_no, todo);
208 sch->todo = SCH_TODO_NOTHING;
209 spin_unlock_irq(sch->lock);
210 /* Perform todo. */
211 if (todo == SCH_TODO_UNREG)
212 css_sch_device_unregister(sch);
213 /* Release workqueue ref. */
214 put_device(&sch->dev);
215}
216
217/**
218 * css_sched_sch_todo - schedule a subchannel operation
219 * @sch: subchannel
220 * @todo: todo
221 *
222 * Schedule the operation identified by @todo to be performed on the slow path
223 * workqueue. Do nothing if another operation with higher priority is already
224 * scheduled. Needs to be called with subchannel lock held.
225 */
226void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
227{
228 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
229 sch->schid.ssid, sch->schid.sch_no, todo);
230 if (sch->todo >= todo)
231 return;
232 /* Get workqueue ref. */
233 if (!get_device(&sch->dev))
234 return;
235 sch->todo = todo;
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100236 if (!queue_work(cio_work_q, &sch->todo_work)) {
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100237 /* Already queued, release workqueue ref. */
238 put_device(&sch->dev);
239 }
240}
241
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200242static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
243{
244 int i;
245 int mask;
246
247 memset(ssd, 0, sizeof(struct chsc_ssd_info));
248 ssd->path_mask = pmcw->pim;
249 for (i = 0; i < 8; i++) {
250 mask = 0x80 >> i;
251 if (pmcw->pim & mask) {
252 chp_id_init(&ssd->chpid[i]);
253 ssd->chpid[i].id = pmcw->chpid[i];
254 }
255 }
256}
257
258static void ssd_register_chpids(struct chsc_ssd_info *ssd)
259{
260 int i;
261 int mask;
262
263 for (i = 0; i < 8; i++) {
264 mask = 0x80 >> i;
265 if (ssd->path_mask & mask)
266 if (!chp_is_registered(ssd->chpid[i]))
267 chp_new(ssd->chpid[i]);
268 }
269}
270
271void css_update_ssd_info(struct subchannel *sch)
272{
273 int ret;
274
275 if (cio_is_console(sch->schid)) {
276 /* Console is initialized too early for functions requiring
277 * memory allocation. */
278 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
279 } else {
280 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
281 if (ret)
282 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
283 ssd_register_chpids(&sch->ssd_info);
284 }
285}
286
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200287static ssize_t type_show(struct device *dev, struct device_attribute *attr,
288 char *buf)
289{
290 struct subchannel *sch = to_subchannel(dev);
291
292 return sprintf(buf, "%01x\n", sch->st);
293}
294
295static DEVICE_ATTR(type, 0444, type_show, NULL);
296
297static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
298 char *buf)
299{
300 struct subchannel *sch = to_subchannel(dev);
301
302 return sprintf(buf, "css:t%01X\n", sch->st);
303}
304
305static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
306
307static struct attribute *subch_attrs[] = {
308 &dev_attr_type.attr,
309 &dev_attr_modalias.attr,
310 NULL,
311};
312
313static struct attribute_group subch_attr_group = {
314 .attrs = subch_attrs,
315};
316
David Brownella4dbd672009-06-24 10:06:31 -0700317static const struct attribute_group *default_subch_attr_groups[] = {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200318 &subch_attr_group,
319 NULL,
320};
321
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200322static int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323{
324 int ret;
325
326 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200327 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 sch->dev.bus = &css_bus_type;
329 sch->dev.release = &css_subchannel_release;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200330 sch->dev.groups = default_subch_attr_groups;
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200331 /*
332 * We don't want to generate uevents for I/O subchannels that don't
333 * have a working ccw device behind them since they will be
334 * unregistered before they can be used anyway, so we delay the add
335 * uevent until after device recognition was successful.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200336 * Note that we suppress the uevent for all subchannel types;
337 * the subchannel driver can decide itself when it wants to inform
338 * userspace of its existence.
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200339 */
Ming Leif67f1292009-03-01 21:10:49 +0800340 dev_set_uevent_suppress(&sch->dev, 1);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200341 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200343 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100344 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200345 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
346 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100347 return ret;
348 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200349 if (!sch->driver) {
350 /*
351 * No driver matched. Generate the uevent now so that
352 * a fitting driver module may be loaded based on the
353 * modalias.
354 */
Ming Leif67f1292009-03-01 21:10:49 +0800355 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200356 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
357 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 return ret;
359}
360
Cornelia Huckc820de32008-07-14 09:58:45 +0200361int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
363 int ret;
364 struct subchannel *sch;
365
Sebastian Ott703e5c92009-09-22 22:58:38 +0200366 if (cio_is_console(schid))
367 sch = cio_get_console_subchannel();
368 else {
369 sch = css_alloc_subchannel(schid);
370 if (IS_ERR(sch))
371 return PTR_ERR(sch);
372 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 ret = css_register_subchannel(sch);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200374 if (ret) {
375 if (!cio_is_console(schid))
376 put_device(&sch->dev);
377 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return ret;
379}
380
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700381static int
382check_subchannel(struct device * dev, void * data)
383{
384 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800385 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700386
387 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800388 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700389}
390
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800392get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 struct device *dev;
395
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700396 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200397 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700399 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400}
401
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100402/**
403 * css_sch_is_valid() - check if a subchannel is valid
404 * @schib: subchannel information block for the subchannel
405 */
406int css_sch_is_valid(struct schib *schib)
407{
408 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
409 return 0;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200410 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
411 return 0;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100412 return 1;
413}
414EXPORT_SYMBOL_GPL(css_sch_is_valid);
415
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200416static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
417{
418 struct schib schib;
419
420 if (!slow) {
421 /* Will be done on the slow path. */
422 return -EAGAIN;
423 }
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100424 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200425 /* Unusable - ignore. */
426 return 0;
427 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100428 CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
429 schid.sch_no);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200430
431 return css_probe_device(schid);
432}
433
Cornelia Huckc820de32008-07-14 09:58:45 +0200434static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
435{
436 int ret = 0;
437
438 if (sch->driver) {
439 if (sch->driver->sch_event)
440 ret = sch->driver->sch_event(sch, slow);
441 else
442 dev_dbg(&sch->dev,
443 "Got subchannel machine check but "
444 "no sch_event handler provided.\n");
445 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100446 if (ret != 0 && ret != -EAGAIN) {
447 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
448 sch->schid.ssid, sch->schid.sch_no, ret);
449 }
Cornelia Huckc820de32008-07-14 09:58:45 +0200450 return ret;
451}
452
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200453static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200454{
455 struct subchannel *sch;
456 int ret;
457
458 sch = get_subchannel_by_schid(schid);
459 if (sch) {
460 ret = css_evaluate_known_subchannel(sch, slow);
461 put_device(&sch->dev);
462 } else
463 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200464 if (ret == -EAGAIN)
465 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466}
467
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200468static struct idset *slow_subchannel_set;
469static spinlock_t slow_subchannel_lock;
Sebastian Ott25530552009-09-22 22:58:34 +0200470static wait_queue_head_t css_eval_wq;
471static atomic_t css_eval_scheduled;
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200472
473static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200475 spin_lock_init(&slow_subchannel_lock);
Sebastian Ott25530552009-09-22 22:58:34 +0200476 atomic_set(&css_eval_scheduled, 0);
477 init_waitqueue_head(&css_eval_wq);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200478 slow_subchannel_set = idset_sch_new();
479 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200480 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200481 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200483 return 0;
484}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100486static int slow_eval_known_fn(struct subchannel *sch, void *data)
487{
488 int eval;
489 int rc;
490
491 spin_lock_irq(&slow_subchannel_lock);
492 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
493 idset_sch_del(slow_subchannel_set, sch->schid);
494 spin_unlock_irq(&slow_subchannel_lock);
495 if (eval) {
496 rc = css_evaluate_known_subchannel(sch, 1);
497 if (rc == -EAGAIN)
498 css_schedule_eval(sch->schid);
499 }
500 return 0;
501}
502
503static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
504{
505 int eval;
506 int rc = 0;
507
508 spin_lock_irq(&slow_subchannel_lock);
509 eval = idset_sch_contains(slow_subchannel_set, schid);
510 idset_sch_del(slow_subchannel_set, schid);
511 spin_unlock_irq(&slow_subchannel_lock);
512 if (eval) {
513 rc = css_evaluate_new_subchannel(schid, 1);
514 switch (rc) {
515 case -EAGAIN:
516 css_schedule_eval(schid);
517 rc = 0;
518 break;
519 case -ENXIO:
520 case -ENOMEM:
521 case -EIO:
522 /* These should abort looping */
523 break;
524 default:
525 rc = 0;
526 }
527 }
528 return rc;
529}
530
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200531static void css_slow_path_func(struct work_struct *unused)
532{
Sebastian Ott25530552009-09-22 22:58:34 +0200533 unsigned long flags;
534
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200535 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100536 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
537 NULL);
Sebastian Ott25530552009-09-22 22:58:34 +0200538 spin_lock_irqsave(&slow_subchannel_lock, flags);
539 if (idset_is_empty(slow_subchannel_set)) {
540 atomic_set(&css_eval_scheduled, 0);
541 wake_up(&css_eval_wq);
542 }
543 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544}
545
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200546static DECLARE_WORK(slow_path_work, css_slow_path_func);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100547struct workqueue_struct *cio_work_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200549void css_schedule_eval(struct subchannel_id schid)
550{
551 unsigned long flags;
552
553 spin_lock_irqsave(&slow_subchannel_lock, flags);
554 idset_sch_add(slow_subchannel_set, schid);
Sebastian Ott25530552009-09-22 22:58:34 +0200555 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100556 queue_work(cio_work_q, &slow_path_work);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200557 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
558}
559
560void css_schedule_eval_all(void)
561{
562 unsigned long flags;
563
564 spin_lock_irqsave(&slow_subchannel_lock, flags);
565 idset_fill(slow_subchannel_set);
Sebastian Ott25530552009-09-22 22:58:34 +0200566 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100567 queue_work(cio_work_q, &slow_path_work);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200568 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
569}
570
Sebastian Ott703e5c92009-09-22 22:58:38 +0200571static int __unset_registered(struct device *dev, void *data)
572{
573 struct idset *set = data;
574 struct subchannel *sch = to_subchannel(dev);
575
576 idset_sch_del(set, sch->schid);
577 return 0;
578}
579
580void css_schedule_eval_all_unreg(void)
581{
582 unsigned long flags;
583 struct idset *unreg_set;
584
585 /* Find unregistered subchannels. */
586 unreg_set = idset_sch_new();
587 if (!unreg_set) {
588 /* Fallback. */
589 css_schedule_eval_all();
590 return;
591 }
592 idset_fill(unreg_set);
593 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
594 /* Apply to slow_subchannel_set. */
595 spin_lock_irqsave(&slow_subchannel_lock, flags);
596 idset_add_set(slow_subchannel_set, unreg_set);
597 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100598 queue_work(cio_work_q, &slow_path_work);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200599 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
600 idset_free(unreg_set);
601}
602
Cornelia Huck22806dc2008-04-17 07:45:59 +0200603void css_wait_for_slow_path(void)
604{
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100605 flush_workqueue(cio_work_q);
Cornelia Huck22806dc2008-04-17 07:45:59 +0200606}
607
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200608/* Schedule reprobing of all unregistered subchannels. */
609void css_schedule_reprobe(void)
610{
Sebastian Ott703e5c92009-09-22 22:58:38 +0200611 css_schedule_eval_all_unreg();
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200612}
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200613EXPORT_SYMBOL_GPL(css_schedule_reprobe);
614
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 * Called from the machine check handler for subchannel report words.
617 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200618static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800620 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Cornelia Huckc1156182008-07-14 09:58:46 +0200622 if (overflow) {
623 css_schedule_eval_all();
624 return;
625 }
626 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
627 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
628 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
629 crw0->erc, crw0->rsid);
630 if (crw1)
631 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
632 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
633 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
634 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800635 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200636 mchk_schid.sch_no = crw0->rsid;
637 if (crw1)
638 mchk_schid.ssid = (crw1->rsid >> 8) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800639
Cornelia Huckc1156182008-07-14 09:58:46 +0200640 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 * Since we are always presented with IPI in the CRW, we have to
642 * use stsch() to find out if the subchannel in question has come
643 * or gone.
644 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200645 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646}
647
648static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800649css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650{
Cornelia Huck75784c02008-07-14 09:58:57 +0200651 if (css_general_characteristics.mcss) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800652 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
653 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
654 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655#ifdef CONFIG_SMP
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100656 css->global_pgid.pgid_high.cpu_addr = stap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800658 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659#endif
660 }
Heiko Carstense86a6ed2009-09-11 10:29:04 +0200661 css->global_pgid.cpu_id = S390_lowcore.cpu_id.ident;
662 css->global_pgid.cpu_model = S390_lowcore.cpu_id.machine;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800663 css->global_pgid.tod_high = tod_high;
664
665}
666
Cornelia Huck3b793062006-01-06 00:19:26 -0800667static void
668channel_subsystem_release(struct device *dev)
669{
670 struct channel_subsystem *css;
671
672 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800673 mutex_destroy(&css->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200674 if (css->pseudo_subchannel) {
675 /* Implies that it has been generated but never registered. */
676 css_subchannel_release(&css->pseudo_subchannel->dev);
677 css->pseudo_subchannel = NULL;
678 }
Cornelia Huck3b793062006-01-06 00:19:26 -0800679 kfree(css);
680}
681
Cornelia Huck495a5b42006-03-24 03:15:14 -0800682static ssize_t
683css_cm_enable_show(struct device *dev, struct device_attribute *attr,
684 char *buf)
685{
686 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200687 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800688
689 if (!css)
690 return 0;
Michael Ernst8284fb192008-04-17 07:46:01 +0200691 mutex_lock(&css->mutex);
692 ret = sprintf(buf, "%x\n", css->cm_enabled);
693 mutex_unlock(&css->mutex);
694 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800695}
696
697static ssize_t
698css_cm_enable_store(struct device *dev, struct device_attribute *attr,
699 const char *buf, size_t count)
700{
701 struct channel_subsystem *css = to_css(dev);
702 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +0200703 unsigned long val;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800704
Cornelia Huck2f972202008-04-30 13:38:33 +0200705 ret = strict_strtoul(buf, 16, &val);
706 if (ret)
707 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200708 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200709 switch (val) {
710 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800711 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
712 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200713 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800714 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
715 break;
716 default:
717 ret = -EINVAL;
718 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200719 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800720 return ret < 0 ? ret : count;
721}
722
723static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
724
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100725static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800726{
727 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100728 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200729 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800730
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200731 css = channel_subsystems[nr];
732 memset(css, 0, sizeof(struct channel_subsystem));
733 css->pseudo_subchannel =
734 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
735 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100736 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200737 css->pseudo_subchannel->dev.parent = &css->device;
738 css->pseudo_subchannel->dev.release = css_subchannel_release;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200739 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100740 mutex_init(&css->pseudo_subchannel->reg_mutex);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200741 ret = cio_create_sch_lock(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100742 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200743 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100744 return ret;
745 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200746 mutex_init(&css->mutex);
747 css->valid = 1;
748 css->cssid = nr;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200749 dev_set_name(&css->device, "css%x", nr);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200750 css->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800751 tod_high = (u32) (get_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200752 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100753 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754}
755
Cornelia Hucka55360d2007-10-12 16:11:20 +0200756static int css_reboot_event(struct notifier_block *this,
757 unsigned long event,
758 void *ptr)
759{
760 int ret, i;
761
762 ret = NOTIFY_DONE;
763 for (i = 0; i <= __MAX_CSSID; i++) {
764 struct channel_subsystem *css;
765
766 css = channel_subsystems[i];
Michael Ernst8284fb192008-04-17 07:46:01 +0200767 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200768 if (css->cm_enabled)
769 if (chsc_secm(css, 0))
770 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200771 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200772 }
773
774 return ret;
775}
776
777static struct notifier_block css_reboot_notifier = {
778 .notifier_call = css_reboot_event,
779};
780
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200782 * Since the css devices are neither on a bus nor have a class
783 * nor have a special device type, we cannot stop/restart channel
784 * path measurements via the normal suspend/resume callbacks, but have
785 * to use notifiers.
786 */
787static int css_power_event(struct notifier_block *this, unsigned long event,
788 void *ptr)
789{
790 void *secm_area;
791 int ret, i;
792
793 switch (event) {
794 case PM_HIBERNATION_PREPARE:
795 case PM_SUSPEND_PREPARE:
796 ret = NOTIFY_DONE;
797 for (i = 0; i <= __MAX_CSSID; i++) {
798 struct channel_subsystem *css;
799
800 css = channel_subsystems[i];
801 mutex_lock(&css->mutex);
802 if (!css->cm_enabled) {
803 mutex_unlock(&css->mutex);
804 continue;
805 }
806 secm_area = (void *)get_zeroed_page(GFP_KERNEL |
807 GFP_DMA);
808 if (secm_area) {
809 if (__chsc_do_secm(css, 0, secm_area))
810 ret = NOTIFY_BAD;
811 free_page((unsigned long)secm_area);
812 } else
813 ret = NOTIFY_BAD;
814
815 mutex_unlock(&css->mutex);
816 }
817 break;
818 case PM_POST_HIBERNATION:
819 case PM_POST_SUSPEND:
820 ret = NOTIFY_DONE;
821 for (i = 0; i <= __MAX_CSSID; i++) {
822 struct channel_subsystem *css;
823
824 css = channel_subsystems[i];
825 mutex_lock(&css->mutex);
826 if (!css->cm_enabled) {
827 mutex_unlock(&css->mutex);
828 continue;
829 }
830 secm_area = (void *)get_zeroed_page(GFP_KERNEL |
831 GFP_DMA);
832 if (secm_area) {
833 if (__chsc_do_secm(css, 1, secm_area))
834 ret = NOTIFY_BAD;
835 free_page((unsigned long)secm_area);
836 } else
837 ret = NOTIFY_BAD;
838
839 mutex_unlock(&css->mutex);
840 }
841 /* search for subchannels, which appeared during hibernation */
842 css_schedule_reprobe();
843 break;
844 default:
845 ret = NOTIFY_DONE;
846 }
847 return ret;
848
849}
850static struct notifier_block css_power_notifier = {
851 .notifier_call = css_power_event,
852};
853
854/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 * Now that the driver core is running, we can setup our channel subsystem.
856 * The struct subchannel's are created during probing (except for the
857 * static console subchannel).
858 */
Sebastian Ott2f176442009-09-22 22:58:33 +0200859static int __init css_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800861 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Cornelia Huck4434a382007-07-27 12:29:21 +0200863 ret = chsc_determine_css_characteristics();
864 if (ret == -ENOMEM)
Sebastian Ott2f176442009-09-22 22:58:33 +0200865 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
Cornelia Huck4434a382007-07-27 12:29:21 +0200867 ret = chsc_alloc_sei_area();
868 if (ret)
869 goto out;
870
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200871 /* Try to enable MSS. */
872 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
873 switch (ret) {
874 case 0: /* Success. */
875 max_ssid = __MAX_SSID;
876 break;
877 case -ENOMEM:
878 goto out;
879 default:
880 max_ssid = 0;
881 }
882
Cornelia Huck4434a382007-07-27 12:29:21 +0200883 ret = slow_subchannel_init();
884 if (ret)
885 goto out;
886
Heiko Carstensf5daba12009-03-26 15:24:01 +0100887 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
Cornelia Huckc1156182008-07-14 09:58:46 +0200888 if (ret)
889 goto out;
890
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 if ((ret = bus_register(&css_bus_type)))
892 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
Cornelia Hucka28c6942006-01-06 00:19:23 -0800894 /* Setup css structure. */
895 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200896 struct channel_subsystem *css;
897
898 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
899 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800900 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800901 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800902 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200903 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100904 ret = setup_css(i);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200905 if (ret) {
906 kfree(channel_subsystems[i]);
907 goto out_unregister;
908 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200909 ret = device_register(&css->device);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200910 if (ret) {
911 put_device(&css->device);
912 goto out_unregister;
913 }
Cornelia Huck75784c02008-07-14 09:58:57 +0200914 if (css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200915 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200916 &dev_attr_cm_enable);
917 if (ret)
918 goto out_device;
919 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200920 ret = device_register(&css->pseudo_subchannel->dev);
Sebastian Ottc6304932009-09-11 10:28:38 +0200921 if (ret) {
922 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100923 goto out_file;
Sebastian Ottc6304932009-09-11 10:28:38 +0200924 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800925 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200926 ret = register_reboot_notifier(&css_reboot_notifier);
927 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +0200928 goto out_unregister;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200929 ret = register_pm_notifier(&css_power_notifier);
930 if (ret) {
931 unregister_reboot_notifier(&css_reboot_notifier);
932 goto out_unregister;
933 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 css_init_done = 1;
935
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200936 /* Enable default isc for I/O subchannels. */
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200937 isc_register(IO_SCH_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 return 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100940out_file:
Cornelia Hucka2164b82008-09-09 12:38:57 +0200941 if (css_chsc_characteristics.secm)
942 device_remove_file(&channel_subsystems[i]->device,
943 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200944out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200945 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800946out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800947 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200948 struct channel_subsystem *css;
949
Cornelia Hucka28c6942006-01-06 00:19:23 -0800950 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200951 css = channel_subsystems[i];
952 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200953 css->pseudo_subchannel = NULL;
Cornelia Huck75784c02008-07-14 09:58:57 +0200954 if (css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200955 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800956 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200957 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800958 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 bus_unregister(&css_bus_type);
960out:
Heiko Carstensf5daba12009-03-26 15:24:01 +0100961 crw_unregister_handler(CRW_RSC_CSS);
Cornelia Huck4434a382007-07-27 12:29:21 +0200962 chsc_free_sei_area();
Sebastian Ottb827d1c82009-09-22 22:58:36 +0200963 idset_free(slow_subchannel_set);
Michael Ernste6d5a422008-12-25 13:39:36 +0100964 pr_alert("The CSS device driver initialization failed with "
965 "errno=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 return ret;
967}
968
Sebastian Ott2f176442009-09-22 22:58:33 +0200969static void __init css_bus_cleanup(void)
970{
971 struct channel_subsystem *css;
972 int i;
973
974 for (i = 0; i <= __MAX_CSSID; i++) {
975 css = channel_subsystems[i];
976 device_unregister(&css->pseudo_subchannel->dev);
977 css->pseudo_subchannel = NULL;
978 if (css_chsc_characteristics.secm)
979 device_remove_file(&css->device, &dev_attr_cm_enable);
980 device_unregister(&css->device);
981 }
982 bus_unregister(&css_bus_type);
983 crw_unregister_handler(CRW_RSC_CSS);
984 chsc_free_sei_area();
Sebastian Ottb827d1c82009-09-22 22:58:36 +0200985 idset_free(slow_subchannel_set);
Sebastian Ott2f176442009-09-22 22:58:33 +0200986 isc_unregister(IO_SCH_ISC);
987}
988
989static int __init channel_subsystem_init(void)
990{
991 int ret;
992
993 ret = css_bus_init();
994 if (ret)
995 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100996 cio_work_q = create_singlethread_workqueue("cio");
997 if (!cio_work_q) {
998 ret = -ENOMEM;
999 goto out_bus;
1000 }
Sebastian Ott2f176442009-09-22 22:58:33 +02001001 ret = io_subchannel_init();
1002 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001003 goto out_wq;
Sebastian Ott2f176442009-09-22 22:58:33 +02001004
1005 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001006out_wq:
1007 destroy_workqueue(cio_work_q);
1008out_bus:
1009 css_bus_cleanup();
1010 return ret;
Sebastian Ott2f176442009-09-22 22:58:33 +02001011}
1012subsys_initcall(channel_subsystem_init);
1013
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001014static int css_settle(struct device_driver *drv, void *unused)
1015{
1016 struct css_driver *cssdrv = to_cssdriver(drv);
1017
1018 if (cssdrv->settle)
Sebastian Ottb4c70722010-02-26 22:37:27 +01001019 return cssdrv->settle();
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001020 return 0;
1021}
1022
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001023int css_complete_work(void)
Sebastian Ott879acca52010-02-26 22:37:25 +01001024{
1025 int ret;
1026
1027 /* Wait for the evaluation of subchannels to finish. */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001028 ret = wait_event_interruptible(css_eval_wq,
1029 atomic_read(&css_eval_scheduled) == 0);
1030 if (ret)
1031 return -EINTR;
Sebastian Ott879acca52010-02-26 22:37:25 +01001032 flush_workqueue(cio_work_q);
1033 /* Wait for the subchannel type specific initialization to finish */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001034 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
Sebastian Ott879acca52010-02-26 22:37:25 +01001035}
1036
1037
Sebastian Ott2f176442009-09-22 22:58:33 +02001038/*
1039 * Wait for the initialization of devices to finish, to make sure we are
1040 * done with our setup if the search for the root device starts.
1041 */
1042static int __init channel_subsystem_init_sync(void)
1043{
Sebastian Ott703e5c92009-09-22 22:58:38 +02001044 /* Start initial subchannel evaluation. */
1045 css_schedule_eval_all();
Sebastian Ott879acca52010-02-26 22:37:25 +01001046 css_complete_work();
1047 return 0;
Sebastian Ott2f176442009-09-22 22:58:33 +02001048}
1049subsys_initcall_sync(channel_subsystem_init_sync);
1050
Sebastian Ott879acca52010-02-26 22:37:25 +01001051#ifdef CONFIG_PROC_FS
1052static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1053 size_t count, loff_t *ppos)
1054{
Sebastian Ottb4c70722010-02-26 22:37:27 +01001055 int ret;
1056
Sebastian Ott879acca52010-02-26 22:37:25 +01001057 /* Handle pending CRW's. */
1058 crw_wait_for_channel_report();
Sebastian Ottb4c70722010-02-26 22:37:27 +01001059 ret = css_complete_work();
1060
1061 return ret ? ret : count;
Sebastian Ott879acca52010-02-26 22:37:25 +01001062}
1063
1064static const struct file_operations cio_settle_proc_fops = {
1065 .write = cio_settle_write,
1066};
1067
1068static int __init cio_settle_init(void)
1069{
1070 struct proc_dir_entry *entry;
1071
1072 entry = proc_create("cio_settle", S_IWUSR, NULL,
1073 &cio_settle_proc_fops);
1074 if (!entry)
1075 return -ENOMEM;
1076 return 0;
1077}
1078device_initcall(cio_settle_init);
1079#endif /*CONFIG_PROC_FS*/
1080
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001081int sch_is_pseudo_sch(struct subchannel *sch)
1082{
1083 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1084}
1085
Cornelia Huckf08adc02008-07-14 09:59:03 +02001086static int css_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087{
Cornelia Huck084325d2008-01-26 14:10:38 +01001088 struct subchannel *sch = to_subchannel(dev);
1089 struct css_driver *driver = to_cssdriver(drv);
Cornelia Huckf08adc02008-07-14 09:59:03 +02001090 struct css_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Cornelia Huckf08adc02008-07-14 09:59:03 +02001092 for (id = driver->subchannel_type; id->match_flags; id++) {
1093 if (sch->st == id->type)
1094 return 1;
1095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096
1097 return 0;
1098}
1099
Cornelia Huck98c13c22008-01-26 14:10:40 +01001100static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001101{
1102 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +01001103 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001104
1105 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +01001106 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001107 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1108 if (ret)
1109 sch->driver = NULL;
1110 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001111}
1112
Cornelia Huck98c13c22008-01-26 14:10:40 +01001113static int css_remove(struct device *dev)
1114{
1115 struct subchannel *sch;
1116 int ret;
1117
1118 sch = to_subchannel(dev);
1119 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1120 sch->driver = NULL;
1121 return ret;
1122}
1123
1124static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001125{
1126 struct subchannel *sch;
1127
1128 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001129 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001130 sch->driver->shutdown(sch);
1131}
1132
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001133static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1134{
1135 struct subchannel *sch = to_subchannel(dev);
1136 int ret;
1137
1138 ret = add_uevent_var(env, "ST=%01X", sch->st);
1139 if (ret)
1140 return ret;
1141 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1142 return ret;
1143}
1144
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001145static int css_pm_prepare(struct device *dev)
1146{
1147 struct subchannel *sch = to_subchannel(dev);
1148 struct css_driver *drv;
1149
1150 if (mutex_is_locked(&sch->reg_mutex))
1151 return -EAGAIN;
1152 if (!sch->dev.driver)
1153 return 0;
1154 drv = to_cssdriver(sch->dev.driver);
1155 /* Notify drivers that they may not register children. */
1156 return drv->prepare ? drv->prepare(sch) : 0;
1157}
1158
1159static void css_pm_complete(struct device *dev)
1160{
1161 struct subchannel *sch = to_subchannel(dev);
1162 struct css_driver *drv;
1163
1164 if (!sch->dev.driver)
1165 return;
1166 drv = to_cssdriver(sch->dev.driver);
1167 if (drv->complete)
1168 drv->complete(sch);
1169}
1170
1171static int css_pm_freeze(struct device *dev)
1172{
1173 struct subchannel *sch = to_subchannel(dev);
1174 struct css_driver *drv;
1175
1176 if (!sch->dev.driver)
1177 return 0;
1178 drv = to_cssdriver(sch->dev.driver);
1179 return drv->freeze ? drv->freeze(sch) : 0;
1180}
1181
1182static int css_pm_thaw(struct device *dev)
1183{
1184 struct subchannel *sch = to_subchannel(dev);
1185 struct css_driver *drv;
1186
1187 if (!sch->dev.driver)
1188 return 0;
1189 drv = to_cssdriver(sch->dev.driver);
1190 return drv->thaw ? drv->thaw(sch) : 0;
1191}
1192
1193static int css_pm_restore(struct device *dev)
1194{
1195 struct subchannel *sch = to_subchannel(dev);
1196 struct css_driver *drv;
1197
1198 if (!sch->dev.driver)
1199 return 0;
1200 drv = to_cssdriver(sch->dev.driver);
1201 return drv->restore ? drv->restore(sch) : 0;
1202}
1203
Alexey Dobriyan47145212009-12-14 18:00:08 -08001204static const struct dev_pm_ops css_pm_ops = {
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001205 .prepare = css_pm_prepare,
1206 .complete = css_pm_complete,
1207 .freeze = css_pm_freeze,
1208 .thaw = css_pm_thaw,
1209 .restore = css_pm_restore,
1210};
1211
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001213 .name = "css",
1214 .match = css_bus_match,
1215 .probe = css_probe,
1216 .remove = css_remove,
1217 .shutdown = css_shutdown,
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001218 .uevent = css_uevent,
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001219 .pm = &css_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220};
1221
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001222/**
1223 * css_driver_register - register a css driver
1224 * @cdrv: css driver to register
1225 *
1226 * This is mainly a wrapper around driver_register that sets name
1227 * and bus_type in the embedded struct device_driver correctly.
1228 */
1229int css_driver_register(struct css_driver *cdrv)
1230{
1231 cdrv->drv.name = cdrv->name;
1232 cdrv->drv.bus = &css_bus_type;
Cornelia Huck4beee642008-01-26 14:10:47 +01001233 cdrv->drv.owner = cdrv->owner;
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001234 return driver_register(&cdrv->drv);
1235}
1236EXPORT_SYMBOL_GPL(css_driver_register);
1237
1238/**
1239 * css_driver_unregister - unregister a css driver
1240 * @cdrv: css driver to unregister
1241 *
1242 * This is a wrapper around driver_unregister.
1243 */
1244void css_driver_unregister(struct css_driver *cdrv)
1245{
1246 driver_unregister(&cdrv->drv);
1247}
1248EXPORT_SYMBOL_GPL(css_driver_unregister);
1249
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250MODULE_LICENSE("GPL");
1251EXPORT_SYMBOL(css_bus_type);