blob: 76bbb1e74c2988c025d695c3927fc690ca067ba6 [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>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020016#include <asm/isc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
Cornelia Huckc1156182008-07-14 09:58:46 +020018#include "../s390mach.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include "css.h"
20#include "cio.h"
21#include "cio_debug.h"
22#include "ioasm.h"
23#include "chsc.h"
Peter Oberparleiter40154b82006-06-29 14:57:03 +020024#include "device.h"
Peter Oberparleiter83b33702007-04-27 16:01:34 +020025#include "idset.h"
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +020026#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Linus Torvalds1da177e2005-04-16 15:20:36 -070028int css_init_done = 0;
Peter Oberparleiter40154b82006-06-29 14:57:03 +020029static int need_reprobe = 0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -080030static int max_ssid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Cornelia Huck7c9f4e32007-10-12 16:11:13 +020032struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Heiko Carstens4d284ca2007-02-05 21:18:53 +010034int
Cornelia Huckf97a56f2006-01-06 00:19:22 -080035for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
36{
37 struct subchannel_id schid;
38 int ret;
39
40 init_subchannel_id(&schid);
41 ret = -ENODEV;
42 do {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080043 do {
44 ret = fn(schid, data);
45 if (ret)
46 break;
47 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
48 schid.sch_no = 0;
49 } while (schid.ssid++ < max_ssid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080050 return ret;
51}
52
Peter Oberparleitere82a1562008-01-26 14:10:48 +010053struct cb_data {
54 void *data;
55 struct idset *set;
56 int (*fn_known_sch)(struct subchannel *, void *);
57 int (*fn_unknown_sch)(struct subchannel_id, void *);
58};
59
60static int call_fn_known_sch(struct device *dev, void *data)
61{
62 struct subchannel *sch = to_subchannel(dev);
63 struct cb_data *cb = data;
64 int rc = 0;
65
66 idset_sch_del(cb->set, sch->schid);
67 if (cb->fn_known_sch)
68 rc = cb->fn_known_sch(sch, cb->data);
69 return rc;
70}
71
72static int call_fn_unknown_sch(struct subchannel_id schid, void *data)
73{
74 struct cb_data *cb = data;
75 int rc = 0;
76
77 if (idset_sch_contains(cb->set, schid))
78 rc = cb->fn_unknown_sch(schid, cb->data);
79 return rc;
80}
81
82int for_each_subchannel_staged(int (*fn_known)(struct subchannel *, void *),
83 int (*fn_unknown)(struct subchannel_id,
84 void *), void *data)
85{
86 struct cb_data cb;
87 int rc;
88
89 cb.set = idset_sch_new();
90 if (!cb.set)
91 return -ENOMEM;
92 idset_fill(cb.set);
93 cb.data = data;
94 cb.fn_known_sch = fn_known;
95 cb.fn_unknown_sch = fn_unknown;
96 /* Process registered subchannels. */
97 rc = bus_for_each_dev(&css_bus_type, NULL, &cb, call_fn_known_sch);
98 if (rc)
99 goto out;
100 /* Process unregistered subchannels. */
101 if (fn_unknown)
102 rc = for_each_subchannel(call_fn_unknown_sch, &cb);
103out:
104 idset_free(cb.set);
105
106 return rc;
107}
108
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109static struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800110css_alloc_subchannel(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
112 struct subchannel *sch;
113 int ret;
114
115 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
116 if (sch == NULL)
117 return ERR_PTR(-ENOMEM);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800118 ret = cio_validate_subchannel (sch, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (ret < 0) {
120 kfree(sch);
121 return ERR_PTR(ret);
122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 return sch;
124}
125
126static void
127css_free_subchannel(struct subchannel *sch)
128{
129 if (sch) {
130 /* Reset intparm to zeroes. */
131 sch->schib.pmcw.intparm = 0;
132 cio_modify(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100133 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 kfree(sch);
135 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136}
137
138static void
139css_subchannel_release(struct device *dev)
140{
141 struct subchannel *sch;
142
143 sch = to_subchannel(dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100144 if (!cio_is_console(sch->schid)) {
145 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 kfree(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
Cornelia Huck07c6a332007-07-27 12:29:10 +0200150static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200151{
152 int ret;
153
154 mutex_lock(&sch->reg_mutex);
155 ret = device_register(&sch->dev);
156 mutex_unlock(&sch->reg_mutex);
157 return ret;
158}
159
Cornelia Huck44a1c192008-07-14 09:58:47 +0200160/**
161 * css_sch_device_unregister - unregister a subchannel
162 * @sch: subchannel to be unregistered
163 */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200164void css_sch_device_unregister(struct subchannel *sch)
165{
166 mutex_lock(&sch->reg_mutex);
Sebastian Ottef60cd12008-07-14 09:59:20 +0200167 if (device_is_registered(&sch->dev))
168 device_unregister(&sch->dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200169 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;
Cornelia Huckb3a686f2008-07-14 09:58:48 +0200335 if ((schib->pmcw.st == SUBCHANNEL_TYPE_MSG) && !schib->pmcw.w)
336 return 0;
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100337 return 1;
338}
339EXPORT_SYMBOL_GPL(css_sch_is_valid);
340
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200341static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
342{
343 struct schib schib;
344
345 if (!slow) {
346 /* Will be done on the slow path. */
347 return -EAGAIN;
348 }
Cornelia Huckb279a4f2008-01-26 14:10:45 +0100349 if (stsch_err(schid, &schib) || !css_sch_is_valid(&schib)) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200350 /* Unusable - ignore. */
351 return 0;
352 }
353 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
354 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
355
356 return css_probe_device(schid);
357}
358
Cornelia Huckc820de32008-07-14 09:58:45 +0200359static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
360{
361 int ret = 0;
362
363 if (sch->driver) {
364 if (sch->driver->sch_event)
365 ret = sch->driver->sch_event(sch, slow);
366 else
367 dev_dbg(&sch->dev,
368 "Got subchannel machine check but "
369 "no sch_event handler provided.\n");
370 }
371 return ret;
372}
373
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200374static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200375{
376 struct subchannel *sch;
377 int ret;
378
379 sch = get_subchannel_by_schid(schid);
380 if (sch) {
381 ret = css_evaluate_known_subchannel(sch, slow);
382 put_device(&sch->dev);
383 } else
384 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200385 if (ret == -EAGAIN)
386 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387}
388
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200389static struct idset *slow_subchannel_set;
390static spinlock_t slow_subchannel_lock;
391
392static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200394 spin_lock_init(&slow_subchannel_lock);
395 slow_subchannel_set = idset_sch_new();
396 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200397 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200398 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200400 return 0;
401}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100403static int slow_eval_known_fn(struct subchannel *sch, void *data)
404{
405 int eval;
406 int rc;
407
408 spin_lock_irq(&slow_subchannel_lock);
409 eval = idset_sch_contains(slow_subchannel_set, sch->schid);
410 idset_sch_del(slow_subchannel_set, sch->schid);
411 spin_unlock_irq(&slow_subchannel_lock);
412 if (eval) {
413 rc = css_evaluate_known_subchannel(sch, 1);
414 if (rc == -EAGAIN)
415 css_schedule_eval(sch->schid);
416 }
417 return 0;
418}
419
420static int slow_eval_unknown_fn(struct subchannel_id schid, void *data)
421{
422 int eval;
423 int rc = 0;
424
425 spin_lock_irq(&slow_subchannel_lock);
426 eval = idset_sch_contains(slow_subchannel_set, schid);
427 idset_sch_del(slow_subchannel_set, schid);
428 spin_unlock_irq(&slow_subchannel_lock);
429 if (eval) {
430 rc = css_evaluate_new_subchannel(schid, 1);
431 switch (rc) {
432 case -EAGAIN:
433 css_schedule_eval(schid);
434 rc = 0;
435 break;
436 case -ENXIO:
437 case -ENOMEM:
438 case -EIO:
439 /* These should abort looping */
440 break;
441 default:
442 rc = 0;
443 }
444 }
445 return rc;
446}
447
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200448static void css_slow_path_func(struct work_struct *unused)
449{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200450 CIO_TRACE_EVENT(4, "slowpath");
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100451 for_each_subchannel_staged(slow_eval_known_fn, slow_eval_unknown_fn,
452 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453}
454
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200455static DECLARE_WORK(slow_path_work, css_slow_path_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456struct workqueue_struct *slow_path_wq;
457
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200458void css_schedule_eval(struct subchannel_id schid)
459{
460 unsigned long flags;
461
462 spin_lock_irqsave(&slow_subchannel_lock, flags);
463 idset_sch_add(slow_subchannel_set, schid);
464 queue_work(slow_path_wq, &slow_path_work);
465 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
466}
467
468void css_schedule_eval_all(void)
469{
470 unsigned long flags;
471
472 spin_lock_irqsave(&slow_subchannel_lock, flags);
473 idset_fill(slow_subchannel_set);
474 queue_work(slow_path_wq, &slow_path_work);
475 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
476}
477
Cornelia Huck22806dc2008-04-17 07:45:59 +0200478void css_wait_for_slow_path(void)
479{
Cornelia Huck22806dc2008-04-17 07:45:59 +0200480 flush_workqueue(slow_path_wq);
481}
482
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200483/* Reprobe subchannel if unregistered. */
484static int reprobe_subchannel(struct subchannel_id schid, void *data)
485{
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200486 int ret;
487
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200488 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
489 schid.ssid, schid.sch_no);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200490 if (need_reprobe)
491 return -EAGAIN;
492
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200493 ret = css_probe_device(schid);
494 switch (ret) {
495 case 0:
496 break;
497 case -ENXIO:
498 case -ENOMEM:
Peter Oberparleiter671756162007-12-04 16:09:02 +0100499 case -EIO:
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200500 /* These should abort looping */
501 break;
502 default:
503 ret = 0;
504 }
505
506 return ret;
507}
508
509/* Work function used to reprobe all unregistered subchannels. */
Al Viro4927b3f2006-12-06 19:18:20 +0000510static void reprobe_all(struct work_struct *unused)
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200511{
512 int ret;
513
Michael Ernst139b83d2008-05-07 09:22:54 +0200514 CIO_MSG_EVENT(4, "reprobe start\n");
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200515
516 need_reprobe = 0;
517 /* Make sure initial subchannel scan is done. */
518 wait_event(ccw_device_init_wq,
519 atomic_read(&ccw_device_init_count) == 0);
Peter Oberparleitere82a1562008-01-26 14:10:48 +0100520 ret = for_each_subchannel_staged(NULL, reprobe_subchannel, NULL);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200521
Michael Ernst139b83d2008-05-07 09:22:54 +0200522 CIO_MSG_EVENT(4, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200523 need_reprobe);
524}
525
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100526static DECLARE_WORK(css_reprobe_work, reprobe_all);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200527
528/* Schedule reprobing of all unregistered subchannels. */
529void css_schedule_reprobe(void)
530{
531 need_reprobe = 1;
Cornelia Huckc5d4a992007-11-20 11:13:41 +0100532 queue_work(slow_path_wq, &css_reprobe_work);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200533}
534
535EXPORT_SYMBOL_GPL(css_schedule_reprobe);
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * Called from the machine check handler for subchannel report words.
539 */
Cornelia Huckc1156182008-07-14 09:58:46 +0200540static void css_process_crw(struct crw *crw0, struct crw *crw1, int overflow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800542 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Cornelia Huckc1156182008-07-14 09:58:46 +0200544 if (overflow) {
545 css_schedule_eval_all();
546 return;
547 }
548 CIO_CRW_EVENT(2, "CRW0 reports slct=%d, oflw=%d, "
549 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
550 crw0->slct, crw0->oflw, crw0->chn, crw0->rsc, crw0->anc,
551 crw0->erc, crw0->rsid);
552 if (crw1)
553 CIO_CRW_EVENT(2, "CRW1 reports slct=%d, oflw=%d, "
554 "chn=%d, rsc=%X, anc=%d, erc=%X, rsid=%X\n",
555 crw1->slct, crw1->oflw, crw1->chn, crw1->rsc,
556 crw1->anc, crw1->erc, crw1->rsid);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800557 init_subchannel_id(&mchk_schid);
Cornelia Huckc1156182008-07-14 09:58:46 +0200558 mchk_schid.sch_no = crw0->rsid;
559 if (crw1)
560 mchk_schid.ssid = (crw1->rsid >> 8) & 3;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800561
Cornelia Huckc1156182008-07-14 09:58:46 +0200562 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 * Since we are always presented with IPI in the CRW, we have to
564 * use stsch() to find out if the subchannel in question has come
565 * or gone.
566 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200567 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568}
569
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800570static int __init
571__init_channel_subsystem(struct subchannel_id schid, void *data)
572{
573 struct subchannel *sch;
574 int ret;
575
576 if (cio_is_console(schid))
577 sch = cio_get_console_subchannel();
578 else {
579 sch = css_alloc_subchannel(schid);
580 if (IS_ERR(sch))
581 ret = PTR_ERR(sch);
582 else
583 ret = 0;
584 switch (ret) {
585 case 0:
586 break;
587 case -ENOMEM:
588 panic("Out of memory in init_channel_subsystem\n");
589 /* -ENXIO: no more subchannels. */
590 case -ENXIO:
591 return ret;
Greg Smithe843e282006-03-14 19:50:17 -0800592 /* -EIO: this subchannel set not supported. */
593 case -EIO:
594 return ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800595 default:
596 return 0;
597 }
598 }
599 /*
600 * We register ALL valid subchannels in ioinfo, even those
601 * that have been present before init_channel_subsystem.
602 * These subchannels can't have been registered yet (kmalloc
603 * not working) so we do it now. This is true e.g. for the
604 * console subchannel.
605 */
606 css_register_subchannel(sch);
607 return 0;
608}
609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800611css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612{
Cornelia Huck75784c02008-07-14 09:58:57 +0200613 if (css_general_characteristics.mcss) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800614 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
615 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
616 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617#ifdef CONFIG_SMP
Cornelia Hucka28c6942006-01-06 00:19:23 -0800618 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800620 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621#endif
622 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800623 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
624 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
625 css->global_pgid.tod_high = tod_high;
626
627}
628
Cornelia Huck3b793062006-01-06 00:19:26 -0800629static void
630channel_subsystem_release(struct device *dev)
631{
632 struct channel_subsystem *css;
633
634 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800635 mutex_destroy(&css->mutex);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200636 if (css->pseudo_subchannel) {
637 /* Implies that it has been generated but never registered. */
638 css_subchannel_release(&css->pseudo_subchannel->dev);
639 css->pseudo_subchannel = NULL;
640 }
Cornelia Huck3b793062006-01-06 00:19:26 -0800641 kfree(css);
642}
643
Cornelia Huck495a5b42006-03-24 03:15:14 -0800644static ssize_t
645css_cm_enable_show(struct device *dev, struct device_attribute *attr,
646 char *buf)
647{
648 struct channel_subsystem *css = to_css(dev);
Michael Ernst8284fb192008-04-17 07:46:01 +0200649 int ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800650
651 if (!css)
652 return 0;
Michael Ernst8284fb192008-04-17 07:46:01 +0200653 mutex_lock(&css->mutex);
654 ret = sprintf(buf, "%x\n", css->cm_enabled);
655 mutex_unlock(&css->mutex);
656 return ret;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800657}
658
659static ssize_t
660css_cm_enable_store(struct device *dev, struct device_attribute *attr,
661 const char *buf, size_t count)
662{
663 struct channel_subsystem *css = to_css(dev);
664 int ret;
Cornelia Huck2f972202008-04-30 13:38:33 +0200665 unsigned long val;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800666
Cornelia Huck2f972202008-04-30 13:38:33 +0200667 ret = strict_strtoul(buf, 16, &val);
668 if (ret)
669 return ret;
Michael Ernst8284fb192008-04-17 07:46:01 +0200670 mutex_lock(&css->mutex);
Cornelia Huck2f972202008-04-30 13:38:33 +0200671 switch (val) {
672 case 0:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800673 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
674 break;
Cornelia Huck2f972202008-04-30 13:38:33 +0200675 case 1:
Cornelia Huck495a5b42006-03-24 03:15:14 -0800676 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
677 break;
678 default:
679 ret = -EINVAL;
680 }
Michael Ernst8284fb192008-04-17 07:46:01 +0200681 mutex_unlock(&css->mutex);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800682 return ret < 0 ? ret : count;
683}
684
685static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
686
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100687static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800688{
689 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100690 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200691 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800692
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200693 css = channel_subsystems[nr];
694 memset(css, 0, sizeof(struct channel_subsystem));
695 css->pseudo_subchannel =
696 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
697 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100698 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200699 css->pseudo_subchannel->dev.parent = &css->device;
700 css->pseudo_subchannel->dev.release = css_subchannel_release;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200701 dev_set_name(&css->pseudo_subchannel->dev, "defunct");
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200702 ret = cio_create_sch_lock(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100703 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200704 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100705 return ret;
706 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200707 mutex_init(&css->mutex);
708 css->valid = 1;
709 css->cssid = nr;
Cornelia Huck1bf5b282008-10-10 21:33:10 +0200710 dev_set_name(&css->device, "css%x", nr);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200711 css->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800712 tod_high = (u32) (get_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200713 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100714 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715}
716
Cornelia Hucka55360d2007-10-12 16:11:20 +0200717static int css_reboot_event(struct notifier_block *this,
718 unsigned long event,
719 void *ptr)
720{
721 int ret, i;
722
723 ret = NOTIFY_DONE;
724 for (i = 0; i <= __MAX_CSSID; i++) {
725 struct channel_subsystem *css;
726
727 css = channel_subsystems[i];
Michael Ernst8284fb192008-04-17 07:46:01 +0200728 mutex_lock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200729 if (css->cm_enabled)
730 if (chsc_secm(css, 0))
731 ret = NOTIFY_BAD;
Michael Ernst8284fb192008-04-17 07:46:01 +0200732 mutex_unlock(&css->mutex);
Cornelia Hucka55360d2007-10-12 16:11:20 +0200733 }
734
735 return ret;
736}
737
738static struct notifier_block css_reboot_notifier = {
739 .notifier_call = css_reboot_event,
740};
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742/*
743 * Now that the driver core is running, we can setup our channel subsystem.
744 * The struct subchannel's are created during probing (except for the
745 * static console subchannel).
746 */
747static int __init
748init_channel_subsystem (void)
749{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800750 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751
Cornelia Huck4434a382007-07-27 12:29:21 +0200752 ret = chsc_determine_css_characteristics();
753 if (ret == -ENOMEM)
754 goto out; /* No need to continue. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Cornelia Huck4434a382007-07-27 12:29:21 +0200756 ret = chsc_alloc_sei_area();
757 if (ret)
758 goto out;
759
760 ret = slow_subchannel_init();
761 if (ret)
762 goto out;
763
Cornelia Huckc1156182008-07-14 09:58:46 +0200764 ret = s390_register_crw_handler(CRW_RSC_SCH, css_process_crw);
765 if (ret)
766 goto out;
767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 if ((ret = bus_register(&css_bus_type)))
769 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800771 /* Try to enable MSS. */
772 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
773 switch (ret) {
774 case 0: /* Success. */
775 max_ssid = __MAX_SSID;
776 break;
777 case -ENOMEM:
778 goto out_bus;
779 default:
780 max_ssid = 0;
781 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800782 /* Setup css structure. */
783 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200784 struct channel_subsystem *css;
785
786 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
787 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800788 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800789 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800790 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200791 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100792 ret = setup_css(i);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200793 if (ret) {
794 kfree(channel_subsystems[i]);
795 goto out_unregister;
796 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200797 ret = device_register(&css->device);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200798 if (ret) {
799 put_device(&css->device);
800 goto out_unregister;
801 }
Cornelia Huck75784c02008-07-14 09:58:57 +0200802 if (css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200803 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200804 &dev_attr_cm_enable);
805 if (ret)
806 goto out_device;
807 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200808 ret = device_register(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100809 if (ret)
810 goto out_file;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800811 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200812 ret = register_reboot_notifier(&css_reboot_notifier);
813 if (ret)
Cornelia Hucka2164b82008-09-09 12:38:57 +0200814 goto out_unregister;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 css_init_done = 1;
816
Cornelia Huck3a3fc292008-07-14 09:58:58 +0200817 /* Enable default isc for I/O subchannels. */
Cornelia Huck6ef556cc2008-07-14 09:59:01 +0200818 isc_register(IO_SCH_ISC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800820 for_each_subchannel(__init_channel_subsystem, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 return 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100822out_file:
Cornelia Hucka2164b82008-09-09 12:38:57 +0200823 if (css_chsc_characteristics.secm)
824 device_remove_file(&channel_subsystems[i]->device,
825 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200826out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200827 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800828out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800829 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200830 struct channel_subsystem *css;
831
Cornelia Hucka28c6942006-01-06 00:19:23 -0800832 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200833 css = channel_subsystems[i];
834 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Hucka2164b82008-09-09 12:38:57 +0200835 css->pseudo_subchannel = NULL;
Cornelia Huck75784c02008-07-14 09:58:57 +0200836 if (css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200837 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800838 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200839 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800840 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800841out_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 bus_unregister(&css_bus_type);
843out:
Cornelia Huckc1156182008-07-14 09:58:46 +0200844 s390_unregister_crw_handler(CRW_RSC_CSS);
Cornelia Huck4434a382007-07-27 12:29:21 +0200845 chsc_free_sei_area();
846 kfree(slow_subchannel_set);
847 printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
848 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 return ret;
850}
851
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100852int sch_is_pseudo_sch(struct subchannel *sch)
853{
854 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
855}
856
Cornelia Huckf08adc02008-07-14 09:59:03 +0200857static int css_bus_match(struct device *dev, struct device_driver *drv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
Cornelia Huck084325d2008-01-26 14:10:38 +0100859 struct subchannel *sch = to_subchannel(dev);
860 struct css_driver *driver = to_cssdriver(drv);
Cornelia Huckf08adc02008-07-14 09:59:03 +0200861 struct css_device_id *id;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862
Cornelia Huckf08adc02008-07-14 09:59:03 +0200863 for (id = driver->subchannel_type; id->match_flags; id++) {
864 if (sch->st == id->type)
865 return 1;
866 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
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);