blob: bc3a8e4a49f78d7884c77b087e12aaf9845d845d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/css.c
3 * driver for channel subsystem
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02005 * Copyright IBM Corp. 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08007 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 */
9#include <linux/module.h>
10#include <linux/init.h>
11#include <linux/device.h>
12#include <linux/slab.h>
13#include <linux/errno.h>
14#include <linux/list.h>
Cornelia Hucka55360d2007-10-12 16:11:20 +020015#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016
Cornelia Huckc1156182008-07-14 09:58:46 +020017#include "../s390mach.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "css.h"
19#include "cio.h"
20#include "cio_debug.h"
21#include "ioasm.h"
22#include "chsc.h"
Peter Oberparleiter40154b82006-06-29 14:57:03 +020023#include "device.h"
Peter Oberparleiter83b33702007-04-27 16:01:34 +020024#include "idset.h"
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +020025#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027int css_init_done = 0;
Peter Oberparleiter40154b82006-06-29 14:57:03 +020028static int need_reprobe = 0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -080029static int max_ssid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Cornelia Huck7c9f4e32007-10-12 16:11:13 +020031struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Cornelia Hucka28c6942006-01-06 00:19:23 -080033int css_characteristics_avail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Heiko Carstens4d284ca2007-02-05 21:18:53 +010035int
Cornelia Huckf97a56f2006-01-06 00:19:22 -080036for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
37{
38 struct subchannel_id schid;
39 int ret;
40
41 init_subchannel_id(&schid);
42 ret = -ENODEV;
43 do {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080044 do {
45 ret = fn(schid, data);
46 if (ret)
47 break;
48 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
49 schid.sch_no = 0;
50 } while (schid.ssid++ < max_ssid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080051 return ret;
52}
53
Peter Oberparleitere82a1562008-01-26 14:10:48 +010054struct cb_data {
55 void *data;
56 struct idset *set;
57 int (*fn_known_sch)(struct subchannel *, void *);
58 int (*fn_unknown_sch)(struct subchannel_id, void *);
59};
60
61static int call_fn_known_sch(struct device *dev, void *data)
62{
63 struct subchannel *sch = to_subchannel(dev);
64 struct cb_data *cb = data;
65 int rc = 0;
66
67 idset_sch_del(cb->set, sch->schid);
68 if (cb->fn_known_sch)
69 rc = cb->fn_known_sch(sch, cb->data);
70 return rc;
71}
72
73static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
74{
75 struct cb_data *cb = data;
76 int rc = 0;
77
78 if (idset_sch_contains(cb->set, schid))
79 rc = cb->fn_unknown_sch(schid, cb->data);
80 return rc;
81}
82
83int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
84 int (*fn_unknown)(struct subchannel_id,
85 void *), void *data)
86{
87 struct cb_data cb;
88 int rc;
89
90 cb.set = idset_sch_new();
91 if (!cb.set)
92 return -ENOMEM;
93 idset_fill(cb.set);
94 cb.data = data;
95 cb.fn_known_sch = fn_known;
96 cb.fn_unknown_sch = fn_unknown;
97 /* Process registered subchannels. */
98 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
99 if (rc)
100 goto out;
101 /* Process unregistered subchannels. */
102 if (fn_unknown)
103 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
104out:
105 idset_free(cb.set);
106
107 return rc;
108}
109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800111css_alloc_subchannel(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
113 struct subchannel *sch;
114 int ret;
115
116 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
117 if (sch == NULL)
118 return ERR_PTR(-ENOMEM);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800119 ret = cio_validate_subchannel (sch, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 if (ret < 0) {
121 kfree(sch);
122 return ERR_PTR(ret);
123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124 return sch;
125}
126
127static void
128css_free_subchannel(struct subchannel *sch)
129{
130 if (sch) {
131 /* Reset intparm to zeroes. */
132 sch->schib.pmcw.intparm = 0;
133 cio_modify(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100134 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 kfree(sch);
136 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
139static void
140css_subchannel_release(struct device *dev)
141{
142 struct subchannel *sch;
143
144 sch = to_subchannel(dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100145 if (!cio_is_console(sch->schid)) {
146 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 kfree(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100148 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Cornelia Huck07c6a332007-07-27 12:29:10 +0200151static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200152{
153 int ret;
154
155 mutex_lock(&sch->reg_mutex);
156 ret = device_register(&sch->dev);
157 mutex_unlock(&sch->reg_mutex);
158 return ret;
159}
160
Cornelia Huck44a1c192008-07-14 09:58:47 +0200161/**
162 * css_sch_device_unregister - unregister a subchannel
163 * @sch: subchannel to be unregistered
164 */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200165void css_sch_device_unregister(struct subchannel *sch)
166{
167 mutex_lock(&sch->reg_mutex);
168 device_unregister(&sch->dev);
169 mutex_unlock(&sch->reg_mutex);
170}
Cornelia Huck44a1c192008-07-14 09:58:47 +0200171EXPORT_SYMBOL_GPL(css_sch_device_unregister);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200172
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200173static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
174{
175 int i;
176 int mask;
177
178 memset(ssd, 0, sizeof(struct chsc_ssd_info));
179 ssd->path_mask = pmcw->pim;
180 for (i = 0; i < 8; i++) {
181 mask = 0x80 >> i;
182 if (pmcw->pim & mask) {
183 chp_id_init(&ssd->chpid[i]);
184 ssd->chpid[i].id = pmcw->chpid[i];
185 }
186 }
187}
188
189static void ssd_register_chpids(struct chsc_ssd_info *ssd)
190{
191 int i;
192 int mask;
193
194 for (i = 0; i < 8; i++) {
195 mask = 0x80 >> i;
196 if (ssd->path_mask & mask)
197 if (!chp_is_registered(ssd->chpid[i]))
198 chp_new(ssd->chpid[i]);
199 }
200}
201
202void css_update_ssd_info(struct subchannel *sch)
203{
204 int ret;
205
206 if (cio_is_console(sch->schid)) {
207 /* Console is initialized too early for functions requiring
208 * memory allocation. */
209 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
210 } else {
211 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
212 if (ret)
213 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
214 ssd_register_chpids(&sch->ssd_info);
215 }
216}
217
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200218static ssize_t type_show(struct device *dev, struct device_attribute *attr,
219 char *buf)
220{
221 struct subchannel *sch = to_subchannel(dev);
222
223 return sprintf(buf, "%01x\n", sch->st);
224}
225
226static DEVICE_ATTR(type, 0444, type_show, NULL);
227
228static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
229 char *buf)
230{
231 struct subchannel *sch = to_subchannel(dev);
232
233 return sprintf(buf, "css:t%01X\n", sch->st);
234}
235
236static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
237
238static struct attribute *subch_attrs[] = {
239 &dev_attr_type.attr,
240 &dev_attr_modalias.attr,
241 NULL,
242};
243
244static struct attribute_group subch_attr_group = {
245 .attrs = subch_attrs,
246};
247
248static struct attribute_group *default_subch_attr_groups[] = {
249 &subch_attr_group,
250 NULL,
251};
252
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200253static int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 int ret;
256
257 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200258 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 sch->dev.bus = &css_bus_type;
260 sch->dev.release = &css_subchannel_release;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200261 sch->dev.groups = default_subch_attr_groups;
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200262 /*
263 * We don't want to generate uevents for I/O subchannels that don't
264 * have a working ccw device behind them since they will be
265 * unregistered before they can be used anyway, so we delay the add
266 * uevent until after device recognition was successful.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200267 * Note that we suppress the uevent for all subchannel types;
268 * the subchannel driver can decide itself when it wants to inform
269 * userspace of its existence.
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200270 */
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200271 sch->dev.uevent_suppress = 1;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200272 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200274 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100275 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200276 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
277 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100278 return ret;
279 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200280 if (!sch->driver) {
281 /*
282 * No driver matched. Generate the uevent now so that
283 * a fitting driver module may be loaded based on the
284 * modalias.
285 */
286 sch->dev.uevent_suppress = 0;
287 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
288 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 return ret;
290}
291
Cornelia Huckc820de32008-07-14 09:58:45 +0200292int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293{
294 int ret;
295 struct subchannel *sch;
296
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800297 sch = css_alloc_subchannel(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (IS_ERR(sch))
299 return PTR_ERR(sch);
300 ret = css_register_subchannel(sch);
301 if (ret)
302 css_free_subchannel(sch);
303 return ret;
304}
305
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700306static int
307check_subchannel(struct device * dev, void * data)
308{
309 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800310 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700311
312 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800313 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700314}
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800317get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 struct device *dev;
320
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700321 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200322 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700324 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325}
326
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100327/**
328 * css_sch_is_valid() - check if a subchannel is valid
329 * @schib: subchannel information block for the subchannel
330 */
331int css_sch_is_valid(struct schib *schib)
332{
333 if ((schib->pmcw.st == SUBCHANNEL_TYPE_IO) && !schib->pmcw.dnv)
334 return 0;
335 return 1;
336}
337EXPORT_SYMBOL_GPL(css_sch_is_valid);
338
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200339static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
340{
341 struct schib schib;
342
343 if (!slow) {
344 /* Will be done on the slow path. */
345 return -EAGAIN;
346 }
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100347 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200348 /* Unusable - ignore. */
349 return 0;
350 }
351 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
352 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
353
354 return css_probe_device(schid);
355}
356
Cornelia Huckc820de32008-07-14 09:58:45 +0200357static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
358{
359 int ret = 0;
360
361 if (sch->driver) {
362 if (sch->driver->sch_event)
363 ret = sch->driver->sch_event(sch, slow);
364 else
365 dev_dbg(&sch->dev,
366 "Got subchannel machine check but "
367 "no sch_event handler provided.\n");
368 }
369 return ret;
370}
371
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200372static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200373{
374 struct subchannel *sch;
375 int ret;
376
377 sch = get_subchannel_by_schid(schid);
378 if (sch) {
379 ret = css_evaluate_known_subchannel(sch, slow);
380 put_device(&sch->dev);
381 } else
382 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200383 if (ret == -EAGAIN)
384 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385}
386
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200387static struct idset *slow_subchannel_set;
388static spinlock_t slow_subchannel_lock;
389
390static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200392 spin_lock_init(&slow_subchannel_lock);
393 slow_subchannel_set = idset_sch_new();
394 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200395 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200396 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200398 return 0;
399}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100401static int slow_eval_known_fn(struct subchannel *sch, void *data)
402{
403 int eval;
404 int rc;
405
406 spin_lock_irq(&slow_subchannel_lock);
407 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
408 idset_sch_del(slow_subchannel_set, sch->schid);
409 spin_unlock_irq(&slow_subchannel_lock);
410 if (eval) {
411 rc = css_evaluate_known_subchannel(sch, 1);
412 if (rc == -EAGAIN)
413 css_schedule_eval(sch->schid);
414 }
415 return 0;
416}
417
418static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
419{
420 int eval;
421 int rc = 0;
422
423 spin_lock_irq(&slow_subchannel_lock);
424 eval = idset_sch_contains(slow_subchannel_set, schid);
425 idset_sch_del(slow_subchannel_set, schid);
426 spin_unlock_irq(&slow_subchannel_lock);
427 if (eval) {
428 rc = css_evaluate_new_subchannel(schid, 1);
429 switch (rc) {
430 case -EAGAIN:
431 css_schedule_eval(schid);
432 rc = 0;
433 break;
434 case -ENXIO:
435 case -ENOMEM:
436 case -EIO:
437 /* These should abort looping */
438 break;
439 default:
440 rc = 0;
441 }
442 }
443 return rc;
444}
445
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200446static void css_slow_path_func(struct work_struct *unused)
447{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200448 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100449 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
450 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451}
452
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200453static DECLARE_WORK(slow_path_work, css_slow_path_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454struct workqueue_struct *slow_path_wq;
455
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200456void css_schedule_eval(struct subchannel_id schid)
457{
458 unsigned long flags;
459
460 spin_lock_irqsave(&slow_subchannel_lock, flags);
461 idset_sch_add(slow_subchannel_set, schid);
462 queue_work(slow_path_wq, &slow_path_work);
463 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
464}
465
466void css_schedule_eval_all(void)
467{
468 unsigned long flags;
469
470 spin_lock_irqsave(&slow_subchannel_lock, flags);
471 idset_fill(slow_subchannel_set);
472 queue_work(slow_path_wq, &slow_path_work);
473 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
474}
475
Cornelia Huck22806dc2008-04-17 07:45:59 +0200476void css_wait_for_slow_path(void)
477{
478 flush_workqueue(ccw_device_notify_work);
479 flush_workqueue(slow_path_wq);
480}
481
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200482/* Reprobe subchannel if unregistered. */
483static int reprobe_subchannel(struct subchannel_id schid, void *data)
484{
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200485 int ret;
486
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200487 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
488 schid.ssid, schid.sch_no);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200489 if (need_reprobe)
490 return -EAGAIN;
491
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200492 ret = css_probe_device(schid);
493 switch (ret) {
494 case 0:
495 break;
496 case -ENXIO:
497 case -ENOMEM:
Peter Oberparleiter67175612007-12-04 16:09:02 +0100498 case -EIO:
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200499 /* These should abort looping */
500 break;
501 default:
502 ret = 0;
503 }
504
505 return ret;
506}
507
508/* Work function used to reprobe all unregistered subchannels. */
Al Viro4927b3f2006-12-06 19:18:20 +0000509static void reprobe_all(struct work_struct *unused)
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200510{
511 int ret;
512
Michael Ernst139b83d2008-05-07 09:22:54 +0200513 CIO_MSG_EVENT(4, "reprobe start\n");
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200514
515 need_reprobe = 0;
516 /* Make sure initial subchannel scan is done. */
517 wait_event(ccw_device_init_wq,
518 atomic_read(&ccw_device_init_count) == 0);
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100519 ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200520
Michael Ernst139b83d2008-05-07 09:22:54 +0200521 CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200522 need_reprobe);
523}
524
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100525static DECLARE_WORK(css_reprobe_work, reprobe_all);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200526
527/* Schedule reprobing of all unregistered subchannels. */
528void css_schedule_reprobe(void)
529{
530 need_reprobe = 1;
Cornelia Huckc5d4a992007-11-20 11:13:41 +0100531 queue_work(slow_path_wq, &css_reprobe_work);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200532}
533
534EXPORT_SYMBOL_GPL(css_schedule_reprobe);
535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 * Called from the machine check handler for subchannel report words.
538 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200539static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800541 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
Cornelia Huckc1156182008-07-14 09:58:46 +0200543 if (overflow) {
544 css_schedule_eval_all();
545 return;
546 }
547 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
548 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
549 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
550 crw0->erc, crw0->rsid);
551 if (crw1)
552 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
553 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
554 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
555 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800556 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200557 mchk_schid.sch_no = crw0->rsid;
558 if (crw1)
559 mchk_schid.ssid = (crw1->rsid >> 8) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800560
Cornelia Huckc1156182008-07-14 09:58:46 +0200561 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 * Since we are always presented with IPI in the CRW, we have to
563 * use stsch() to find out if the subchannel in question has come
564 * or gone.
565 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200566 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567}
568
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800569static int __init
570__init_channel_subsystem(struct subchannel_id schid, void *data)
571{
572 struct subchannel *sch;
573 int ret;
574
575 if (cio_is_console(schid))
576 sch = cio_get_console_subchannel();
577 else {
578 sch = css_alloc_subchannel(schid);
579 if (IS_ERR(sch))
580 ret = PTR_ERR(sch);
581 else
582 ret = 0;
583 switch (ret) {
584 case 0:
585 break;
586 case -ENOMEM:
587 panic("Out of memory in init_channel_subsystem\n");
588 /* -ENXIO: no more subchannels. */
589 case -ENXIO:
590 return ret;
Greg Smithe843e282006-03-14 19:50:17 -0800591 /* -EIO: this subchannel set not supported. */
592 case -EIO:
593 return ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800594 default:
595 return 0;
596 }
597 }
598 /*
599 * We register ALL valid subchannels in ioinfo, even those
600 * that have been present before init_channel_subsystem.
601 * These subchannels can't have been registered yet (kmalloc
602 * not working) so we do it now. This is true e.g. for the
603 * console subchannel.
604 */
605 css_register_subchannel(sch);
606 return 0;
607}
608
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800610css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800612 if (css_characteristics_avail && css_general_characteristics.mcss) {
613 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
614 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
615 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616#ifdef CONFIG_SMP
Cornelia Hucka28c6942006-01-06 00:19:23 -0800617 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800619 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620#endif
621 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800622 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
623 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
624 css->global_pgid.tod_high = tod_high;
625
626}
627
Cornelia Huck3b793062006-01-06 00:19:26 -0800628static void
629channel_subsystem_release(struct device *dev)
630{
631 struct channel_subsystem *css;
632
633 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800634 mutex_destroy(&css->mutex);
Cornelia Huck3b793062006-01-06 00:19:26 -0800635 kfree(css);
636}
637
Cornelia Huck495a5b42006-03-24 03:15:14 -0800638static ssize_t
639css_cm_enable_show(struct device *dev, struct device_attribute *attr,
640 char *buf)
641{
642 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200643 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800644
645 if (!css)
646 return 0;
Michael Ernst8284fb192008-04-17 07:46:01 +0200647 mutex_lock(&css->mutex);
648 ret = sprintf(buf, "%x\n", css->cm_enabled);
649 mutex_unlock(&css->mutex);
650 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800651}
652
653static ssize_t
654css_cm_enable_store(struct device *dev, struct device_attribute *attr,
655 const char *buf, size_t count)
656{
657 struct channel_subsystem *css = to_css(dev);
658 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +0200659 unsigned long val;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800660
Cornelia Huck2f972202008-04-30 13:38:33 +0200661 ret = strict_strtoul(buf, 16, &val);
662 if (ret)
663 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200664 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200665 switch (val) {
666 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800667 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
668 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200669 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800670 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
671 break;
672 default:
673 ret = -EINVAL;
674 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200675 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800676 return ret < 0 ? ret : count;
677}
678
679static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
680
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100681static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800682{
683 u32 tod_high;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100684 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200685 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800686
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200687 css = channel_subsystems[nr];
688 memset(css, 0, sizeof(struct channel_subsystem));
689 css->pseudo_subchannel =
690 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
691 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100692 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200693 css->pseudo_subchannel->dev.parent = &css->device;
694 css->pseudo_subchannel->dev.release = css_subchannel_release;
695 sprintf(css->pseudo_subchannel->dev.bus_id, "defunct");
696 ret = cio_create_sch_lock(css->pseudo_subchannel);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100697 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200698 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100699 return ret;
700 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200701 mutex_init(&css->mutex);
702 css->valid = 1;
703 css->cssid = nr;
704 sprintf(css->device.bus_id, "css%x", nr);
705 css->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800706 tod_high = (u32) (get_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200707 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100708 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Cornelia Hucka55360d2007-10-12 16:11:20 +0200711static int css_reboot_event(struct notifier_block *this,
712 unsigned long event,
713 void *ptr)
714{
715 int ret, i;
716
717 ret = NOTIFY_DONE;
718 for (i = 0; i <= __MAX_CSSID; i++) {
719 struct channel_subsystem *css;
720
721 css = channel_subsystems[i];
Michael Ernst8284fb192008-04-17 07:46:01 +0200722 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200723 if (css->cm_enabled)
724 if (chsc_secm(css, 0))
725 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200726 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200727 }
728
729 return ret;
730}
731
732static struct notifier_block css_reboot_notifier = {
733 .notifier_call = css_reboot_event,
734};
735
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736/*
737 * Now that the driver core is running, we can setup our channel subsystem.
738 * The struct subchannel's are created during probing (except for the
739 * static console subchannel).
740 */
741static int __init
742init_channel_subsystem (void)
743{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800744 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Cornelia Huck4434a382007-07-27 12:29:21 +0200746 ret = chsc_determine_css_characteristics();
747 if (ret == -ENOMEM)
748 goto out; /* No need to continue. */
749 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 css_characteristics_avail = 1;
751
Cornelia Huck4434a382007-07-27 12:29:21 +0200752 ret = chsc_alloc_sei_area();
753 if (ret)
754 goto out;
755
756 ret = slow_subchannel_init();
757 if (ret)
758 goto out;
759
Cornelia Huckc1156182008-07-14 09:58:46 +0200760 ret = s390_register_crw_handler(CRW_RSC_SCH, css_process_crw);
761 if (ret)
762 goto out;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 if ((ret = bus_register(&css_bus_type)))
765 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800767 /* Try to enable MSS. */
768 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
769 switch (ret) {
770 case 0: /* Success. */
771 max_ssid = __MAX_SSID;
772 break;
773 case -ENOMEM:
774 goto out_bus;
775 default:
776 max_ssid = 0;
777 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800778 /* Setup css structure. */
779 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200780 struct channel_subsystem *css;
781
782 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
783 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800784 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800785 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800786 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200787 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100788 ret = setup_css(i);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800789 if (ret)
790 goto out_free;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200791 ret = device_register(&css->device);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100792 if (ret)
793 goto out_free_all;
Cornelia Huck7e560812006-07-12 16:40:19 +0200794 if (css_characteristics_avail &&
795 css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200796 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200797 &dev_attr_cm_enable);
798 if (ret)
799 goto out_device;
800 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200801 ret = device_register(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100802 if (ret)
803 goto out_file;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800804 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200805 ret = register_reboot_notifier(&css_reboot_notifier);
806 if (ret)
807 goto out_pseudo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 css_init_done = 1;
809
810 ctl_set_bit(6, 28);
811
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800812 for_each_subchannel(__init_channel_subsystem, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 return 0;
Cornelia Hucka55360d2007-10-12 16:11:20 +0200814out_pseudo:
815 device_unregister(&channel_subsystems[i]->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100816out_file:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200817 device_remove_file(&channel_subsystems[i]->device,
818 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200819out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200820 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100821out_free_all:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200822 kfree(channel_subsystems[i]->pseudo_subchannel->lock);
823 kfree(channel_subsystems[i]->pseudo_subchannel);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800824out_free:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200825 kfree(channel_subsystems[i]);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800826out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800827 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200828 struct channel_subsystem *css;
829
Cornelia Hucka28c6942006-01-06 00:19:23 -0800830 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200831 css = channel_subsystems[i];
832 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800833 if (css_characteristics_avail && css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200834 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800835 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200836 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800837 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800838out_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 bus_unregister(&css_bus_type);
840out:
Cornelia Huckc1156182008-07-14 09:58:46 +0200841 s390_unregister_crw_handler(CRW_RSC_CSS);
Cornelia Huck4434a382007-07-27 12:29:21 +0200842 chsc_free_sei_area();
843 kfree(slow_subchannel_set);
844 printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
845 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 return ret;
847}
848
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100849int sch_is_pseudo_sch(struct subchannel *sch)
850{
851 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
852}
853
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854/*
855 * find a driver for a subchannel. They identify by the subchannel
856 * type with the exception that the console subchannel driver has its own
857 * subchannel type although the device is an i/o subchannel
858 */
859static int
860css_bus_match (struct device *dev, struct device_driver *drv)
861{
Cornelia Huck084325d2008-01-26 14:10:38 +0100862 struct subchannel *sch = to_subchannel(dev);
863 struct css_driver *driver = to_cssdriver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
865 if (sch->st == driver->subchannel_type)
866 return 1;
867
868 return 0;
869}
870
Cornelia Huck98c13c22008-01-26 14:10:40 +0100871static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +0100872{
873 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +0100874 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +0100875
876 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +0100877 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +0100878 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
879 if (ret)
880 sch->driver = NULL;
881 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +0100882}
883
Cornelia Huck98c13c22008-01-26 14:10:40 +0100884static int css_remove(struct device *dev)
885{
886 struct subchannel *sch;
887 int ret;
888
889 sch = to_subchannel(dev);
890 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
891 sch->driver = NULL;
892 return ret;
893}
894
895static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +0100896{
897 struct subchannel *sch;
898
899 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +0100900 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +0100901 sch->driver->shutdown(sch);
902}
903
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200904static int css_uevent(struct device *dev, struct kobj_uevent_env *env)
905{
906 struct subchannel *sch = to_subchannel(dev);
907 int ret;
908
909 ret = add_uevent_var(env, "ST=%01X", sch->st);
910 if (ret)
911 return ret;
912 ret = add_uevent_var(env, "MODALIAS=css:t%01X", sch->st);
913 return ret;
914}
915
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +0100917 .name = "css",
918 .match = css_bus_match,
919 .probe = css_probe,
920 .remove = css_remove,
921 .shutdown = css_shutdown,
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200922 .uevent = css_uevent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923};
924
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100925/**
926 * css_driver_register - register a css driver
927 * @cdrv: css driver to register
928 *
929 * This is mainly a wrapper around driver_register that sets name
930 * and bus_type in the embedded struct device_driver correctly.
931 */
932int css_driver_register(struct css_driver *cdrv)
933{
934 cdrv->drv.name = cdrv->name;
935 cdrv->drv.bus = &css_bus_type;
Cornelia Huck4beee642008-01-26 14:10:47 +0100936 cdrv->drv.owner = cdrv->owner;
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100937 return driver_register(&cdrv->drv);
938}
939EXPORT_SYMBOL_GPL(css_driver_register);
940
941/**
942 * css_driver_unregister - unregister a css driver
943 * @cdrv: css driver to unregister
944 *
945 * This is a wrapper around driver_unregister.
946 */
947void css_driver_unregister(struct css_driver *cdrv)
948{
949 driver_unregister(&cdrv->drv);
950}
951EXPORT_SYMBOL_GPL(css_driver_unregister);
952
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953subsys_initcall(init_channel_subsystem);
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955MODULE_LICENSE("GPL");
956EXPORT_SYMBOL(css_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957EXPORT_SYMBOL_GPL(css_characteristics_avail);