blob: 0268e5fd59b5522fe1d61dbf90ddf17ed4e2df1f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02002 * driver for channel subsystem
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Sebastian Ott34aec072010-10-25 16:10:28 +02004 * Copyright IBM Corp. 2002, 2010
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02005 *
6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
7 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
Michael Ernste6d5a422008-12-25 13:39:36 +01009
10#define KMSG_COMPONENT "cio"
11#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
12
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/device.h>
16#include <linux/slab.h>
17#include <linux/errno.h>
18#include <linux/list.h>
Cornelia Hucka55360d2007-10-12 16:11:20 +020019#include <linux/reboot.h>
Sebastian Ottdcbd16d2009-06-16 10:30:22 +020020#include <linux/suspend.h>
Sebastian Ott879acca52010-02-26 22:37:25 +010021#include <linux/proc_fs.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020022#include <asm/isc.h>
Heiko Carstensf5daba12009-03-26 15:24:01 +010023#include <asm/crw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024
25#include "css.h"
26#include "cio.h"
27#include "cio_debug.h"
28#include "ioasm.h"
29#include "chsc.h"
Peter Oberparleiter40154b82006-06-29 14:57:03 +020030#include "device.h"
Peter Oberparleiter83b33702007-04-27 16:01:34 +020031#include "idset.h"
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +020032#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Linus Torvalds1da177e2005-04-16 15:20:36 -070034int css_init_done = 0;
Sebastian Ottb0a285d2009-09-22 22:58:37 +020035int max_ssid;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Cornelia Huck7c9f4e32007-10-12 16:11:13 +020037struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
Sebastian Ott3041b6a2011-03-15 17:08:31 +010038static struct bus_type css_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Heiko Carstens4d284ca2007-02-05 21:18:53 +010040int
Cornelia Huckf97a56f2006-01-06 00:19:22 -080041for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
42{
43 struct subchannel_id schid;
44 int ret;
45
46 init_subchannel_id(&schid);
47 ret = -ENODEV;
48 do {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080049 do {
50 ret = fn(schid, data);
51 if (ret)
52 break;
53 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
54 schid.sch_no = 0;
55 } while (schid.ssid++ < max_ssid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080056 return ret;
57}
58
Peter Oberparleitere82a1562008-01-26 14:10:48 +010059struct cb_data {
60 void *data;
61 struct idset *set;
62 int (*fn_known_sch)(struct subchannel *, void *);
63 int (*fn_unknown_sch)(struct subchannel_id, void *);
64};
65
66static int call_fn_known_sch(struct device *dev, void *data)
67{
68 struct subchannel *sch = to_subchannel(dev);
69 struct cb_data *cb = data;
70 int rc = 0;
71
Peter Oberparleiter47d30672013-11-26 14:59:21 +010072 if (cb->set)
73 idset_sch_del(cb->set, sch->schid);
Peter Oberparleitere82a1562008-01-26 14:10:48 +010074 if (cb->fn_known_sch)
75 rc = cb->fn_known_sch(sch, cb->data);
76 return rc;
77}
78
79static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
80{
81 struct cb_data *cb = data;
82 int rc = 0;
83
84 if (idset_sch_contains(cb->set, schid))
85 rc = cb->fn_unknown_sch(schid, cb->data);
86 return rc;
87}
88
Sebastian Ott90ac24a2009-03-26 15:24:11 +010089static int call_fn_all_sch(struct subchannel_id schid, void *data)
90{
91 struct cb_data *cb = data;
92 struct subchannel *sch;
93 int rc = 0;
94
95 sch = get_subchannel_by_schid(schid);
96 if (sch) {
97 if (cb->fn_known_sch)
98 rc = cb->fn_known_sch(sch, cb->data);
99 put_device(&sch->dev);
100 } else {
101 if (cb->fn_unknown_sch)
102 rc = cb->fn_unknown_sch(schid, cb->data);
103 }
104
105 return rc;
106}
107
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100108int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
109 int (*fn_unknown)(struct subchannel_id,
110 void *), void *data)
111{
112 struct cb_data cb;
113 int rc;
114
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100115 cb.data = data;
116 cb.fn_known_sch = fn_known;
117 cb.fn_unknown_sch = fn_unknown;
Sebastian Ott90ac24a2009-03-26 15:24:11 +0100118
Peter Oberparleiter47d30672013-11-26 14:59:21 +0100119 if (fn_known && !fn_unknown) {
120 /* Skip idset allocation in case of known-only loop. */
121 cb.set = NULL;
122 return bus_for_each_dev(&css_bus_type, NULL, &cb,
123 call_fn_known_sch);
124 }
125
Sebastian Ott90ac24a2009-03-26 15:24:11 +0100126 cb.set = idset_sch_new();
127 if (!cb.set)
128 /* fall back to brute force scanning in case of oom */
129 return for_each_subchannel(call_fn_all_sch, &cb);
130
131 idset_fill(cb.set);
132
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100133 /* Process registered subchannels. */
134 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
135 if (rc)
136 goto out;
137 /* Process unregistered subchannels. */
138 if (fn_unknown)
139 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
140out:
141 idset_free(cb.set);
142
143 return rc;
144}
145
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100146static void css_sch_todo(struct work_struct *work);
147
Sebastian Otte5dcf002013-04-13 13:08:01 +0200148static int css_sch_create_locks(struct subchannel *sch)
149{
150 sch->lock = kmalloc(sizeof(*sch->lock), GFP_KERNEL);
151 if (!sch->lock)
152 return -ENOMEM;
153
154 spin_lock_init(sch->lock);
155 mutex_init(&sch->reg_mutex);
156
157 return 0;
158}
159
Sebastian Ottc135ad12013-04-13 12:58:55 +0200160static void css_subchannel_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161{
Sebastian Ott863fc842013-04-13 13:01:50 +0200162 struct subchannel *sch = to_subchannel(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Sebastian Ott863fc842013-04-13 13:01:50 +0200164 sch->config.intparm = 0;
165 cio_commit_config(sch);
166 kfree(sch->lock);
167 kfree(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Sebastian Ott863fc842013-04-13 13:01:50 +0200170struct subchannel *css_alloc_subchannel(struct subchannel_id schid)
Sebastian Ottc135ad12013-04-13 12:58:55 +0200171{
172 struct subchannel *sch;
173 int ret;
174
Sebastian Otte5dcf002013-04-13 13:08:01 +0200175 sch = kzalloc(sizeof(*sch), GFP_KERNEL | GFP_DMA);
176 if (!sch)
Sebastian Ottc135ad12013-04-13 12:58:55 +0200177 return ERR_PTR(-ENOMEM);
Sebastian Otte5dcf002013-04-13 13:08:01 +0200178
179 ret = cio_validate_subchannel(sch, schid);
180 if (ret < 0)
181 goto err;
182
183 ret = css_sch_create_locks(sch);
184 if (ret)
185 goto err;
186
Sebastian Ottc135ad12013-04-13 12:58:55 +0200187 INIT_WORK(&sch->todo_work, css_sch_todo);
188 sch->dev.release = &css_subchannel_release;
189 device_initialize(&sch->dev);
190 return sch;
Sebastian Otte5dcf002013-04-13 13:08:01 +0200191
192err:
193 kfree(sch);
194 return ERR_PTR(ret);
Sebastian Ottc135ad12013-04-13 12:58:55 +0200195}
196
Cornelia Huck07c6a332007-07-27 12:29:10 +0200197static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200198{
199 int ret;
200
201 mutex_lock(&sch->reg_mutex);
Sebastian Ott6ee4fec2009-09-11 10:28:25 +0200202 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
203 sch->schid.sch_no);
Sebastian Ottc135ad12013-04-13 12:58:55 +0200204 ret = device_add(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200205 mutex_unlock(&sch->reg_mutex);
206 return ret;
207}
208
Cornelia Huck44a1c192008-07-14 09:58:47 +0200209/**
210 * css_sch_device_unregister - unregister a subchannel
211 * @sch: subchannel to be unregistered
212 */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200213void css_sch_device_unregister(struct subchannel *sch)
214{
215 mutex_lock(&sch->reg_mutex);
Sebastian Ottef60cd12008-07-14 09:59:20 +0200216 if (device_is_registered(&sch->dev))
217 device_unregister(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200218 mutex_unlock(&sch->reg_mutex);
219}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200220EXPORT_SYMBOL_GPL(css_sch_device_unregister);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200221
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200222static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
223{
224 int i;
225 int mask;
226
227 memset(ssd, 0, sizeof(struct chsc_ssd_info));
228 ssd->path_mask = pmcw->pim;
229 for (i = 0; i < 8; i++) {
230 mask = 0x80 >> i;
231 if (pmcw->pim & mask) {
232 chp_id_init(&ssd->chpid[i]);
233 ssd->chpid[i].id = pmcw->chpid[i];
234 }
235 }
236}
237
238static void ssd_register_chpids(struct chsc_ssd_info *ssd)
239{
240 int i;
241 int mask;
242
243 for (i = 0; i < 8; i++) {
244 mask = 0x80 >> i;
245 if (ssd->path_mask & mask)
246 if (!chp_is_registered(ssd->chpid[i]))
247 chp_new(ssd->chpid[i]);
248 }
249}
250
251void css_update_ssd_info(struct subchannel *sch)
252{
253 int ret;
254
Sebastian Ott14556b32013-04-13 13:03:54 +0200255 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
256 if (ret)
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200257 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
Sebastian Ott14556b32013-04-13 13:03:54 +0200258
259 ssd_register_chpids(&sch->ssd_info);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200260}
261
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200262static ssize_t type_show(struct device *dev, struct device_attribute *attr,
263 char *buf)
264{
265 struct subchannel *sch = to_subchannel(dev);
266
267 return sprintf(buf, "%01x\n", sch->st);
268}
269
270static DEVICE_ATTR(type, 0444, type_show, NULL);
271
272static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
273 char *buf)
274{
275 struct subchannel *sch = to_subchannel(dev);
276
277 return sprintf(buf, "css:t%01X\n", sch->st);
278}
279
280static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
281
282static struct attribute *subch_attrs[] = {
283 &dev_attr_type.attr,
284 &dev_attr_modalias.attr,
285 NULL,
286};
287
288static struct attribute_group subch_attr_group = {
289 .attrs = subch_attrs,
290};
291
David Brownella4dbd672009-06-24 10:06:31 -0700292static const struct attribute_group *default_subch_attr_groups[] = {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200293 &subch_attr_group,
294 NULL,
295};
296
Sebastian Ott14556b32013-04-13 13:03:54 +0200297int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298{
299 int ret;
300
301 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200302 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 sch->dev.bus = &css_bus_type;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200304 sch->dev.groups = default_subch_attr_groups;
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200305 /*
306 * We don't want to generate uevents for I/O subchannels that don't
307 * have a working ccw device behind them since they will be
308 * unregistered before they can be used anyway, so we delay the add
309 * uevent until after device recognition was successful.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200310 * Note that we suppress the uevent for all subchannel types;
311 * the subchannel driver can decide itself when it wants to inform
312 * userspace of its existence.
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200313 */
Ming Leif67f1292009-03-01 21:10:49 +0800314 dev_set_uevent_suppress(&sch->dev, 1);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200315 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200317 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100318 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200319 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
320 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100321 return ret;
322 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200323 if (!sch->driver) {
324 /*
325 * No driver matched. Generate the uevent now so that
326 * a fitting driver module may be loaded based on the
327 * modalias.
328 */
Ming Leif67f1292009-03-01 21:10:49 +0800329 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200330 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 return ret;
333}
334
Sebastian Ott4e5ebd52013-04-13 13:04:49 +0200335static int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 struct subchannel *sch;
Sebastian Ott4e5ebd52013-04-13 13:04:49 +0200338 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339
Sebastian Ott14556b32013-04-13 13:03:54 +0200340 sch = css_alloc_subchannel(schid);
341 if (IS_ERR(sch))
342 return PTR_ERR(sch);
343
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 ret = css_register_subchannel(sch);
Sebastian Ott863fc842013-04-13 13:01:50 +0200345 if (ret)
346 put_device(&sch->dev);
347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return ret;
349}
350
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700351static int
352check_subchannel(struct device * dev, void * data)
353{
354 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800355 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700356
357 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800358 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700359}
360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800362get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 struct device *dev;
365
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700366 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200367 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700369 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370}
371
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100372/**
373 * css_sch_is_valid() - check if a subchannel is valid
374 * @schib: subchannel information block for the subchannel
375 */
376int css_sch_is_valid(struct schib *schib)
377{
378 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
379 return 0;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200380 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
381 return 0;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100382 return 1;
383}
384EXPORT_SYMBOL_GPL(css_sch_is_valid);
385
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200386static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
387{
388 struct schib schib;
389
390 if (!slow) {
391 /* Will be done on the slow path. */
392 return -EAGAIN;
393 }
Sebastian Ottcec85462012-10-15 12:24:56 +0200394 if (stsch_err(schid, &schib)) {
395 /* Subchannel is not provided. */
396 return -ENXIO;
397 }
398 if (!css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200399 /* Unusable - ignore. */
400 return 0;
401 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100402 CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
403 schid.sch_no);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200404
405 return css_probe_device(schid);
406}
407
Cornelia Huckc820de32008-07-14 09:58:45 +0200408static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
409{
410 int ret = 0;
411
412 if (sch->driver) {
413 if (sch->driver->sch_event)
414 ret = sch->driver->sch_event(sch, slow);
415 else
416 dev_dbg(&sch->dev,
417 "Got subchannel machine check but "
418 "no sch_event handler provided.\n");
419 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100420 if (ret != 0 && ret != -EAGAIN) {
421 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
422 sch->schid.ssid, sch->schid.sch_no, ret);
423 }
Cornelia Huckc820de32008-07-14 09:58:45 +0200424 return ret;
425}
426
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200427static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200428{
429 struct subchannel *sch;
430 int ret;
431
432 sch = get_subchannel_by_schid(schid);
433 if (sch) {
434 ret = css_evaluate_known_subchannel(sch, slow);
435 put_device(&sch->dev);
436 } else
437 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200438 if (ret == -EAGAIN)
439 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Sebastian Ott817e5002011-12-01 13:32:19 +0100442/**
443 * css_sched_sch_todo - schedule a subchannel operation
444 * @sch: subchannel
445 * @todo: todo
446 *
447 * Schedule the operation identified by @todo to be performed on the slow path
448 * workqueue. Do nothing if another operation with higher priority is already
449 * scheduled. Needs to be called with subchannel lock held.
450 */
451void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
452{
453 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
454 sch->schid.ssid, sch->schid.sch_no, todo);
455 if (sch->todo >= todo)
456 return;
457 /* Get workqueue ref. */
458 if (!get_device(&sch->dev))
459 return;
460 sch->todo = todo;
461 if (!queue_work(cio_work_q, &sch->todo_work)) {
462 /* Already queued, release workqueue ref. */
463 put_device(&sch->dev);
464 }
465}
Sebastian Ott01c5e6d2012-08-28 16:42:37 +0200466EXPORT_SYMBOL_GPL(css_sched_sch_todo);
Sebastian Ott817e5002011-12-01 13:32:19 +0100467
468static void css_sch_todo(struct work_struct *work)
469{
470 struct subchannel *sch;
471 enum sch_todo todo;
472 int ret;
473
474 sch = container_of(work, struct subchannel, todo_work);
475 /* Find out todo. */
476 spin_lock_irq(sch->lock);
477 todo = sch->todo;
478 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
479 sch->schid.sch_no, todo);
480 sch->todo = SCH_TODO_NOTHING;
481 spin_unlock_irq(sch->lock);
482 /* Perform todo. */
483 switch (todo) {
484 case SCH_TODO_NOTHING:
485 break;
486 case SCH_TODO_EVAL:
487 ret = css_evaluate_known_subchannel(sch, 1);
488 if (ret == -EAGAIN) {
489 spin_lock_irq(sch->lock);
490 css_sched_sch_todo(sch, todo);
491 spin_unlock_irq(sch->lock);
492 }
493 break;
494 case SCH_TODO_UNREG:
495 css_sch_device_unregister(sch);
496 break;
497 }
498 /* Release workqueue ref. */
499 put_device(&sch->dev);
500}
501
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200502static struct idset *slow_subchannel_set;
503static spinlock_t slow_subchannel_lock;
Sebastian Ott25530552009-09-22 22:58:34 +0200504static wait_queue_head_t css_eval_wq;
505static atomic_t css_eval_scheduled;
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200506
507static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700508{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200509 spin_lock_init(&slow_subchannel_lock);
Sebastian Ott25530552009-09-22 22:58:34 +0200510 atomic_set(&css_eval_scheduled, 0);
511 init_waitqueue_head(&css_eval_wq);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200512 slow_subchannel_set = idset_sch_new();
513 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200514 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200515 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200517 return 0;
518}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100520static int slow_eval_known_fn(struct subchannel *sch, void *data)
521{
522 int eval;
523 int rc;
524
525 spin_lock_irq(&slow_subchannel_lock);
526 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
527 idset_sch_del(slow_subchannel_set, sch->schid);
528 spin_unlock_irq(&slow_subchannel_lock);
529 if (eval) {
530 rc = css_evaluate_known_subchannel(sch, 1);
531 if (rc == -EAGAIN)
532 css_schedule_eval(sch->schid);
533 }
534 return 0;
535}
536
537static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
538{
539 int eval;
540 int rc = 0;
541
542 spin_lock_irq(&slow_subchannel_lock);
543 eval = idset_sch_contains(slow_subchannel_set, schid);
544 idset_sch_del(slow_subchannel_set, schid);
545 spin_unlock_irq(&slow_subchannel_lock);
546 if (eval) {
547 rc = css_evaluate_new_subchannel(schid, 1);
548 switch (rc) {
549 case -EAGAIN:
550 css_schedule_eval(schid);
551 rc = 0;
552 break;
553 case -ENXIO:
554 case -ENOMEM:
555 case -EIO:
556 /* These should abort looping */
Sebastian Otteb072a72013-08-29 20:31:06 +0200557 spin_lock_irq(&slow_subchannel_lock);
Sebastian Ottcec85462012-10-15 12:24:56 +0200558 idset_sch_del_subseq(slow_subchannel_set, schid);
Sebastian Otteb072a72013-08-29 20:31:06 +0200559 spin_unlock_irq(&slow_subchannel_lock);
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100560 break;
561 default:
562 rc = 0;
563 }
Peter Oberparleiterb207f5a2013-11-26 14:57:13 +0100564 /* Allow scheduling here since the containing loop might
565 * take a while. */
566 cond_resched();
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100567 }
568 return rc;
569}
570
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200571static void css_slow_path_func(struct work_struct *unused)
572{
Sebastian Ott25530552009-09-22 22:58:34 +0200573 unsigned long flags;
574
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200575 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100576 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
577 NULL);
Sebastian Ott25530552009-09-22 22:58:34 +0200578 spin_lock_irqsave(&slow_subchannel_lock, flags);
579 if (idset_is_empty(slow_subchannel_set)) {
580 atomic_set(&css_eval_scheduled, 0);
581 wake_up(&css_eval_wq);
582 }
583 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584}
585
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100586static DECLARE_DELAYED_WORK(slow_path_work, css_slow_path_func);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100587struct workqueue_struct *cio_work_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200589void css_schedule_eval(struct subchannel_id schid)
590{
591 unsigned long flags;
592
593 spin_lock_irqsave(&slow_subchannel_lock, flags);
594 idset_sch_add(slow_subchannel_set, schid);
Sebastian Ott25530552009-09-22 22:58:34 +0200595 atomic_set(&css_eval_scheduled, 1);
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100596 queue_delayed_work(cio_work_q, &slow_path_work, 0);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200597 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
598}
599
600void css_schedule_eval_all(void)
601{
602 unsigned long flags;
603
604 spin_lock_irqsave(&slow_subchannel_lock, flags);
605 idset_fill(slow_subchannel_set);
Sebastian Ott25530552009-09-22 22:58:34 +0200606 atomic_set(&css_eval_scheduled, 1);
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100607 queue_delayed_work(cio_work_q, &slow_path_work, 0);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200608 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
609}
610
Sebastian Ott703e5c92009-09-22 22:58:38 +0200611static int __unset_registered(struct device *dev, void *data)
612{
613 struct idset *set = data;
614 struct subchannel *sch = to_subchannel(dev);
615
616 idset_sch_del(set, sch->schid);
617 return 0;
618}
619
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100620void css_schedule_eval_all_unreg(unsigned long delay)
Sebastian Ott703e5c92009-09-22 22:58:38 +0200621{
622 unsigned long flags;
623 struct idset *unreg_set;
624
625 /* Find unregistered subchannels. */
626 unreg_set = idset_sch_new();
627 if (!unreg_set) {
628 /* Fallback. */
629 css_schedule_eval_all();
630 return;
631 }
632 idset_fill(unreg_set);
633 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
634 /* Apply to slow_subchannel_set. */
635 spin_lock_irqsave(&slow_subchannel_lock, flags);
636 idset_add_set(slow_subchannel_set, unreg_set);
637 atomic_set(&css_eval_scheduled, 1);
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100638 queue_delayed_work(cio_work_q, &slow_path_work, delay);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200639 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
640 idset_free(unreg_set);
641}
642
Cornelia Huck22806dc2008-04-17 07:45:59 +0200643void css_wait_for_slow_path(void)
644{
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100645 flush_workqueue(cio_work_q);
Cornelia Huck22806dc2008-04-17 07:45:59 +0200646}
647
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200648/* Schedule reprobing of all unregistered subchannels. */
649void css_schedule_reprobe(void)
650{
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100651 /* Schedule with a delay to allow merging of subsequent calls. */
652 css_schedule_eval_all_unreg(1 * HZ);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200653}
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200654EXPORT_SYMBOL_GPL(css_schedule_reprobe);
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 * Called from the machine check handler for subchannel report words.
658 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200659static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800661 struct subchannel_id mchk_schid;
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100662 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663
Cornelia Huckc1156182008-07-14 09:58:46 +0200664 if (overflow) {
665 css_schedule_eval_all();
666 return;
667 }
668 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
669 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
670 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
671 crw0->erc, crw0->rsid);
672 if (crw1)
673 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
674 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
675 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
676 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800677 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200678 mchk_schid.sch_no = crw0->rsid;
679 if (crw1)
Sebastian Ott8d7bfb42010-12-01 10:08:02 +0100680 mchk_schid.ssid = (crw1->rsid >> 4) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800681
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100682 if (crw0->erc == CRW_ERC_PMOD) {
683 sch = get_subchannel_by_schid(mchk_schid);
684 if (sch) {
685 css_update_ssd_info(sch);
686 put_device(&sch->dev);
687 }
688 }
Cornelia Huckc1156182008-07-14 09:58:46 +0200689 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 * Since we are always presented with IPI in the CRW, we have to
691 * use stsch() to find out if the subchannel in question has come
692 * or gone.
693 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200694 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695}
696
697static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800698css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699{
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200700 struct cpuid cpu_id;
701
Cornelia Huck75784c02008-07-14 09:58:57 +0200702 if (css_general_characteristics.mcss) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800703 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
704 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
705 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706#ifdef CONFIG_SMP
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100707 css->global_pgid.pgid_high.cpu_addr = stap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800709 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710#endif
711 }
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200712 get_cpu_id(&cpu_id);
713 css->global_pgid.cpu_id = cpu_id.ident;
714 css->global_pgid.cpu_model = cpu_id.machine;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800715 css->global_pgid.tod_high = tod_high;
716
717}
718
Cornelia Huck3b793062006-01-06 00:19:26 -0800719static void
720channel_subsystem_release(struct device *dev)
721{
722 struct channel_subsystem *css;
723
724 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800725 mutex_destroy(&css->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200726 if (css->pseudo_subchannel) {
727 /* Implies that it has been generated but never registered. */
728 css_subchannel_release(&css->pseudo_subchannel->dev);
729 css->pseudo_subchannel = NULL;
730 }
Cornelia Huck3b793062006-01-06 00:19:26 -0800731 kfree(css);
732}
733
Cornelia Huck495a5b42006-03-24 03:15:14 -0800734static ssize_t
735css_cm_enable_show(struct device *dev, struct device_attribute *attr,
736 char *buf)
737{
738 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200739 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800740
741 if (!css)
742 return 0;
Michael Ernst8284fb192008-04-17 07:46:01 +0200743 mutex_lock(&css->mutex);
744 ret = sprintf(buf, "%x\n", css->cm_enabled);
745 mutex_unlock(&css->mutex);
746 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800747}
748
749static ssize_t
750css_cm_enable_store(struct device *dev, struct device_attribute *attr,
751 const char *buf, size_t count)
752{
753 struct channel_subsystem *css = to_css(dev);
754 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +0200755 unsigned long val;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800756
Jingoo Han01787222013-07-22 10:18:15 +0900757 ret = kstrtoul(buf, 16, &val);
Cornelia Huck2f972202008-04-30 13:38:33 +0200758 if (ret)
759 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200760 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200761 switch (val) {
762 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800763 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
764 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200765 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800766 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
767 break;
768 default:
769 ret = -EINVAL;
770 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200771 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800772 return ret < 0 ? ret : count;
773}
774
775static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
776
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100777static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800778{
779 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100780 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200781 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800782
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200783 css = channel_subsystems[nr];
784 memset(css, 0, sizeof(struct channel_subsystem));
785 css->pseudo_subchannel =
786 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
787 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100788 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200789 css->pseudo_subchannel->dev.parent = &css->device;
790 css->pseudo_subchannel->dev.release = css_subchannel_release;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200791 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100792 mutex_init(&css->pseudo_subchannel->reg_mutex);
Sebastian Otte5dcf002013-04-13 13:08:01 +0200793 ret = css_sch_create_locks(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100794 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200795 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100796 return ret;
797 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200798 mutex_init(&css->mutex);
799 css->valid = 1;
800 css->cssid = nr;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200801 dev_set_name(&css->device, "css%x", nr);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200802 css->device.release = channel_subsystem_release;
Heiko Carstens1aae0562013-01-30 09:49:40 +0100803 tod_high = (u32) (get_tod_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200804 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
807
Cornelia Hucka55360d2007-10-12 16:11:20 +0200808static int css_reboot_event(struct notifier_block *this,
809 unsigned long event,
810 void *ptr)
811{
812 int ret, i;
813
814 ret = NOTIFY_DONE;
815 for (i = 0; i <= __MAX_CSSID; i++) {
816 struct channel_subsystem *css;
817
818 css = channel_subsystems[i];
Michael Ernst8284fb192008-04-17 07:46:01 +0200819 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200820 if (css->cm_enabled)
821 if (chsc_secm(css, 0))
822 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200823 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200824 }
825
826 return ret;
827}
828
829static struct notifier_block css_reboot_notifier = {
830 .notifier_call = css_reboot_event,
831};
832
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200834 * Since the css devices are neither on a bus nor have a class
835 * nor have a special device type, we cannot stop/restart channel
836 * path measurements via the normal suspend/resume callbacks, but have
837 * to use notifiers.
838 */
839static int css_power_event(struct notifier_block *this, unsigned long event,
840 void *ptr)
841{
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200842 int ret, i;
843
844 switch (event) {
845 case PM_HIBERNATION_PREPARE:
846 case PM_SUSPEND_PREPARE:
847 ret = NOTIFY_DONE;
848 for (i = 0; i <= __MAX_CSSID; i++) {
849 struct channel_subsystem *css;
850
851 css = channel_subsystems[i];
852 mutex_lock(&css->mutex);
853 if (!css->cm_enabled) {
854 mutex_unlock(&css->mutex);
855 continue;
856 }
Akinobu Mitaf0c077a2011-07-08 20:53:36 +0200857 ret = __chsc_do_secm(css, 0);
858 ret = notifier_from_errno(ret);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200859 mutex_unlock(&css->mutex);
860 }
861 break;
862 case PM_POST_HIBERNATION:
863 case PM_POST_SUSPEND:
864 ret = NOTIFY_DONE;
865 for (i = 0; i <= __MAX_CSSID; i++) {
866 struct channel_subsystem *css;
867
868 css = channel_subsystems[i];
869 mutex_lock(&css->mutex);
870 if (!css->cm_enabled) {
871 mutex_unlock(&css->mutex);
872 continue;
873 }
Akinobu Mitaf0c077a2011-07-08 20:53:36 +0200874 ret = __chsc_do_secm(css, 1);
875 ret = notifier_from_errno(ret);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200876 mutex_unlock(&css->mutex);
877 }
878 /* search for subchannels, which appeared during hibernation */
879 css_schedule_reprobe();
880 break;
881 default:
882 ret = NOTIFY_DONE;
883 }
884 return ret;
885
886}
887static struct notifier_block css_power_notifier = {
888 .notifier_call = css_power_event,
889};
890
891/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 * Now that the driver core is running, we can setup our channel subsystem.
Sebastian Ott14556b32013-04-13 13:03:54 +0200893 * The struct subchannel's are created during probing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 */
Sebastian Ott2f176442009-09-22 22:58:33 +0200895static int __init css_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800897 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898
Sebastian Ott34aec072010-10-25 16:10:28 +0200899 ret = chsc_init();
900 if (ret)
901 return ret;
902
Sebastian Ott34196f82010-10-25 16:10:29 +0200903 chsc_determine_css_characteristics();
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200904 /* Try to enable MSS. */
905 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott818c2722010-04-22 17:17:03 +0200906 if (ret)
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200907 max_ssid = 0;
Sebastian Ott818c2722010-04-22 17:17:03 +0200908 else /* Success. */
909 max_ssid = __MAX_SSID;
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200910
Cornelia Huck4434a382007-07-27 12:29:21 +0200911 ret = slow_subchannel_init();
912 if (ret)
913 goto out;
914
Heiko Carstensf5daba12009-03-26 15:24:01 +0100915 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
Cornelia Huckc1156182008-07-14 09:58:46 +0200916 if (ret)
917 goto out;
918
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 if ((ret = bus_register(&css_bus_type)))
920 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Cornelia Hucka28c6942006-01-06 00:19:23 -0800922 /* Setup css structure. */
923 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200924 struct channel_subsystem *css;
925
926 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
927 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800928 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800929 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800930 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200931 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100932 ret = setup_css(i);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200933 if (ret) {
934 kfree(channel_subsystems[i]);
935 goto out_unregister;
936 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200937 ret = device_register(&css->device);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200938 if (ret) {
939 put_device(&css->device);
940 goto out_unregister;
941 }
Cornelia Huck75784c02008-07-14 09:58:57 +0200942 if (css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200943 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200944 &dev_attr_cm_enable);
945 if (ret)
946 goto out_device;
947 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200948 ret = device_register(&css->pseudo_subchannel->dev);
Sebastian Ottc6304932009-09-11 10:28:38 +0200949 if (ret) {
950 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100951 goto out_file;
Sebastian Ottc6304932009-09-11 10:28:38 +0200952 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800953 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200954 ret = register_reboot_notifier(&css_reboot_notifier);
955 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +0200956 goto out_unregister;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200957 ret = register_pm_notifier(&css_power_notifier);
958 if (ret) {
959 unregister_reboot_notifier(&css_reboot_notifier);
960 goto out_unregister;
961 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 css_init_done = 1;
963
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200964 /* Enable default isc for I/O subchannels. */
Cornelia Huck6ef556c2008-07-14 09:59:01 +0200965 isc_register(IO_SCH_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 return 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100968out_file:
Cornelia Hucka2164b82008-09-09 12:38:57 +0200969 if (css_chsc_characteristics.secm)
970 device_remove_file(&channel_subsystems[i]->device,
971 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200972out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200973 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800974out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800975 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200976 struct channel_subsystem *css;
977
Cornelia Hucka28c6942006-01-06 00:19:23 -0800978 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200979 css = channel_subsystems[i];
980 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200981 css->pseudo_subchannel = NULL;
Cornelia Huck75784c02008-07-14 09:58:57 +0200982 if (css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200983 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800984 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200985 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800986 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 bus_unregister(&css_bus_type);
988out:
Sebastian Ott34aec072010-10-25 16:10:28 +0200989 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c82009-09-22 22:58:36 +0200990 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +0200991 chsc_init_cleanup();
Michael Ernste6d5a422008-12-25 13:39:36 +0100992 pr_alert("The CSS device driver initialization failed with "
993 "errno=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return ret;
995}
996
Sebastian Ott2f176442009-09-22 22:58:33 +0200997static void __init css_bus_cleanup(void)
998{
999 struct channel_subsystem *css;
1000 int i;
1001
1002 for (i = 0; i <= __MAX_CSSID; i++) {
1003 css = channel_subsystems[i];
1004 device_unregister(&css->pseudo_subchannel->dev);
1005 css->pseudo_subchannel = NULL;
1006 if (css_chsc_characteristics.secm)
1007 device_remove_file(&css->device, &dev_attr_cm_enable);
1008 device_unregister(&css->device);
1009 }
1010 bus_unregister(&css_bus_type);
Sebastian Ott34aec072010-10-25 16:10:28 +02001011 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c82009-09-22 22:58:36 +02001012 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +02001013 chsc_init_cleanup();
Sebastian Ott2f176442009-09-22 22:58:33 +02001014 isc_unregister(IO_SCH_ISC);
1015}
1016
1017static int __init channel_subsystem_init(void)
1018{
1019 int ret;
1020
1021 ret = css_bus_init();
1022 if (ret)
1023 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001024 cio_work_q = create_singlethread_workqueue("cio");
1025 if (!cio_work_q) {
1026 ret = -ENOMEM;
1027 goto out_bus;
1028 }
Sebastian Ott2f176442009-09-22 22:58:33 +02001029 ret = io_subchannel_init();
1030 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001031 goto out_wq;
Sebastian Ott2f176442009-09-22 22:58:33 +02001032
1033 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001034out_wq:
1035 destroy_workqueue(cio_work_q);
1036out_bus:
1037 css_bus_cleanup();
1038 return ret;
Sebastian Ott2f176442009-09-22 22:58:33 +02001039}
1040subsys_initcall(channel_subsystem_init);
1041
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001042static int css_settle(struct device_driver *drv, void *unused)
1043{
1044 struct css_driver *cssdrv = to_cssdriver(drv);
1045
1046 if (cssdrv->settle)
Sebastian Ottb4c70722010-02-26 22:37:27 +01001047 return cssdrv->settle();
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001048 return 0;
1049}
1050
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001051int css_complete_work(void)
Sebastian Ott879acca52010-02-26 22:37:25 +01001052{
1053 int ret;
1054
1055 /* Wait for the evaluation of subchannels to finish. */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001056 ret = wait_event_interruptible(css_eval_wq,
1057 atomic_read(&css_eval_scheduled) == 0);
1058 if (ret)
1059 return -EINTR;
Sebastian Ott879acca52010-02-26 22:37:25 +01001060 flush_workqueue(cio_work_q);
1061 /* Wait for the subchannel type specific initialization to finish */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001062 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
Sebastian Ott879acca52010-02-26 22:37:25 +01001063}
1064
1065
Sebastian Ott2f176442009-09-22 22:58:33 +02001066/*
1067 * Wait for the initialization of devices to finish, to make sure we are
1068 * done with our setup if the search for the root device starts.
1069 */
1070static int __init channel_subsystem_init_sync(void)
1071{
Sebastian Ott14556b32013-04-13 13:03:54 +02001072 /* Register subchannels which are already in use. */
1073 cio_register_early_subchannels();
Sebastian Ott703e5c92009-09-22 22:58:38 +02001074 /* Start initial subchannel evaluation. */
1075 css_schedule_eval_all();
Sebastian Ott879acca52010-02-26 22:37:25 +01001076 css_complete_work();
1077 return 0;
Sebastian Ott2f176442009-09-22 22:58:33 +02001078}
1079subsys_initcall_sync(channel_subsystem_init_sync);
1080
Sebastian Ott889ee952010-04-22 17:17:04 +02001081void channel_subsystem_reinit(void)
1082{
Sebastian Ott62da1772010-10-25 16:10:32 +02001083 struct channel_path *chp;
1084 struct chp_id chpid;
1085
Sebastian Ott889ee952010-04-22 17:17:04 +02001086 chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott62da1772010-10-25 16:10:32 +02001087 chp_id_for_each(&chpid) {
1088 chp = chpid_to_chp(chpid);
Peter Oberparleitercce0eac2013-03-11 12:58:18 +01001089 if (chp)
1090 chp_update_desc(chp);
Sebastian Ott62da1772010-10-25 16:10:32 +02001091 }
Sebastian Ott889ee952010-04-22 17:17:04 +02001092}
1093
Sebastian Ott879acca52010-02-26 22:37:25 +01001094#ifdef CONFIG_PROC_FS
1095static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1096 size_t count, loff_t *ppos)
1097{
Sebastian Ottb4c70722010-02-26 22:37:27 +01001098 int ret;
1099
Sebastian Ott879acca52010-02-26 22:37:25 +01001100 /* Handle pending CRW's. */
1101 crw_wait_for_channel_report();
Sebastian Ottb4c70722010-02-26 22:37:27 +01001102 ret = css_complete_work();
1103
1104 return ret ? ret : count;
Sebastian Ott879acca52010-02-26 22:37:25 +01001105}
1106
1107static const struct file_operations cio_settle_proc_fops = {
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +02001108 .open = nonseekable_open,
Sebastian Ott879acca52010-02-26 22:37:25 +01001109 .write = cio_settle_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001110 .llseek = no_llseek,
Sebastian Ott879acca52010-02-26 22:37:25 +01001111};
1112
1113static int __init cio_settle_init(void)
1114{
1115 struct proc_dir_entry *entry;
1116
1117 entry = proc_create("cio_settle", S_IWUSR, NULL,
1118 &cio_settle_proc_fops);
1119 if (!entry)
1120 return -ENOMEM;
1121 return 0;
1122}
1123device_initcall(cio_settle_init);
1124#endif /*CONFIG_PROC_FS*/
1125
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001126int sch_is_pseudo_sch(struct subchannel *sch)
1127{
1128 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1129}
1130
Cornelia Huckf08adc02008-07-14 09:59:03 +02001131static int css_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132{
Cornelia Huck084325d2008-01-26 14:10:38 +01001133 struct subchannel *sch = to_subchannel(dev);
1134 struct css_driver *driver = to_cssdriver(drv);
Cornelia Huckf08adc02008-07-14 09:59:03 +02001135 struct css_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
Cornelia Huckf08adc02008-07-14 09:59:03 +02001137 for (id = driver->subchannel_type; id->match_flags; id++) {
1138 if (sch->st == id->type)
1139 return 1;
1140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
1142 return 0;
1143}
1144
Cornelia Huck98c13c22008-01-26 14:10:40 +01001145static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001146{
1147 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +01001148 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001149
1150 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +01001151 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001152 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1153 if (ret)
1154 sch->driver = NULL;
1155 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001156}
1157
Cornelia Huck98c13c22008-01-26 14:10:40 +01001158static int css_remove(struct device *dev)
1159{
1160 struct subchannel *sch;
1161 int ret;
1162
1163 sch = to_subchannel(dev);
1164 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1165 sch->driver = NULL;
1166 return ret;
1167}
1168
1169static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001170{
1171 struct subchannel *sch;
1172
1173 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001174 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001175 sch->driver->shutdown(sch);
1176}
1177
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001178static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1179{
1180 struct subchannel *sch = to_subchannel(dev);
1181 int ret;
1182
1183 ret = add_uevent_var(env, "ST=%01X", sch->st);
1184 if (ret)
1185 return ret;
1186 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1187 return ret;
1188}
1189
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001190static int css_pm_prepare(struct device *dev)
1191{
1192 struct subchannel *sch = to_subchannel(dev);
1193 struct css_driver *drv;
1194
1195 if (mutex_is_locked(&sch->reg_mutex))
1196 return -EAGAIN;
1197 if (!sch->dev.driver)
1198 return 0;
1199 drv = to_cssdriver(sch->dev.driver);
1200 /* Notify drivers that they may not register children. */
1201 return drv->prepare ? drv->prepare(sch) : 0;
1202}
1203
1204static void css_pm_complete(struct device *dev)
1205{
1206 struct subchannel *sch = to_subchannel(dev);
1207 struct css_driver *drv;
1208
1209 if (!sch->dev.driver)
1210 return;
1211 drv = to_cssdriver(sch->dev.driver);
1212 if (drv->complete)
1213 drv->complete(sch);
1214}
1215
1216static int css_pm_freeze(struct device *dev)
1217{
1218 struct subchannel *sch = to_subchannel(dev);
1219 struct css_driver *drv;
1220
1221 if (!sch->dev.driver)
1222 return 0;
1223 drv = to_cssdriver(sch->dev.driver);
1224 return drv->freeze ? drv->freeze(sch) : 0;
1225}
1226
1227static int css_pm_thaw(struct device *dev)
1228{
1229 struct subchannel *sch = to_subchannel(dev);
1230 struct css_driver *drv;
1231
1232 if (!sch->dev.driver)
1233 return 0;
1234 drv = to_cssdriver(sch->dev.driver);
1235 return drv->thaw ? drv->thaw(sch) : 0;
1236}
1237
1238static int css_pm_restore(struct device *dev)
1239{
1240 struct subchannel *sch = to_subchannel(dev);
1241 struct css_driver *drv;
1242
Sebastian Otteb4f5d92010-10-25 16:10:33 +02001243 css_update_ssd_info(sch);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001244 if (!sch->dev.driver)
1245 return 0;
1246 drv = to_cssdriver(sch->dev.driver);
1247 return drv->restore ? drv->restore(sch) : 0;
1248}
1249
Alexey Dobriyan47145212009-12-14 18:00:08 -08001250static const struct dev_pm_ops css_pm_ops = {
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001251 .prepare = css_pm_prepare,
1252 .complete = css_pm_complete,
1253 .freeze = css_pm_freeze,
1254 .thaw = css_pm_thaw,
1255 .restore = css_pm_restore,
1256};
1257
Sebastian Ott3041b6a2011-03-15 17:08:31 +01001258static struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001259 .name = "css",
1260 .match = css_bus_match,
1261 .probe = css_probe,
1262 .remove = css_remove,
1263 .shutdown = css_shutdown,
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001264 .uevent = css_uevent,
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001265 .pm = &css_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266};
1267
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001268/**
1269 * css_driver_register - register a css driver
1270 * @cdrv: css driver to register
1271 *
1272 * This is mainly a wrapper around driver_register that sets name
1273 * and bus_type in the embedded struct device_driver correctly.
1274 */
1275int css_driver_register(struct css_driver *cdrv)
1276{
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001277 cdrv->drv.bus = &css_bus_type;
1278 return driver_register(&cdrv->drv);
1279}
1280EXPORT_SYMBOL_GPL(css_driver_register);
1281
1282/**
1283 * css_driver_unregister - unregister a css driver
1284 * @cdrv: css driver to unregister
1285 *
1286 * This is a wrapper around driver_unregister.
1287 */
1288void css_driver_unregister(struct css_driver *cdrv)
1289{
1290 driver_unregister(&cdrv->drv);
1291}
1292EXPORT_SYMBOL_GPL(css_driver_unregister);
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294MODULE_LICENSE("GPL");