blob: 3d2b20ee613f8e38843bde3a2fed32a5ba05e347 [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);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080047 do {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080048 do {
49 ret = fn(schid, data);
50 if (ret)
51 break;
52 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
53 schid.sch_no = 0;
54 } while (schid.ssid++ < max_ssid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080055 return ret;
56}
57
Peter Oberparleitere82a1562008-01-26 14:10:48 +010058struct cb_data {
59 void *data;
60 struct idset *set;
61 int (*fn_known_sch)(struct subchannel *, void *);
62 int (*fn_unknown_sch)(struct subchannel_id, void *);
63};
64
65static int call_fn_known_sch(struct device *dev, void *data)
66{
67 struct subchannel *sch = to_subchannel(dev);
68 struct cb_data *cb = data;
69 int rc = 0;
70
Peter Oberparleiter47d30672013-11-26 14:59:21 +010071 if (cb->set)
72 idset_sch_del(cb->set, sch->schid);
Peter Oberparleitere82a1562008-01-26 14:10:48 +010073 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
Peter Oberparleiter47d30672013-11-26 14:59:21 +0100118 if (fn_known && !fn_unknown) {
119 /* Skip idset allocation in case of known-only loop. */
120 cb.set = NULL;
121 return bus_for_each_dev(&css_bus_type, NULL, &cb,
122 call_fn_known_sch);
123 }
124
Sebastian Ott90ac24a2009-03-26 15:24:11 +0100125 cb.set = idset_sch_new();
126 if (!cb.set)
127 /* fall back to brute force scanning in case of oom */
128 return for_each_subchannel(call_fn_all_sch, &cb);
129
130 idset_fill(cb.set);
131
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100132 /* Process registered subchannels. */
133 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
134 if (rc)
135 goto out;
136 /* Process unregistered subchannels. */
137 if (fn_unknown)
138 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
139out:
140 idset_free(cb.set);
141
142 return rc;
143}
144
Peter Oberparleiter390935a2009-12-07 12:51:18 +0100145static void css_sch_todo(struct work_struct *work);
146
Sebastian Otte5dcf002013-04-13 13:08:01 +0200147static int css_sch_create_locks(struct subchannel *sch)
148{
149 sch->lock = kmalloc(sizeof(*sch->lock), GFP_KERNEL);
150 if (!sch->lock)
151 return -ENOMEM;
152
153 spin_lock_init(sch->lock);
154 mutex_init(&sch->reg_mutex);
155
156 return 0;
157}
158
Sebastian Ottc135ad12013-04-13 12:58:55 +0200159static void css_subchannel_release(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Sebastian Ott863fc842013-04-13 13:01:50 +0200161 struct subchannel *sch = to_subchannel(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
Sebastian Ott863fc842013-04-13 13:01:50 +0200163 sch->config.intparm = 0;
164 cio_commit_config(sch);
165 kfree(sch->lock);
166 kfree(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Sebastian Ott863fc842013-04-13 13:01:50 +0200169struct subchannel *css_alloc_subchannel(struct subchannel_id schid)
Sebastian Ottc135ad12013-04-13 12:58:55 +0200170{
171 struct subchannel *sch;
172 int ret;
173
Sebastian Otte5dcf002013-04-13 13:08:01 +0200174 sch = kzalloc(sizeof(*sch), GFP_KERNEL | GFP_DMA);
175 if (!sch)
Sebastian Ottc135ad12013-04-13 12:58:55 +0200176 return ERR_PTR(-ENOMEM);
Sebastian Otte5dcf002013-04-13 13:08:01 +0200177
178 ret = cio_validate_subchannel(sch, schid);
179 if (ret < 0)
180 goto err;
181
182 ret = css_sch_create_locks(sch);
183 if (ret)
184 goto err;
185
Sebastian Ottc135ad12013-04-13 12:58:55 +0200186 INIT_WORK(&sch->todo_work, css_sch_todo);
187 sch->dev.release = &css_subchannel_release;
188 device_initialize(&sch->dev);
189 return sch;
Sebastian Otte5dcf002013-04-13 13:08:01 +0200190
191err:
192 kfree(sch);
193 return ERR_PTR(ret);
Sebastian Ottc135ad12013-04-13 12:58:55 +0200194}
195
Cornelia Huck07c6a332007-07-27 12:29:10 +0200196static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200197{
198 int ret;
199
200 mutex_lock(&sch->reg_mutex);
Sebastian Ott6ee4fec2009-09-11 10:28:25 +0200201 dev_set_name(&sch->dev, "0.%x.%04x", sch->schid.ssid,
202 sch->schid.sch_no);
Sebastian Ottc135ad12013-04-13 12:58:55 +0200203 ret = device_add(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200204 mutex_unlock(&sch->reg_mutex);
205 return ret;
206}
207
Cornelia Huck44a1c192008-07-14 09:58:47 +0200208/**
209 * css_sch_device_unregister - unregister a subchannel
210 * @sch: subchannel to be unregistered
211 */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200212void css_sch_device_unregister(struct subchannel *sch)
213{
214 mutex_lock(&sch->reg_mutex);
Sebastian Ottef60cd12008-07-14 09:59:20 +0200215 if (device_is_registered(&sch->dev))
216 device_unregister(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200217 mutex_unlock(&sch->reg_mutex);
218}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200219EXPORT_SYMBOL_GPL(css_sch_device_unregister);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200220
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200221static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
222{
223 int i;
224 int mask;
225
226 memset(ssd, 0, sizeof(struct chsc_ssd_info));
227 ssd->path_mask = pmcw->pim;
228 for (i = 0; i < 8; i++) {
229 mask = 0x80 >> i;
230 if (pmcw->pim & mask) {
231 chp_id_init(&ssd->chpid[i]);
232 ssd->chpid[i].id = pmcw->chpid[i];
233 }
234 }
235}
236
237static void ssd_register_chpids(struct chsc_ssd_info *ssd)
238{
239 int i;
240 int mask;
241
242 for (i = 0; i < 8; i++) {
243 mask = 0x80 >> i;
244 if (ssd->path_mask & mask)
245 if (!chp_is_registered(ssd->chpid[i]))
246 chp_new(ssd->chpid[i]);
247 }
248}
249
250void css_update_ssd_info(struct subchannel *sch)
251{
252 int ret;
253
Sebastian Ott14556b32013-04-13 13:03:54 +0200254 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
255 if (ret)
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200256 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
Sebastian Ott14556b32013-04-13 13:03:54 +0200257
258 ssd_register_chpids(&sch->ssd_info);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200259}
260
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200261static ssize_t type_show(struct device *dev, struct device_attribute *attr,
262 char *buf)
263{
264 struct subchannel *sch = to_subchannel(dev);
265
266 return sprintf(buf, "%01x\n", sch->st);
267}
268
269static DEVICE_ATTR(type, 0444, type_show, NULL);
270
271static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
272 char *buf)
273{
274 struct subchannel *sch = to_subchannel(dev);
275
276 return sprintf(buf, "css:t%01X\n", sch->st);
277}
278
279static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
280
281static struct attribute *subch_attrs[] = {
282 &dev_attr_type.attr,
283 &dev_attr_modalias.attr,
284 NULL,
285};
286
287static struct attribute_group subch_attr_group = {
288 .attrs = subch_attrs,
289};
290
David Brownella4dbd672009-06-24 10:06:31 -0700291static const struct attribute_group *default_subch_attr_groups[] = {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200292 &subch_attr_group,
293 NULL,
294};
295
Sebastian Ott14556b32013-04-13 13:03:54 +0200296int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 int ret;
299
300 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200301 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 sch->dev.bus = &css_bus_type;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200303 sch->dev.groups = default_subch_attr_groups;
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200304 /*
305 * We don't want to generate uevents for I/O subchannels that don't
306 * have a working ccw device behind them since they will be
307 * unregistered before they can be used anyway, so we delay the add
308 * uevent until after device recognition was successful.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200309 * Note that we suppress the uevent for all subchannel types;
310 * the subchannel driver can decide itself when it wants to inform
311 * userspace of its existence.
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200312 */
Ming Leif67f1292009-03-01 21:10:49 +0800313 dev_set_uevent_suppress(&sch->dev, 1);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200314 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200316 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100317 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200318 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
319 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100320 return ret;
321 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200322 if (!sch->driver) {
323 /*
324 * No driver matched. Generate the uevent now so that
325 * a fitting driver module may be loaded based on the
326 * modalias.
327 */
Ming Leif67f1292009-03-01 21:10:49 +0800328 dev_set_uevent_suppress(&sch->dev, 0);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200329 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
330 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 return ret;
332}
333
Sebastian Ott4e5ebd52013-04-13 13:04:49 +0200334static int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 struct subchannel *sch;
Sebastian Ott4e5ebd52013-04-13 13:04:49 +0200337 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Sebastian Ott14556b32013-04-13 13:03:54 +0200339 sch = css_alloc_subchannel(schid);
340 if (IS_ERR(sch))
341 return PTR_ERR(sch);
342
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 ret = css_register_subchannel(sch);
Sebastian Ott863fc842013-04-13 13:01:50 +0200344 if (ret)
345 put_device(&sch->dev);
346
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 return ret;
348}
349
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700350static int
351check_subchannel(struct device * dev, void * data)
352{
353 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800354 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700355
356 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800357 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800361get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct device *dev;
364
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700365 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200366 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700368 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
370
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100371/**
372 * css_sch_is_valid() - check if a subchannel is valid
373 * @schib: subchannel information block for the subchannel
374 */
375int css_sch_is_valid(struct schib *schib)
376{
377 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
378 return 0;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200379 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
380 return 0;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100381 return 1;
382}
383EXPORT_SYMBOL_GPL(css_sch_is_valid);
384
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200385static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
386{
387 struct schib schib;
388
389 if (!slow) {
390 /* Will be done on the slow path. */
391 return -EAGAIN;
392 }
Peter Oberparleiter62e65da2015-12-18 12:58:47 +0100393 if (stsch(schid, &schib)) {
Sebastian Ottcec85462012-10-15 12:24:56 +0200394 /* Subchannel is not provided. */
395 return -ENXIO;
396 }
397 if (!css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200398 /* Unusable - ignore. */
399 return 0;
400 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100401 CIO_MSG_EVENT(4, "event: sch 0.%x.%04x, new\n", schid.ssid,
402 schid.sch_no);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200403
404 return css_probe_device(schid);
405}
406
Cornelia Huckc820de32008-07-14 09:58:45 +0200407static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
408{
409 int ret = 0;
410
411 if (sch->driver) {
412 if (sch->driver->sch_event)
413 ret = sch->driver->sch_event(sch, slow);
414 else
415 dev_dbg(&sch->dev,
416 "Got subchannel machine check but "
417 "no sch_event handler provided.\n");
418 }
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100419 if (ret != 0 && ret != -EAGAIN) {
420 CIO_MSG_EVENT(2, "eval: sch 0.%x.%04x, rc=%d\n",
421 sch->schid.ssid, sch->schid.sch_no, ret);
422 }
Cornelia Huckc820de32008-07-14 09:58:45 +0200423 return ret;
424}
425
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200426static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200427{
428 struct subchannel *sch;
429 int ret;
430
431 sch = get_subchannel_by_schid(schid);
432 if (sch) {
433 ret = css_evaluate_known_subchannel(sch, slow);
434 put_device(&sch->dev);
435 } else
436 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200437 if (ret == -EAGAIN)
438 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
440
Sebastian Ott817e5002011-12-01 13:32:19 +0100441/**
442 * css_sched_sch_todo - schedule a subchannel operation
443 * @sch: subchannel
444 * @todo: todo
445 *
446 * Schedule the operation identified by @todo to be performed on the slow path
447 * workqueue. Do nothing if another operation with higher priority is already
448 * scheduled. Needs to be called with subchannel lock held.
449 */
450void css_sched_sch_todo(struct subchannel *sch, enum sch_todo todo)
451{
452 CIO_MSG_EVENT(4, "sch_todo: sched sch=0.%x.%04x todo=%d\n",
453 sch->schid.ssid, sch->schid.sch_no, todo);
454 if (sch->todo >= todo)
455 return;
456 /* Get workqueue ref. */
457 if (!get_device(&sch->dev))
458 return;
459 sch->todo = todo;
460 if (!queue_work(cio_work_q, &sch->todo_work)) {
461 /* Already queued, release workqueue ref. */
462 put_device(&sch->dev);
463 }
464}
Sebastian Ott01c5e6d2012-08-28 16:42:37 +0200465EXPORT_SYMBOL_GPL(css_sched_sch_todo);
Sebastian Ott817e5002011-12-01 13:32:19 +0100466
467static void css_sch_todo(struct work_struct *work)
468{
469 struct subchannel *sch;
470 enum sch_todo todo;
471 int ret;
472
473 sch = container_of(work, struct subchannel, todo_work);
474 /* Find out todo. */
475 spin_lock_irq(sch->lock);
476 todo = sch->todo;
477 CIO_MSG_EVENT(4, "sch_todo: sch=0.%x.%04x, todo=%d\n", sch->schid.ssid,
478 sch->schid.sch_no, todo);
479 sch->todo = SCH_TODO_NOTHING;
480 spin_unlock_irq(sch->lock);
481 /* Perform todo. */
482 switch (todo) {
483 case SCH_TODO_NOTHING:
484 break;
485 case SCH_TODO_EVAL:
486 ret = css_evaluate_known_subchannel(sch, 1);
487 if (ret == -EAGAIN) {
488 spin_lock_irq(sch->lock);
489 css_sched_sch_todo(sch, todo);
490 spin_unlock_irq(sch->lock);
491 }
492 break;
493 case SCH_TODO_UNREG:
494 css_sch_device_unregister(sch);
495 break;
496 }
497 /* Release workqueue ref. */
498 put_device(&sch->dev);
499}
500
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200501static struct idset *slow_subchannel_set;
502static spinlock_t slow_subchannel_lock;
Sebastian Ott25530552009-09-22 22:58:34 +0200503static wait_queue_head_t css_eval_wq;
504static atomic_t css_eval_scheduled;
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200505
506static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200508 spin_lock_init(&slow_subchannel_lock);
Sebastian Ott25530552009-09-22 22:58:34 +0200509 atomic_set(&css_eval_scheduled, 0);
510 init_waitqueue_head(&css_eval_wq);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200511 slow_subchannel_set = idset_sch_new();
512 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200513 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200514 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200516 return 0;
517}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100519static int slow_eval_known_fn(struct subchannel *sch, void *data)
520{
521 int eval;
522 int rc;
523
524 spin_lock_irq(&slow_subchannel_lock);
525 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
526 idset_sch_del(slow_subchannel_set, sch->schid);
527 spin_unlock_irq(&slow_subchannel_lock);
528 if (eval) {
529 rc = css_evaluate_known_subchannel(sch, 1);
530 if (rc == -EAGAIN)
531 css_schedule_eval(sch->schid);
532 }
533 return 0;
534}
535
536static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
537{
538 int eval;
539 int rc = 0;
540
541 spin_lock_irq(&slow_subchannel_lock);
542 eval = idset_sch_contains(slow_subchannel_set, schid);
543 idset_sch_del(slow_subchannel_set, schid);
544 spin_unlock_irq(&slow_subchannel_lock);
545 if (eval) {
546 rc = css_evaluate_new_subchannel(schid, 1);
547 switch (rc) {
548 case -EAGAIN:
549 css_schedule_eval(schid);
550 rc = 0;
551 break;
552 case -ENXIO:
553 case -ENOMEM:
554 case -EIO:
555 /* These should abort looping */
Sebastian Otteb072a72013-08-29 20:31:06 +0200556 spin_lock_irq(&slow_subchannel_lock);
Sebastian Ottcec85462012-10-15 12:24:56 +0200557 idset_sch_del_subseq(slow_subchannel_set, schid);
Sebastian Otteb072a72013-08-29 20:31:06 +0200558 spin_unlock_irq(&slow_subchannel_lock);
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100559 break;
560 default:
561 rc = 0;
562 }
Peter Oberparleiterb207f5a2013-11-26 14:57:13 +0100563 /* Allow scheduling here since the containing loop might
564 * take a while. */
565 cond_resched();
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100566 }
567 return rc;
568}
569
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200570static void css_slow_path_func(struct work_struct *unused)
571{
Sebastian Ott25530552009-09-22 22:58:34 +0200572 unsigned long flags;
573
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200574 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100575 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
576 NULL);
Sebastian Ott25530552009-09-22 22:58:34 +0200577 spin_lock_irqsave(&slow_subchannel_lock, flags);
578 if (idset_is_empty(slow_subchannel_set)) {
579 atomic_set(&css_eval_scheduled, 0);
580 wake_up(&css_eval_wq);
581 }
582 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100585static DECLARE_DELAYED_WORK(slow_path_work, css_slow_path_func);
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100586struct workqueue_struct *cio_work_q;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200588void css_schedule_eval(struct subchannel_id schid)
589{
590 unsigned long flags;
591
592 spin_lock_irqsave(&slow_subchannel_lock, flags);
593 idset_sch_add(slow_subchannel_set, schid);
Sebastian Ott25530552009-09-22 22:58:34 +0200594 atomic_set(&css_eval_scheduled, 1);
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100595 queue_delayed_work(cio_work_q, &slow_path_work, 0);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200596 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
597}
598
599void css_schedule_eval_all(void)
600{
601 unsigned long flags;
602
603 spin_lock_irqsave(&slow_subchannel_lock, flags);
604 idset_fill(slow_subchannel_set);
Sebastian Ott25530552009-09-22 22:58:34 +0200605 atomic_set(&css_eval_scheduled, 1);
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100606 queue_delayed_work(cio_work_q, &slow_path_work, 0);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200607 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
608}
609
Sebastian Ott703e5c92009-09-22 22:58:38 +0200610static int __unset_registered(struct device *dev, void *data)
611{
612 struct idset *set = data;
613 struct subchannel *sch = to_subchannel(dev);
614
615 idset_sch_del(set, sch->schid);
616 return 0;
617}
618
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100619void css_schedule_eval_all_unreg(unsigned long delay)
Sebastian Ott703e5c92009-09-22 22:58:38 +0200620{
621 unsigned long flags;
622 struct idset *unreg_set;
623
624 /* Find unregistered subchannels. */
625 unreg_set = idset_sch_new();
626 if (!unreg_set) {
627 /* Fallback. */
628 css_schedule_eval_all();
629 return;
630 }
631 idset_fill(unreg_set);
632 bus_for_each_dev(&css_bus_type, NULL, unreg_set, __unset_registered);
633 /* Apply to slow_subchannel_set. */
634 spin_lock_irqsave(&slow_subchannel_lock, flags);
635 idset_add_set(slow_subchannel_set, unreg_set);
636 atomic_set(&css_eval_scheduled, 1);
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100637 queue_delayed_work(cio_work_q, &slow_path_work, delay);
Sebastian Ott703e5c92009-09-22 22:58:38 +0200638 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
639 idset_free(unreg_set);
640}
641
Cornelia Huck22806dc2008-04-17 07:45:59 +0200642void css_wait_for_slow_path(void)
643{
Sebastian Ottbe5d3822010-02-26 22:37:24 +0100644 flush_workqueue(cio_work_q);
Cornelia Huck22806dc2008-04-17 07:45:59 +0200645}
646
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200647/* Schedule reprobing of all unregistered subchannels. */
648void css_schedule_reprobe(void)
649{
Peter Oberparleiter175746e2013-11-26 14:58:08 +0100650 /* Schedule with a delay to allow merging of subsequent calls. */
651 css_schedule_eval_all_unreg(1 * HZ);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200652}
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200653EXPORT_SYMBOL_GPL(css_schedule_reprobe);
654
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 * Called from the machine check handler for subchannel report words.
657 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200658static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800660 struct subchannel_id mchk_schid;
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100661 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Cornelia Huckc1156182008-07-14 09:58:46 +0200663 if (overflow) {
664 css_schedule_eval_all();
665 return;
666 }
667 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
668 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
669 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
670 crw0->erc, crw0->rsid);
671 if (crw1)
672 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
673 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
674 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
675 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800676 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200677 mchk_schid.sch_no = crw0->rsid;
678 if (crw1)
Sebastian Ott8d7bfb42010-12-01 10:08:02 +0100679 mchk_schid.ssid = (crw1->rsid >> 4) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800680
Sebastian Ott4bc4e962011-01-05 12:47:58 +0100681 if (crw0->erc == CRW_ERC_PMOD) {
682 sch = get_subchannel_by_schid(mchk_schid);
683 if (sch) {
684 css_update_ssd_info(sch);
685 put_device(&sch->dev);
686 }
687 }
Cornelia Huckc1156182008-07-14 09:58:46 +0200688 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 * Since we are always presented with IPI in the CRW, we have to
690 * use stsch() to find out if the subchannel in question has come
691 * or gone.
692 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200693 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694}
695
696static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800697css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698{
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200699 struct cpuid cpu_id;
700
Cornelia Huck75784c02008-07-14 09:58:57 +0200701 if (css_general_characteristics.mcss) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800702 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
703 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
704 } else {
Martin Schwidefsky7b468482009-03-26 15:24:42 +0100705 css->global_pgid.pgid_high.cpu_addr = stap();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 }
Martin Schwidefsky94038a92010-05-17 10:00:00 +0200707 get_cpu_id(&cpu_id);
708 css->global_pgid.cpu_id = cpu_id.ident;
709 css->global_pgid.cpu_model = cpu_id.machine;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800710 css->global_pgid.tod_high = tod_high;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800711}
712
Cornelia Huck3b793062006-01-06 00:19:26 -0800713static void
714channel_subsystem_release(struct device *dev)
715{
716 struct channel_subsystem *css;
717
718 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800719 mutex_destroy(&css->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200720 if (css->pseudo_subchannel) {
721 /* Implies that it has been generated but never registered. */
722 css_subchannel_release(&css->pseudo_subchannel->dev);
723 css->pseudo_subchannel = NULL;
724 }
Cornelia Huck3b793062006-01-06 00:19:26 -0800725 kfree(css);
726}
727
Cornelia Huck495a5b42006-03-24 03:15:14 -0800728static ssize_t
729css_cm_enable_show(struct device *dev, struct device_attribute *attr,
730 char *buf)
731{
732 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200733 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800734
735 if (!css)
736 return 0;
Michael Ernst8284fb192008-04-17 07:46:01 +0200737 mutex_lock(&css->mutex);
738 ret = sprintf(buf, "%x\n", css->cm_enabled);
739 mutex_unlock(&css->mutex);
740 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800741}
742
743static ssize_t
744css_cm_enable_store(struct device *dev, struct device_attribute *attr,
745 const char *buf, size_t count)
746{
747 struct channel_subsystem *css = to_css(dev);
748 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +0200749 unsigned long val;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800750
Jingoo Han01787222013-07-22 10:18:15 +0900751 ret = kstrtoul(buf, 16, &val);
Cornelia Huck2f972202008-04-30 13:38:33 +0200752 if (ret)
753 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200754 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200755 switch (val) {
756 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800757 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
758 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200759 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800760 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
761 break;
762 default:
763 ret = -EINVAL;
764 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200765 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800766 return ret < 0 ? ret : count;
767}
768
769static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
770
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100771static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800772{
773 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100774 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200775 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800776
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200777 css = channel_subsystems[nr];
778 memset(css, 0, sizeof(struct channel_subsystem));
779 css->pseudo_subchannel =
780 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
781 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100782 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200783 css->pseudo_subchannel->dev.parent = &css->device;
784 css->pseudo_subchannel->dev.release = css_subchannel_release;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200785 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
Peter Oberparleiter5d6e6b62009-12-07 12:51:17 +0100786 mutex_init(&css->pseudo_subchannel->reg_mutex);
Sebastian Otte5dcf002013-04-13 13:08:01 +0200787 ret = css_sch_create_locks(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100788 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200789 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100790 return ret;
791 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200792 mutex_init(&css->mutex);
793 css->valid = 1;
794 css->cssid = nr;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200795 dev_set_name(&css->device, "css%x", nr);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200796 css->device.release = channel_subsystem_release;
Heiko Carstens1aae0562013-01-30 09:49:40 +0100797 tod_high = (u32) (get_tod_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200798 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100799 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Cornelia Hucka55360d2007-10-12 16:11:20 +0200802static int css_reboot_event(struct notifier_block *this,
803 unsigned long event,
804 void *ptr)
805{
806 int ret, i;
807
808 ret = NOTIFY_DONE;
809 for (i = 0; i <= __MAX_CSSID; i++) {
810 struct channel_subsystem *css;
811
812 css = channel_subsystems[i];
Michael Ernst8284fb192008-04-17 07:46:01 +0200813 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200814 if (css->cm_enabled)
815 if (chsc_secm(css, 0))
816 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200817 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200818 }
819
820 return ret;
821}
822
823static struct notifier_block css_reboot_notifier = {
824 .notifier_call = css_reboot_event,
825};
826
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827/*
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200828 * Since the css devices are neither on a bus nor have a class
829 * nor have a special device type, we cannot stop/restart channel
830 * path measurements via the normal suspend/resume callbacks, but have
831 * to use notifiers.
832 */
833static int css_power_event(struct notifier_block *this, unsigned long event,
834 void *ptr)
835{
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200836 int ret, i;
837
838 switch (event) {
839 case PM_HIBERNATION_PREPARE:
840 case PM_SUSPEND_PREPARE:
841 ret = NOTIFY_DONE;
842 for (i = 0; i <= __MAX_CSSID; i++) {
843 struct channel_subsystem *css;
844
845 css = channel_subsystems[i];
846 mutex_lock(&css->mutex);
847 if (!css->cm_enabled) {
848 mutex_unlock(&css->mutex);
849 continue;
850 }
Akinobu Mitaf0c077a2011-07-08 20:53:36 +0200851 ret = __chsc_do_secm(css, 0);
852 ret = notifier_from_errno(ret);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200853 mutex_unlock(&css->mutex);
854 }
855 break;
856 case PM_POST_HIBERNATION:
857 case PM_POST_SUSPEND:
858 ret = NOTIFY_DONE;
859 for (i = 0; i <= __MAX_CSSID; i++) {
860 struct channel_subsystem *css;
861
862 css = channel_subsystems[i];
863 mutex_lock(&css->mutex);
864 if (!css->cm_enabled) {
865 mutex_unlock(&css->mutex);
866 continue;
867 }
Akinobu Mitaf0c077a2011-07-08 20:53:36 +0200868 ret = __chsc_do_secm(css, 1);
869 ret = notifier_from_errno(ret);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200870 mutex_unlock(&css->mutex);
871 }
872 /* search for subchannels, which appeared during hibernation */
873 css_schedule_reprobe();
874 break;
875 default:
876 ret = NOTIFY_DONE;
877 }
878 return ret;
879
880}
881static struct notifier_block css_power_notifier = {
882 .notifier_call = css_power_event,
883};
884
885/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886 * Now that the driver core is running, we can setup our channel subsystem.
Sebastian Ott14556b32013-04-13 13:03:54 +0200887 * The struct subchannel's are created during probing.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 */
Sebastian Ott2f176442009-09-22 22:58:33 +0200889static int __init css_bus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800891 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892
Sebastian Ott34aec072010-10-25 16:10:28 +0200893 ret = chsc_init();
894 if (ret)
895 return ret;
896
Sebastian Ott34196f82010-10-25 16:10:29 +0200897 chsc_determine_css_characteristics();
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200898 /* Try to enable MSS. */
899 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott818c2722010-04-22 17:17:03 +0200900 if (ret)
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200901 max_ssid = 0;
Sebastian Ott818c2722010-04-22 17:17:03 +0200902 else /* Success. */
903 max_ssid = __MAX_SSID;
Sebastian Ottb0a285d2009-09-22 22:58:37 +0200904
Cornelia Huck4434a382007-07-27 12:29:21 +0200905 ret = slow_subchannel_init();
906 if (ret)
907 goto out;
908
Heiko Carstensf5daba12009-03-26 15:24:01 +0100909 ret = crw_register_handler(CRW_RSC_SCH, css_process_crw);
Cornelia Huckc1156182008-07-14 09:58:46 +0200910 if (ret)
911 goto out;
912
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if ((ret = bus_register(&css_bus_type)))
914 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Cornelia Hucka28c6942006-01-06 00:19:23 -0800916 /* Setup css structure. */
917 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200918 struct channel_subsystem *css;
919
920 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
921 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800922 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800923 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800924 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200925 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100926 ret = setup_css(i);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200927 if (ret) {
928 kfree(channel_subsystems[i]);
929 goto out_unregister;
930 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200931 ret = device_register(&css->device);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200932 if (ret) {
933 put_device(&css->device);
934 goto out_unregister;
935 }
Cornelia Huck75784c02008-07-14 09:58:57 +0200936 if (css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200937 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200938 &dev_attr_cm_enable);
939 if (ret)
940 goto out_device;
941 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200942 ret = device_register(&css->pseudo_subchannel->dev);
Sebastian Ottc6304932009-09-11 10:28:38 +0200943 if (ret) {
944 put_device(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100945 goto out_file;
Sebastian Ottc6304932009-09-11 10:28:38 +0200946 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800947 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200948 ret = register_reboot_notifier(&css_reboot_notifier);
949 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +0200950 goto out_unregister;
Sebastian Ottdcbd16d2009-06-16 10:30:22 +0200951 ret = register_pm_notifier(&css_power_notifier);
952 if (ret) {
953 unregister_reboot_notifier(&css_reboot_notifier);
954 goto out_unregister;
955 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 css_init_done = 1;
957
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200958 /* Enable default isc for I/O subchannels. */
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200959 isc_register(IO_SCH_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 return 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100962out_file:
Cornelia Hucka2164b82008-09-09 12:38:57 +0200963 if (css_chsc_characteristics.secm)
964 device_remove_file(&channel_subsystems[i]->device,
965 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200966out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200967 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800968out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800969 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200970 struct channel_subsystem *css;
971
Cornelia Hucka28c6942006-01-06 00:19:23 -0800972 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200973 css = channel_subsystems[i];
974 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200975 css->pseudo_subchannel = NULL;
Cornelia Huck75784c02008-07-14 09:58:57 +0200976 if (css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200977 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800978 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200979 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 bus_unregister(&css_bus_type);
982out:
Sebastian Ott34aec072010-10-25 16:10:28 +0200983 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c82009-09-22 22:58:36 +0200984 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +0200985 chsc_init_cleanup();
Michael Ernste6d5a422008-12-25 13:39:36 +0100986 pr_alert("The CSS device driver initialization failed with "
987 "errno=%d\n", ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return ret;
989}
990
Sebastian Ott2f176442009-09-22 22:58:33 +0200991static void __init css_bus_cleanup(void)
992{
993 struct channel_subsystem *css;
994 int i;
995
996 for (i = 0; i <= __MAX_CSSID; i++) {
997 css = channel_subsystems[i];
998 device_unregister(&css->pseudo_subchannel->dev);
999 css->pseudo_subchannel = NULL;
1000 if (css_chsc_characteristics.secm)
1001 device_remove_file(&css->device, &dev_attr_cm_enable);
1002 device_unregister(&css->device);
1003 }
1004 bus_unregister(&css_bus_type);
Sebastian Ott34aec072010-10-25 16:10:28 +02001005 crw_unregister_handler(CRW_RSC_SCH);
Sebastian Ottb827d1c82009-09-22 22:58:36 +02001006 idset_free(slow_subchannel_set);
Sebastian Ott34aec072010-10-25 16:10:28 +02001007 chsc_init_cleanup();
Sebastian Ott2f176442009-09-22 22:58:33 +02001008 isc_unregister(IO_SCH_ISC);
1009}
1010
1011static int __init channel_subsystem_init(void)
1012{
1013 int ret;
1014
1015 ret = css_bus_init();
1016 if (ret)
1017 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001018 cio_work_q = create_singlethread_workqueue("cio");
1019 if (!cio_work_q) {
1020 ret = -ENOMEM;
1021 goto out_bus;
1022 }
Sebastian Ott2f176442009-09-22 22:58:33 +02001023 ret = io_subchannel_init();
1024 if (ret)
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001025 goto out_wq;
Sebastian Ott2f176442009-09-22 22:58:33 +02001026
1027 return ret;
Sebastian Ottbe5d3822010-02-26 22:37:24 +01001028out_wq:
1029 destroy_workqueue(cio_work_q);
1030out_bus:
1031 css_bus_cleanup();
1032 return ret;
Sebastian Ott2f176442009-09-22 22:58:33 +02001033}
1034subsys_initcall(channel_subsystem_init);
1035
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001036static int css_settle(struct device_driver *drv, void *unused)
1037{
1038 struct css_driver *cssdrv = to_cssdriver(drv);
1039
1040 if (cssdrv->settle)
Sebastian Ottb4c70722010-02-26 22:37:27 +01001041 return cssdrv->settle();
Sebastian Ott8ea7f552009-09-22 22:58:35 +02001042 return 0;
1043}
1044
Sebastian Ott0d01bb82010-02-26 22:37:29 +01001045int css_complete_work(void)
Sebastian Ott879acca52010-02-26 22:37:25 +01001046{
1047 int ret;
1048
1049 /* Wait for the evaluation of subchannels to finish. */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001050 ret = wait_event_interruptible(css_eval_wq,
1051 atomic_read(&css_eval_scheduled) == 0);
1052 if (ret)
1053 return -EINTR;
Sebastian Ott879acca52010-02-26 22:37:25 +01001054 flush_workqueue(cio_work_q);
1055 /* Wait for the subchannel type specific initialization to finish */
Sebastian Ottb4c70722010-02-26 22:37:27 +01001056 return bus_for_each_drv(&css_bus_type, NULL, NULL, css_settle);
Sebastian Ott879acca52010-02-26 22:37:25 +01001057}
1058
1059
Sebastian Ott2f176442009-09-22 22:58:33 +02001060/*
1061 * Wait for the initialization of devices to finish, to make sure we are
1062 * done with our setup if the search for the root device starts.
1063 */
1064static int __init channel_subsystem_init_sync(void)
1065{
Sebastian Ott14556b32013-04-13 13:03:54 +02001066 /* Register subchannels which are already in use. */
1067 cio_register_early_subchannels();
Sebastian Ott703e5c92009-09-22 22:58:38 +02001068 /* Start initial subchannel evaluation. */
1069 css_schedule_eval_all();
Sebastian Ott879acca52010-02-26 22:37:25 +01001070 css_complete_work();
1071 return 0;
Sebastian Ott2f176442009-09-22 22:58:33 +02001072}
1073subsys_initcall_sync(channel_subsystem_init_sync);
1074
Sebastian Ott889ee952010-04-22 17:17:04 +02001075void channel_subsystem_reinit(void)
1076{
Sebastian Ott62da1772010-10-25 16:10:32 +02001077 struct channel_path *chp;
1078 struct chp_id chpid;
1079
Sebastian Ott889ee952010-04-22 17:17:04 +02001080 chsc_enable_facility(CHSC_SDA_OC_MSS);
Sebastian Ott62da1772010-10-25 16:10:32 +02001081 chp_id_for_each(&chpid) {
1082 chp = chpid_to_chp(chpid);
Peter Oberparleitercce0eac2013-03-11 12:58:18 +01001083 if (chp)
1084 chp_update_desc(chp);
Sebastian Ott62da1772010-10-25 16:10:32 +02001085 }
Sebastian Ottab97d212015-09-09 10:29:59 +02001086 cmf_reactivate();
Sebastian Ott889ee952010-04-22 17:17:04 +02001087}
1088
Sebastian Ott879acca52010-02-26 22:37:25 +01001089#ifdef CONFIG_PROC_FS
1090static ssize_t cio_settle_write(struct file *file, const char __user *buf,
1091 size_t count, loff_t *ppos)
1092{
Sebastian Ottb4c70722010-02-26 22:37:27 +01001093 int ret;
1094
Sebastian Ott879acca52010-02-26 22:37:25 +01001095 /* Handle pending CRW's. */
1096 crw_wait_for_channel_report();
Sebastian Ottb4c70722010-02-26 22:37:27 +01001097 ret = css_complete_work();
1098
1099 return ret ? ret : count;
Sebastian Ott879acca52010-02-26 22:37:25 +01001100}
1101
1102static const struct file_operations cio_settle_proc_fops = {
Martin Schwidefsky58ea91c2010-05-17 10:00:07 +02001103 .open = nonseekable_open,
Sebastian Ott879acca52010-02-26 22:37:25 +01001104 .write = cio_settle_write,
Arnd Bergmann6038f372010-08-15 18:52:59 +02001105 .llseek = no_llseek,
Sebastian Ott879acca52010-02-26 22:37:25 +01001106};
1107
1108static int __init cio_settle_init(void)
1109{
1110 struct proc_dir_entry *entry;
1111
1112 entry = proc_create("cio_settle", S_IWUSR, NULL,
1113 &cio_settle_proc_fops);
1114 if (!entry)
1115 return -ENOMEM;
1116 return 0;
1117}
1118device_initcall(cio_settle_init);
1119#endif /*CONFIG_PROC_FS*/
1120
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001121int sch_is_pseudo_sch(struct subchannel *sch)
1122{
1123 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
1124}
1125
Cornelia Huckf08adc02008-07-14 09:59:03 +02001126static int css_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127{
Cornelia Huck084325d2008-01-26 14:10:38 +01001128 struct subchannel *sch = to_subchannel(dev);
1129 struct css_driver *driver = to_cssdriver(drv);
Cornelia Huckf08adc02008-07-14 09:59:03 +02001130 struct css_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131
Cornelia Huckf08adc02008-07-14 09:59:03 +02001132 for (id = driver->subchannel_type; id->match_flags; id++) {
1133 if (sch->st == id->type)
1134 return 1;
1135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136
1137 return 0;
1138}
1139
Cornelia Huck98c13c22008-01-26 14:10:40 +01001140static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001141{
1142 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +01001143 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001144
1145 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +01001146 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001147 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
1148 if (ret)
1149 sch->driver = NULL;
1150 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +01001151}
1152
Cornelia Huck98c13c22008-01-26 14:10:40 +01001153static int css_remove(struct device *dev)
1154{
1155 struct subchannel *sch;
1156 int ret;
1157
1158 sch = to_subchannel(dev);
1159 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
1160 sch->driver = NULL;
1161 return ret;
1162}
1163
1164static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001165{
1166 struct subchannel *sch;
1167
1168 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +01001169 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +01001170 sch->driver->shutdown(sch);
1171}
1172
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001173static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
1174{
1175 struct subchannel *sch = to_subchannel(dev);
1176 int ret;
1177
1178 ret = add_uevent_var(env, "ST=%01X", sch->st);
1179 if (ret)
1180 return ret;
1181 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
1182 return ret;
1183}
1184
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001185static int css_pm_prepare(struct device *dev)
1186{
1187 struct subchannel *sch = to_subchannel(dev);
1188 struct css_driver *drv;
1189
1190 if (mutex_is_locked(&sch->reg_mutex))
1191 return -EAGAIN;
1192 if (!sch->dev.driver)
1193 return 0;
1194 drv = to_cssdriver(sch->dev.driver);
1195 /* Notify drivers that they may not register children. */
1196 return drv->prepare ? drv->prepare(sch) : 0;
1197}
1198
1199static void css_pm_complete(struct device *dev)
1200{
1201 struct subchannel *sch = to_subchannel(dev);
1202 struct css_driver *drv;
1203
1204 if (!sch->dev.driver)
1205 return;
1206 drv = to_cssdriver(sch->dev.driver);
1207 if (drv->complete)
1208 drv->complete(sch);
1209}
1210
1211static int css_pm_freeze(struct device *dev)
1212{
1213 struct subchannel *sch = to_subchannel(dev);
1214 struct css_driver *drv;
1215
1216 if (!sch->dev.driver)
1217 return 0;
1218 drv = to_cssdriver(sch->dev.driver);
1219 return drv->freeze ? drv->freeze(sch) : 0;
1220}
1221
1222static int css_pm_thaw(struct device *dev)
1223{
1224 struct subchannel *sch = to_subchannel(dev);
1225 struct css_driver *drv;
1226
1227 if (!sch->dev.driver)
1228 return 0;
1229 drv = to_cssdriver(sch->dev.driver);
1230 return drv->thaw ? drv->thaw(sch) : 0;
1231}
1232
1233static int css_pm_restore(struct device *dev)
1234{
1235 struct subchannel *sch = to_subchannel(dev);
1236 struct css_driver *drv;
1237
Sebastian Otteb4f5d92010-10-25 16:10:33 +02001238 css_update_ssd_info(sch);
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001239 if (!sch->dev.driver)
1240 return 0;
1241 drv = to_cssdriver(sch->dev.driver);
1242 return drv->restore ? drv->restore(sch) : 0;
1243}
1244
Alexey Dobriyan47145212009-12-14 18:00:08 -08001245static const struct dev_pm_ops css_pm_ops = {
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001246 .prepare = css_pm_prepare,
1247 .complete = css_pm_complete,
1248 .freeze = css_pm_freeze,
1249 .thaw = css_pm_thaw,
1250 .restore = css_pm_restore,
1251};
1252
Sebastian Ott3041b6a2011-03-15 17:08:31 +01001253static struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +01001254 .name = "css",
1255 .match = css_bus_match,
1256 .probe = css_probe,
1257 .remove = css_remove,
1258 .shutdown = css_shutdown,
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001259 .uevent = css_uevent,
Sebastian Ottdcbd16d2009-06-16 10:30:22 +02001260 .pm = &css_pm_ops,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261};
1262
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001263/**
1264 * css_driver_register - register a css driver
1265 * @cdrv: css driver to register
1266 *
1267 * This is mainly a wrapper around driver_register that sets name
1268 * and bus_type in the embedded struct device_driver correctly.
1269 */
1270int css_driver_register(struct css_driver *cdrv)
1271{
Cornelia Huck25b7bb52008-01-26 14:10:41 +01001272 cdrv->drv.bus = &css_bus_type;
1273 return driver_register(&cdrv->drv);
1274}
1275EXPORT_SYMBOL_GPL(css_driver_register);
1276
1277/**
1278 * css_driver_unregister - unregister a css driver
1279 * @cdrv: css driver to unregister
1280 *
1281 * This is a wrapper around driver_unregister.
1282 */
1283void css_driver_unregister(struct css_driver *cdrv)
1284{
1285 driver_unregister(&cdrv->drv);
1286}
1287EXPORT_SYMBOL_GPL(css_driver_unregister);
1288
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289MODULE_LICENSE("GPL");