blob: b4d572f65f079d93650cebd8ff5f7bb39bcb75d2 [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 Oberparleiter7ad6a242007-04-27 16:01:35 +0200198static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
199{
200 int i;
201 int mask;
202
203 memset(ssd, 0, sizeof(struct chsc_ssd_info));
204 ssd->path_mask = pmcw->pim;
205 for (i = 0; i < 8; i++) {
206 mask = 0x80 >> i;
207 if (pmcw->pim & mask) {
208 chp_id_init(&ssd->chpid[i]);
209 ssd->chpid[i].id = pmcw->chpid[i];
210 }
211 }
212}
213
214static void ssd_register_chpids(struct chsc_ssd_info *ssd)
215{
216 int i;
217 int mask;
218
219 for (i = 0; i < 8; i++) {
220 mask = 0x80 >> i;
221 if (ssd->path_mask & mask)
222 if (!chp_is_registered(ssd->chpid[i]))
223 chp_new(ssd->chpid[i]);
224 }
225}
226
227void css_update_ssd_info(struct subchannel *sch)
228{
229 int ret;
230
231 if (cio_is_console(sch->schid)) {
232 /* Console is initialized too early for functions requiring
233 * memory allocation. */
234 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
235 } else {
236 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
237 if (ret)
238 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
239 ssd_register_chpids(&sch->ssd_info);
240 }
241}
242
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200243static ssize_t type_show(struct device *dev, struct device_attribute *attr,
244 char *buf)
245{
246 struct subchannel *sch = to_subchannel(dev);
247
248 return sprintf(buf, "%01x\n", sch->st);
249}
250
251static DEVICE_ATTR(type, 0444, type_show, NULL);
252
253static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
254 char *buf)
255{
256 struct subchannel *sch = to_subchannel(dev);
257
258 return sprintf(buf, "css:t%01X\n", sch->st);
259}
260
261static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
262
263static struct attribute *subch_attrs[] = {
264 &dev_attr_type.attr,
265 &dev_attr_modalias.attr,
266 NULL,
267};
268
269static struct attribute_group subch_attr_group = {
270 .attrs = subch_attrs,
271};
272
David Brownella4dbd672009-06-24 10:06:31 -0700273static const struct attribute_group *default_subch_attr_groups[] = {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200274 &subch_attr_group,
275 NULL,
276};
277
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200278static int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279{
280 int ret;
281
282 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200283 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 sch->dev.bus = &css_bus_type;
285 sch->dev.release = &css_subchannel_release;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200286 sch->dev.groups = default_subch_attr_groups;
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200287 /*
288 * We don't want to generate uevents for I/O subchannels that don't
289 * have a working ccw device behind them since they will be
290 * unregistered before they can be used anyway, so we delay the add
291 * uevent until after device recognition was successful.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200292 * Note that we suppress the uevent for all subchannel types;
293 * the subchannel driver can decide itself when it wants to inform
294 * userspace of its existence.
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200295 */
Ming Leif67f1292009-03-01 21:10:49 +0800296 dev_set_uevent_suppress(&sch->dev, 1);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200297 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200299 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100300 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200301 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
302 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100303 return ret;
304 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200305 if (!sch->driver) {
306 /*
307 * No driver matched. Generate the uevent now so that
308 * a fitting driver module may be loaded based on the
309 * modalias.
310 */
Ming Leif67f1292009-03-01 21:10:49 +0800311 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200312 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
313 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 return ret;
315}
316
Cornelia Huckc820de32008-07-14 09:58:45 +0200317int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
319 int ret;
320 struct subchannel *sch;
321
Sebastian Ott703e5c92009-09-22 22:58:38 +0200322 if (cio_is_console(schid))
323 sch = cio_get_console_subchannel();
324 else {
325 sch = css_alloc_subchannel(schid);
326 if (IS_ERR(sch))
327 return PTR_ERR(sch);
328 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 ret = css_register_subchannel(sch);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200330 if (ret) {
331 if (!cio_is_console(schid))
332 put_device(&sch->dev);
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return ret;
335}
336
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700337static int
338check_subchannel(struct device * dev, void * data)
339{
340 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800341 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700342
343 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800344 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700345}
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800348get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 struct device *dev;
351
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700352 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200353 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700355 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356}
357
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100358/**
359 * css_sch_is_valid() - check if a subchannel is valid
360 * @schib: subchannel information block for the subchannel
361 */
362int css_sch_is_valid(struct schib *schib)
363{
364 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
365 return 0;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200366 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
367 return 0;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100368 return 1;
369}
370EXPORT_SYMBOL_GPL(css_sch_is_valid);
371
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200372static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
373{
374 struct schib schib;
375
376 if (!slow) {
377 /* Will be done on the slow path. */
378 return -EAGAIN;
379 }
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100380 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200381 /* Unusable - ignore. */
382 return 0;
383 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100384 CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
385 schid.sch_no);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200386
387 return css_probe_device(schid);
388}
389
Cornelia Huckc820de32008-07-14 09:58:45 +0200390static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
391{
392 int ret = 0;
393
394 if (sch->driver) {
395 if (sch->driver->sch_event)
396 ret = sch->driver->sch_event(sch, slow);
397 else
398 dev_dbg(&sch->dev,
399 "Got subchannel machine check but "
400 "no sch_event handler provided.\n");
401 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100402 if (ret != 0 && ret != -EAGAIN) {
403 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
404 sch->schid.ssid, sch->schid.sch_no, ret);
405 }
Cornelia Huckc820de32008-07-14 09:58:45 +0200406 return ret;
407}
408
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200409static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200410{
411 struct subchannel *sch;
412 int ret;
413
414 sch = get_subchannel_by_schid(schid);
415 if (sch) {
416 ret = css_evaluate_known_subchannel(sch, slow);
417 put_device(&sch->dev);
418 } else
419 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200420 if (ret == -EAGAIN)
421 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422}
423
Sebastian Ott817e5002011-12-01 13:32:19 +0100424/**
425 * css_sched_sch_todo - schedule a subchannel operation
426 * @sch: subchannel
427 * @todo: todo
428 *
429 * Schedule the operation identified by @todo to be performed on the slow path
430 * workqueue. Do nothing if another operation with higher priority is already
431 * scheduled. Needs to be called with subchannel lock held.
432 */
433void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
434{
435 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
436 sch->schid.ssid, sch->schid.sch_no, todo);
437 if (sch->todo >= todo)
438 return;
439 /* Get workqueue ref. */
440 if (!get_device(&sch->dev))
441 return;
442 sch->todo = todo;
443 if (!queue_work(cio_work_q, &sch->todo_work)) {
444 /* Already queued, release workqueue ref. */
445 put_device(&sch->dev);
446 }
447}
Sebastian Ott01c5e6d2012-08-28 16:42:37 +0200448EXPORT_SYMBOL_GPL(css_sched_sch_todo);
Sebastian Ott817e5002011-12-01 13:32:19 +0100449
450static void css_sch_todo(struct work_struct *work)
451{
452 struct subchannel *sch;
453 enum sch_todo todo;
454 int ret;
455
456 sch = container_of(work, struct subchannel, todo_work);
457 /* Find out todo. */
458 spin_lock_irq(sch->lock);
459 todo = sch->todo;
460 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
461 sch->schid.sch_no, todo);
462 sch->todo = SCH_TODO_NOTHING;
463 spin_unlock_irq(sch->lock);
464 /* Perform todo. */
465 switch (todo) {
466 case SCH_TODO_NOTHING:
467 break;
468 case SCH_TODO_EVAL:
469 ret = css_evaluate_known_subchannel(sch, 1);
470 if (ret == -EAGAIN) {
471 spin_lock_irq(sch->lock);
472 css_sched_sch_todo(sch, todo);
473 spin_unlock_irq(sch->lock);
474 }
475 break;
476 case SCH_TODO_UNREG:
477 css_sch_device_unregister(sch);
478 break;
479 }
480 /* Release workqueue ref. */
481 put_device(&sch->dev);
482}
483
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200484static struct idset *slow_subchannel_set;
485static spinlock_t slow_subchannel_lock;
Sebastian Ott25530552009-09-22 22:58:34 +0200486static wait_queue_head_t css_eval_wq;
487static atomic_t css_eval_scheduled;
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200488
489static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200491 spin_lock_init(&slow_subchannel_lock);
Sebastian Ott25530552009-09-22 22:58:34 +0200492 atomic_set(&css_eval_scheduled, 0);
493 init_waitqueue_head(&css_eval_wq);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200494 slow_subchannel_set = idset_sch_new();
495 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200496 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200497 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200499 return 0;
500}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100502static int slow_eval_known_fn(struct subchannel *sch, void *data)
503{
504 int eval;
505 int rc;
506
507 spin_lock_irq(&slow_subchannel_lock);
508 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
509 idset_sch_del(slow_subchannel_set, sch->schid);
510 spin_unlock_irq(&slow_subchannel_lock);
511 if (eval) {
512 rc = css_evaluate_known_subchannel(sch, 1);
513 if (rc == -EAGAIN)
514 css_schedule_eval(sch->schid);
515 }
516 return 0;
517}
518
519static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
520{
521 int eval;
522 int rc = 0;
523
524 spin_lock_irq(&slow_subchannel_lock);
525 eval = idset_sch_contains(slow_subchannel_set, schid);
526 idset_sch_del(slow_subchannel_set, schid);
527 spin_unlock_irq(&slow_subchannel_lock);
528 if (eval) {
529 rc = css_evaluate_new_subchannel(schid, 1);
530 switch (rc) {
531 case -EAGAIN:
532 css_schedule_eval(schid);
533 rc = 0;
534 break;
535 case -ENXIO:
536 case -ENOMEM:
537 case -EIO:
538 /* These should abort looping */
539 break;
540 default:
541 rc = 0;
542 }
543 }
544 return rc;
545}
546
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200547static void css_slow_path_func(struct work_struct *unused)
548{
Sebastian Ott25530552009-09-22 22:58:34 +0200549 unsigned long flags;
550
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200551 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100552 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
553 NULL);
Sebastian Ott25530552009-09-22 22:58:34 +0200554 spin_lock_irqsave(&slow_subchannel_lock, flags);
555 if (idset_is_empty(slow_subchannel_set)) {
556 atomic_set(&css_eval_scheduled, 0);
557 wake_up(&css_eval_wq);
558 }
559 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560}
561
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200562static DECLARE_WORK(slow_path_work, css_slow_path_func);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100563struct workqueue_struct *cio_work_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200565void css_schedule_eval(struct subchannel_id schid)
566{
567 unsigned long flags;
568
569 spin_lock_irqsave(&slow_subchannel_lock, flags);
570 idset_sch_add(slow_subchannel_set, schid);
Sebastian Ott25530552009-09-22 22:58:34 +0200571 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100572 queue_work(cio_work_q, &slow_path_work);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200573 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
574}
575
576void css_schedule_eval_all(void)
577{
578 unsigned long flags;
579
580 spin_lock_irqsave(&slow_subchannel_lock, flags);
581 idset_fill(slow_subchannel_set);
Sebastian Ott25530552009-09-22 22:58:34 +0200582 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100583 queue_work(cio_work_q, &slow_path_work);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200584 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
585}
586
Sebastian Ott703e5c92009-09-22 22:58:38 +0200587static int __unset_registered(struct device *dev, void *data)
588{
589 struct idset *set = data;
590 struct subchannel *sch = to_subchannel(dev);
591
592 idset_sch_del(set, sch->schid);
593 return 0;
594}
595
Sebastian Otta8481c22010-10-25 16:10:23 +0200596static void css_schedule_eval_all_unreg(void)
Sebastian Ott703e5c92009-09-22 22:58:38 +0200597{
598 unsigned long flags;
599 struct idset *unreg_set;
600
601 /* Find unregistered subchannels. */
602 unreg_set = idset_sch_new();
603 if (!unreg_set) {
604 /* Fallback. */
605 css_schedule_eval_all();
606 return;
607 }
608 idset_fill(unreg_set);
609 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
610 /* Apply to slow_subchannel_set. */
611 spin_lock_irqsave(&slow_subchannel_lock, flags);
612 idset_add_set(slow_subchannel_set, unreg_set);
613 atomic_set(&css_eval_scheduled, 1);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100614 queue_work(cio_work_q, &slow_path_work);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200615 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
616 idset_free(unreg_set);
617}
618
Cornelia Huck22806dc2008-04-17 07:45:59 +0200619void css_wait_for_slow_path(void)
620{
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100621 flush_workqueue(cio_work_q);
Cornelia Huck22806dc2008-04-17 07:45:59 +0200622}
623
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200624/* Schedule reprobing of all unregistered subchannels. */
625void css_schedule_reprobe(void)
626{
Sebastian Ott703e5c92009-09-22 22:58:38 +0200627 css_schedule_eval_all_unreg();
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200628}
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200629EXPORT_SYMBOL_GPL(css_schedule_reprobe);
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 * Called from the machine check handler for subchannel report words.
633 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200634static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800636 struct subchannel_id mchk_schid;
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100637 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638
Cornelia Huckc1156182008-07-14 09:58:46 +0200639 if (overflow) {
640 css_schedule_eval_all();
641 return;
642 }
643 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
644 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
645 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
646 crw0->erc, crw0->rsid);
647 if (crw1)
648 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
649 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
650 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
651 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800652 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200653 mchk_schid.sch_no = crw0->rsid;
654 if (crw1)
Sebastian Ott8d7bfb42010-12-01 10:08:02 +0100655 mchk_schid.ssid = (crw1->rsid >> 4) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800656
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100657 if (crw0->erc == CRW_ERC_PMOD) {
658 sch = get_subchannel_by_schid(mchk_schid);
659 if (sch) {
660 css_update_ssd_info(sch);
661 put_device(&sch->dev);
662 }
663 }
Cornelia Huckc1156182008-07-14 09:58:46 +0200664 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 * Since we are always presented with IPI in the CRW, we have to
666 * use stsch() to find out if the subchannel in question has come
667 * or gone.
668 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200669 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670}
671
672static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800673css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674{
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200675 struct cpuid cpu_id;
676
Cornelia Huck75784c02008-07-14 09:58:57 +0200677 if (css_general_characteristics.mcss) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800678 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
679 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
680 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681#ifdef CONFIG_SMP
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100682 css->global_pgid.pgid_high.cpu_addr = stap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800684 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685#endif
686 }
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200687 get_cpu_id(&cpu_id);
688 css->global_pgid.cpu_id = cpu_id.ident;
689 css->global_pgid.cpu_model = cpu_id.machine;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800690 css->global_pgid.tod_high = tod_high;
691
692}
693
Cornelia Huck3b793062006-01-06 00:19:26 -0800694static void
695channel_subsystem_release(struct device *dev)
696{
697 struct channel_subsystem *css;
698
699 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800700 mutex_destroy(&css->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200701 if (css->pseudo_subchannel) {
702 /* Implies that it has been generated but never registered. */
703 css_subchannel_release(&css->pseudo_subchannel->dev);
704 css->pseudo_subchannel = NULL;
705 }
Cornelia Huck3b793062006-01-06 00:19:26 -0800706 kfree(css);
707}
708
Cornelia Huck495a5b42006-03-24 03:15:14 -0800709static ssize_t
710css_cm_enable_show(struct device *dev, struct device_attribute *attr,
711 char *buf)
712{
713 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200714 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800715
716 if (!css)
717 return 0;
Michael Ernst8284fb192008-04-17 07:46:01 +0200718 mutex_lock(&css->mutex);
719 ret = sprintf(buf, "%x\n", css->cm_enabled);
720 mutex_unlock(&css->mutex);
721 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800722}
723
724static ssize_t
725css_cm_enable_store(struct device *dev, struct device_attribute *attr,
726 const char *buf, size_t count)
727{
728 struct channel_subsystem *css = to_css(dev);
729 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +0200730 unsigned long val;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800731
Cornelia Huck2f972202008-04-30 13:38:33 +0200732 ret = strict_strtoul(buf, 16, &val);
733 if (ret)
734 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200735 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200736 switch (val) {
737 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800738 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
739 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200740 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800741 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
742 break;
743 default:
744 ret = -EINVAL;
745 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200746 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800747 return ret < 0 ? ret : count;
748}
749
750static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
751
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100752static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800753{
754 u32 tod_high;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100755 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200756 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800757
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200758 css = channel_subsystems[nr];
759 memset(css, 0, sizeof(struct channel_subsystem));
760 css->pseudo_subchannel =
761 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
762 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100763 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200764 css->pseudo_subchannel->dev.parent = &css->device;
765 css->pseudo_subchannel->dev.release = css_subchannel_release;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200766 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100767 mutex_init(&css->pseudo_subchannel->reg_mutex);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200768 ret = cio_create_sch_lock(css->pseudo_subchannel);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100769 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200770 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100771 return ret;
772 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200773 mutex_init(&css->mutex);
774 css->valid = 1;
775 css->cssid = nr;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200776 dev_set_name(&css->device, "css%x", nr);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200777 css->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800778 tod_high = (u32) (get_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200779 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100780 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781}
782
Cornelia Hucka55360d2007-10-12 16:11:20 +0200783static int css_reboot_event(struct notifier_block *this,
784 unsigned long event,
785 void *ptr)
786{
787 int ret, i;
788
789 ret = NOTIFY_DONE;
790 for (i = 0; i <= __MAX_CSSID; i++) {
791 struct channel_subsystem *css;
792
793 css = channel_subsystems[i];
Michael Ernst8284fb192008-04-17 07:46:01 +0200794 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200795 if (css->cm_enabled)
796 if (chsc_secm(css, 0))
797 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200798 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200799 }
800
801 return ret;
802}
803
804static struct notifier_block css_reboot_notifier = {
805 .notifier_call = css_reboot_event,
806};
807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200809 * Since the css devices are neither on a bus nor have a class
810 * nor have a special device type, we cannot stop/restart channel
811 * path measurements via the normal suspend/resume callbacks, but have
812 * to use notifiers.
813 */
814static int css_power_event(struct notifier_block *this, unsigned long event,
815 void *ptr)
816{
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200817 int ret, i;
818
819 switch (event) {
820 case PM_HIBERNATION_PREPARE:
821 case PM_SUSPEND_PREPARE:
822 ret = NOTIFY_DONE;
823 for (i = 0; i <= __MAX_CSSID; i++) {
824 struct channel_subsystem *css;
825
826 css = channel_subsystems[i];
827 mutex_lock(&css->mutex);
828 if (!css->cm_enabled) {
829 mutex_unlock(&css->mutex);
830 continue;
831 }
Akinobu Mitaf0c077a2011-07-08 20:53:36 +0200832 ret = __chsc_do_secm(css, 0);
833 ret = notifier_from_errno(ret);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200834 mutex_unlock(&css->mutex);
835 }
836 break;
837 case PM_POST_HIBERNATION:
838 case PM_POST_SUSPEND:
839 ret = NOTIFY_DONE;
840 for (i = 0; i <= __MAX_CSSID; i++) {
841 struct channel_subsystem *css;
842
843 css = channel_subsystems[i];
844 mutex_lock(&css->mutex);
845 if (!css->cm_enabled) {
846 mutex_unlock(&css->mutex);
847 continue;
848 }
Akinobu Mitaf0c077a2011-07-08 20:53:36 +0200849 ret = __chsc_do_secm(css, 1);
850 ret = notifier_from_errno(ret);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200851 mutex_unlock(&css->mutex);
852 }
853 /* search for subchannels, which appeared during hibernation */
854 css_schedule_reprobe();
855 break;
856 default:
857 ret = NOTIFY_DONE;
858 }
859 return ret;
860
861}
862static struct notifier_block css_power_notifier = {
863 .notifier_call = css_power_event,
864};
865
866/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 * Now that the driver core is running, we can setup our channel subsystem.
868 * The struct subchannel's are created during probing (except for the
869 * static console subchannel).
870 */
Sebastian Ott2f176442009-09-22 22:58:33 +0200871static int __init css_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800873 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Sebastian Ott34aec072010-10-25 16:10:28 +0200875 ret = chsc_init();
876 if (ret)
877 return ret;
878
Sebastian Ott34196f82010-10-25 16:10:29 +0200879 chsc_determine_css_characteristics();
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200880 /* Try to enable MSS. */
881 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott818c2722010-04-22 17:17:03 +0200882 if (ret)
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200883 max_ssid = 0;
Sebastian Ott818c2722010-04-22 17:17:03 +0200884 else /* Success. */
885 max_ssid = __MAX_SSID;
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200886
Cornelia Huck4434a382007-07-27 12:29:21 +0200887 ret = slow_subchannel_init();
888 if (ret)
889 goto out;
890
Heiko Carstensf5daba12009-03-26 15:24:01 +0100891 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
Cornelia Huckc1156182008-07-14 09:58:46 +0200892 if (ret)
893 goto out;
894
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895 if ((ret = bus_register(&css_bus_type)))
896 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Cornelia Hucka28c6942006-01-06 00:19:23 -0800898 /* Setup css structure. */
899 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200900 struct channel_subsystem *css;
901
902 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
903 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800904 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800905 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800906 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200907 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100908 ret = setup_css(i);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200909 if (ret) {
910 kfree(channel_subsystems[i]);
911 goto out_unregister;
912 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200913 ret = device_register(&css->device);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200914 if (ret) {
915 put_device(&css->device);
916 goto out_unregister;
917 }
Cornelia Huck75784c02008-07-14 09:58:57 +0200918 if (css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200919 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200920 &dev_attr_cm_enable);
921 if (ret)
922 goto out_device;
923 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200924 ret = device_register(&css->pseudo_subchannel->dev);
Sebastian Ottc6304932009-09-11 10:28:38 +0200925 if (ret) {
926 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100927 goto out_file;
Sebastian Ottc6304932009-09-11 10:28:38 +0200928 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800929 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200930 ret = register_reboot_notifier(&css_reboot_notifier);
931 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +0200932 goto out_unregister;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200933 ret = register_pm_notifier(&css_power_notifier);
934 if (ret) {
935 unregister_reboot_notifier(&css_reboot_notifier);
936 goto out_unregister;
937 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 css_init_done = 1;
939
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200940 /* Enable default isc for I/O subchannels. */
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200941 isc_register(IO_SCH_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 return 0;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100944out_file:
Cornelia Hucka2164b82008-09-09 12:38:57 +0200945 if (css_chsc_characteristics.secm)
946 device_remove_file(&channel_subsystems[i]->device,
947 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200948out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200949 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800950out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800951 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200952 struct channel_subsystem *css;
953
Cornelia Hucka28c6942006-01-06 00:19:23 -0800954 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200955 css = channel_subsystems[i];
956 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200957 css->pseudo_subchannel = NULL;
Cornelia Huck75784c02008-07-14 09:58:57 +0200958 if (css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200959 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800960 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200961 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 bus_unregister(&css_bus_type);
964out:
Sebastian Ott34aec072010-10-25 16:10:28 +0200965 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c2009-09-22 22:58:36 +0200966 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +0200967 chsc_init_cleanup();
Michael Ernste6d5a422008-12-25 13:39:36 +0100968 pr_alert("The CSS device driver initialization failed with "
969 "errno=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 return ret;
971}
972
Sebastian Ott2f176442009-09-22 22:58:33 +0200973static void __init css_bus_cleanup(void)
974{
975 struct channel_subsystem *css;
976 int i;
977
978 for (i = 0; i <= __MAX_CSSID; i++) {
979 css = channel_subsystems[i];
980 device_unregister(&css->pseudo_subchannel->dev);
981 css->pseudo_subchannel = NULL;
982 if (css_chsc_characteristics.secm)
983 device_remove_file(&css->device, &dev_attr_cm_enable);
984 device_unregister(&css->device);
985 }
986 bus_unregister(&css_bus_type);
Sebastian Ott34aec072010-10-25 16:10:28 +0200987 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c2009-09-22 22:58:36 +0200988 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +0200989 chsc_init_cleanup();
Sebastian Ott2f176442009-09-22 22:58:33 +0200990 isc_unregister(IO_SCH_ISC);
991}
992
993static int __init channel_subsystem_init(void)
994{
995 int ret;
996
997 ret = css_bus_init();
998 if (ret)
999 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001000 cio_work_q = create_singlethread_workqueue("cio");
1001 if (!cio_work_q) {
1002 ret = -ENOMEM;
1003 goto out_bus;
1004 }
Sebastian Ott2f176442009-09-22 22:58:33 +02001005 ret = io_subchannel_init();
1006 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001007 goto out_wq;
Sebastian Ott2f176442009-09-22 22:58:33 +02001008
1009 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001010out_wq:
1011 destroy_workqueue(cio_work_q);
1012out_bus:
1013 css_bus_cleanup();
1014 return ret;
Sebastian Ott2f176442009-09-22 22:58:33 +02001015}
1016subsys_initcall(channel_subsystem_init);
1017
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001018static int css_settle(struct device_driver *drv, void *unused)
1019{
1020 struct css_driver *cssdrv = to_cssdriver(drv);
1021
1022 if (cssdrv->settle)
Sebastian Ottb4c70722010-02-26 22:37:27 +01001023 return cssdrv->settle();
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001024 return 0;
1025}
1026
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001027int css_complete_work(void)
Sebastian Ott879acca2010-02-26 22:37:25 +01001028{
1029 int ret;
1030
1031 /* Wait for the evaluation of subchannels to finish. */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001032 ret = wait_event_interruptible(css_eval_wq,
1033 atomic_read(&css_eval_scheduled) == 0);
1034 if (ret)
1035 return -EINTR;
Sebastian Ott879acca2010-02-26 22:37:25 +01001036 flush_workqueue(cio_work_q);
1037 /* Wait for the subchannel type specific initialization to finish */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001038 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
Sebastian Ott879acca2010-02-26 22:37:25 +01001039}
1040
1041
Sebastian Ott2f176442009-09-22 22:58:33 +02001042/*
1043 * Wait for the initialization of devices to finish, to make sure we are
1044 * done with our setup if the search for the root device starts.
1045 */
1046static int __init channel_subsystem_init_sync(void)
1047{
Sebastian Ott703e5c92009-09-22 22:58:38 +02001048 /* Start initial subchannel evaluation. */
1049 css_schedule_eval_all();
Sebastian Ott879acca2010-02-26 22:37:25 +01001050 css_complete_work();
1051 return 0;
Sebastian Ott2f176442009-09-22 22:58:33 +02001052}
1053subsys_initcall_sync(channel_subsystem_init_sync);
1054
Sebastian Ott889ee952010-04-22 17:17:04 +02001055void channel_subsystem_reinit(void)
1056{
Sebastian Ott62da1772010-10-25 16:10:32 +02001057 struct channel_path *chp;
1058 struct chp_id chpid;
1059
Sebastian Ott889ee952010-04-22 17:17:04 +02001060 chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott62da1772010-10-25 16:10:32 +02001061 chp_id_for_each(&chpid) {
1062 chp = chpid_to_chp(chpid);
1063 if (!chp)
1064 continue;
1065 chsc_determine_base_channel_path_desc(chpid, &chp->desc);
1066 }
Sebastian Ott889ee952010-04-22 17:17:04 +02001067}
1068
Sebastian Ott879acca2010-02-26 22:37:25 +01001069#ifdef CONFIG_PROC_FS
1070static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1071 size_t count, loff_t *ppos)
1072{
Sebastian Ottb4c70722010-02-26 22:37:27 +01001073 int ret;
1074
Sebastian Ott879acca2010-02-26 22:37:25 +01001075 /* Handle pending CRW's. */
1076 crw_wait_for_channel_report();
Sebastian Ottb4c70722010-02-26 22:37:27 +01001077 ret = css_complete_work();
1078
1079 return ret ? ret : count;
Sebastian Ott879acca2010-02-26 22:37:25 +01001080}
1081
1082static const struct file_operations cio_settle_proc_fops = {
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +02001083 .open = nonseekable_open,
Sebastian Ott879acca2010-02-26 22:37:25 +01001084 .write = cio_settle_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001085 .llseek = no_llseek,
Sebastian Ott879acca2010-02-26 22:37:25 +01001086};
1087
1088static int __init cio_settle_init(void)
1089{
1090 struct proc_dir_entry *entry;
1091
1092 entry = proc_create("cio_settle", S_IWUSR, NULL,
1093 &cio_settle_proc_fops);
1094 if (!entry)
1095 return -ENOMEM;
1096 return 0;
1097}
1098device_initcall(cio_settle_init);
1099#endif /*CONFIG_PROC_FS*/
1100
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +01001101int sch_is_pseudo_sch(struct subchannel *sch)
1102{
1103 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1104}
1105
Cornelia Huckf08adc02008-07-14 09:59:03 +02001106static int css_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107{
Cornelia Huck084325d2008-01-26 14:10:38 +01001108 struct subchannel *sch = to_subchannel(dev);
1109 struct css_driver *driver = to_cssdriver(drv);
Cornelia Huckf08adc02008-07-14 09:59:03 +02001110 struct css_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111
Cornelia Huckf08adc02008-07-14 09:59:03 +02001112 for (id = driver->subchannel_type; id->match_flags; id++) {
1113 if (sch->st == id->type)
1114 return 1;
1115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 return 0;
1118}
1119
Cornelia Huck98c13c22008-01-26 14:10:40 +01001120static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001121{
1122 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +01001123 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001124
1125 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +01001126 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001127 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1128 if (ret)
1129 sch->driver = NULL;
1130 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001131}
1132
Cornelia Huck98c13c22008-01-26 14:10:40 +01001133static int css_remove(struct device *dev)
1134{
1135 struct subchannel *sch;
1136 int ret;
1137
1138 sch = to_subchannel(dev);
1139 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1140 sch->driver = NULL;
1141 return ret;
1142}
1143
1144static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001145{
1146 struct subchannel *sch;
1147
1148 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001149 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001150 sch->driver->shutdown(sch);
1151}
1152
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001153static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1154{
1155 struct subchannel *sch = to_subchannel(dev);
1156 int ret;
1157
1158 ret = add_uevent_var(env, "ST=%01X", sch->st);
1159 if (ret)
1160 return ret;
1161 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1162 return ret;
1163}
1164
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001165static int css_pm_prepare(struct device *dev)
1166{
1167 struct subchannel *sch = to_subchannel(dev);
1168 struct css_driver *drv;
1169
1170 if (mutex_is_locked(&sch->reg_mutex))
1171 return -EAGAIN;
1172 if (!sch->dev.driver)
1173 return 0;
1174 drv = to_cssdriver(sch->dev.driver);
1175 /* Notify drivers that they may not register children. */
1176 return drv->prepare ? drv->prepare(sch) : 0;
1177}
1178
1179static void css_pm_complete(struct device *dev)
1180{
1181 struct subchannel *sch = to_subchannel(dev);
1182 struct css_driver *drv;
1183
1184 if (!sch->dev.driver)
1185 return;
1186 drv = to_cssdriver(sch->dev.driver);
1187 if (drv->complete)
1188 drv->complete(sch);
1189}
1190
1191static int css_pm_freeze(struct device *dev)
1192{
1193 struct subchannel *sch = to_subchannel(dev);
1194 struct css_driver *drv;
1195
1196 if (!sch->dev.driver)
1197 return 0;
1198 drv = to_cssdriver(sch->dev.driver);
1199 return drv->freeze ? drv->freeze(sch) : 0;
1200}
1201
1202static int css_pm_thaw(struct device *dev)
1203{
1204 struct subchannel *sch = to_subchannel(dev);
1205 struct css_driver *drv;
1206
1207 if (!sch->dev.driver)
1208 return 0;
1209 drv = to_cssdriver(sch->dev.driver);
1210 return drv->thaw ? drv->thaw(sch) : 0;
1211}
1212
1213static int css_pm_restore(struct device *dev)
1214{
1215 struct subchannel *sch = to_subchannel(dev);
1216 struct css_driver *drv;
1217
Sebastian Otteb4f5d92010-10-25 16:10:33 +02001218 css_update_ssd_info(sch);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001219 if (!sch->dev.driver)
1220 return 0;
1221 drv = to_cssdriver(sch->dev.driver);
1222 return drv->restore ? drv->restore(sch) : 0;
1223}
1224
Alexey Dobriyan47145212009-12-14 18:00:08 -08001225static const struct dev_pm_ops css_pm_ops = {
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001226 .prepare = css_pm_prepare,
1227 .complete = css_pm_complete,
1228 .freeze = css_pm_freeze,
1229 .thaw = css_pm_thaw,
1230 .restore = css_pm_restore,
1231};
1232
Sebastian Ott3041b6a2011-03-15 17:08:31 +01001233static struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001234 .name = "css",
1235 .match = css_bus_match,
1236 .probe = css_probe,
1237 .remove = css_remove,
1238 .shutdown = css_shutdown,
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001239 .uevent = css_uevent,
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001240 .pm = &css_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241};
1242
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001243/**
1244 * css_driver_register - register a css driver
1245 * @cdrv: css driver to register
1246 *
1247 * This is mainly a wrapper around driver_register that sets name
1248 * and bus_type in the embedded struct device_driver correctly.
1249 */
1250int css_driver_register(struct css_driver *cdrv)
1251{
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001252 cdrv->drv.bus = &css_bus_type;
1253 return driver_register(&cdrv->drv);
1254}
1255EXPORT_SYMBOL_GPL(css_driver_register);
1256
1257/**
1258 * css_driver_unregister - unregister a css driver
1259 * @cdrv: css driver to unregister
1260 *
1261 * This is a wrapper around driver_unregister.
1262 */
1263void css_driver_unregister(struct css_driver *cdrv)
1264{
1265 driver_unregister(&cdrv->drv);
1266}
1267EXPORT_SYMBOL_GPL(css_driver_unregister);
1268
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269MODULE_LICENSE("GPL");