blob: fe0ace7aece8e65edeaac816637ab135af8ef6f7 [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 *
5 * Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6 * IBM Corporation
7 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08008 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 */
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/device.h>
13#include <linux/slab.h>
14#include <linux/errno.h>
15#include <linux/list.h>
16
17#include "css.h"
18#include "cio.h"
19#include "cio_debug.h"
20#include "ioasm.h"
21#include "chsc.h"
Peter Oberparleiter40154b82006-06-29 14:57:03 +020022#include "device.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Linus Torvalds1da177e2005-04-16 15:20:36 -070024int need_rescan = 0;
25int css_init_done = 0;
Peter Oberparleiter40154b82006-06-29 14:57:03 +020026static int need_reprobe = 0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -080027static int max_ssid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070028
Cornelia Hucka28c6942006-01-06 00:19:23 -080029struct channel_subsystem *css[__MAX_CSSID + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Cornelia Hucka28c6942006-01-06 00:19:23 -080031int css_characteristics_avail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Heiko Carstens4d284ca2007-02-05 21:18:53 +010033int
Cornelia Huckf97a56f2006-01-06 00:19:22 -080034for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
35{
36 struct subchannel_id schid;
37 int ret;
38
39 init_subchannel_id(&schid);
40 ret = -ENODEV;
41 do {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080042 do {
43 ret = fn(schid, data);
44 if (ret)
45 break;
46 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
47 schid.sch_no = 0;
48 } while (schid.ssid++ < max_ssid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080049 return ret;
50}
51
Linus Torvalds1da177e2005-04-16 15:20:36 -070052static struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -080053css_alloc_subchannel(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070054{
55 struct subchannel *sch;
56 int ret;
57
58 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
59 if (sch == NULL)
60 return ERR_PTR(-ENOMEM);
Cornelia Hucka8237fc2006-01-06 00:19:21 -080061 ret = cio_validate_subchannel (sch, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 if (ret < 0) {
63 kfree(sch);
64 return ERR_PTR(ret);
65 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 if (sch->st != SUBCHANNEL_TYPE_IO) {
68 /* For now we ignore all non-io subchannels. */
69 kfree(sch);
70 return ERR_PTR(-EINVAL);
71 }
72
73 /*
74 * Set intparm to subchannel address.
75 * This is fine even on 64bit since the subchannel is always located
76 * under 2G.
77 */
78 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
79 ret = cio_modify(sch);
80 if (ret) {
81 kfree(sch);
82 return ERR_PTR(ret);
83 }
84 return sch;
85}
86
87static void
88css_free_subchannel(struct subchannel *sch)
89{
90 if (sch) {
91 /* Reset intparm to zeroes. */
92 sch->schib.pmcw.intparm = 0;
93 cio_modify(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +010094 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 kfree(sch);
96 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99static void
100css_subchannel_release(struct device *dev)
101{
102 struct subchannel *sch;
103
104 sch = to_subchannel(dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100105 if (!cio_is_console(sch->schid)) {
106 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 kfree(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100108 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109}
110
Cornelia Huck6ab48792006-07-12 16:39:50 +0200111int css_sch_device_register(struct subchannel *sch)
112{
113 int ret;
114
115 mutex_lock(&sch->reg_mutex);
116 ret = device_register(&sch->dev);
117 mutex_unlock(&sch->reg_mutex);
118 return ret;
119}
120
121void css_sch_device_unregister(struct subchannel *sch)
122{
123 mutex_lock(&sch->reg_mutex);
124 device_unregister(&sch->dev);
125 mutex_unlock(&sch->reg_mutex);
126}
127
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static int
129css_register_subchannel(struct subchannel *sch)
130{
131 int ret;
132
133 /* Initialize the subchannel structure */
Cornelia Hucka28c6942006-01-06 00:19:23 -0800134 sch->dev.parent = &css[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 sch->dev.bus = &css_bus_type;
136 sch->dev.release = &css_subchannel_release;
Cornelia Huck529192f2006-12-08 15:55:57 +0100137 sch->dev.groups = subch_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100138
Stefan Badere42734e2006-12-15 17:18:30 +0100139 css_get_ssd_info(sch);
140
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200142 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100143 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 printk (KERN_WARNING "%s: could not register %s\n",
145 __func__, sch->dev.bus_id);
Cornelia Huck7674da72006-12-08 15:54:21 +0100146 return ret;
147 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 return ret;
149}
150
151int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800152css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 int ret;
155 struct subchannel *sch;
156
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800157 sch = css_alloc_subchannel(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 if (IS_ERR(sch))
159 return PTR_ERR(sch);
160 ret = css_register_subchannel(sch);
161 if (ret)
162 css_free_subchannel(sch);
163 return ret;
164}
165
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700166static int
167check_subchannel(struct device * dev, void * data)
168{
169 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800170 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700171
172 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800173 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700174}
175
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800177get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 struct device *dev;
180
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700181 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200182 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700184 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100187static int css_get_subchannel_status(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200191 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return CIO_GONE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200193 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return CIO_REVALIDATE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200195 if (!sch->lpm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return CIO_NO_PATH;
197 return CIO_OPER;
198}
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200199
200static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
202 int event, ret, disc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 unsigned long flags;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200204 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Cornelia Huck2ec22982006-12-08 15:54:26 +0100206 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200207 disc = device_is_disconnected(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (disc && slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200209 /* Disconnected devices are evaluated directly only.*/
Cornelia Huck2ec22982006-12-08 15:54:26 +0100210 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200211 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200213 /* No interrupt after machine check - kill pending timers. */
214 device_kill_pending_timer(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (!disc && !slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200216 /* Non-disconnected devices are evaluated on the slow path. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100217 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200218 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200220 event = css_get_subchannel_status(sch);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800221 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200222 sch->schid.ssid, sch->schid.sch_no, event,
223 disc ? "disconnected" : "normal",
224 slow ? "slow" : "fast");
225 /* Analyze subchannel status. */
226 action = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 switch (event) {
228 case CIO_NO_PATH:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200229 if (disc) {
230 /* Check if paths have become available. */
231 action = REPROBE;
232 break;
233 }
234 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 case CIO_GONE:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200236 /* Prevent unwanted effects when opening lock. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 cio_disable_subchannel(sch);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200238 device_set_disconnected(sch);
239 /* Ask driver what to do with device. */
240 action = UNREGISTER;
241 if (sch->driver && sch->driver->notify) {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100242 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200243 ret = sch->driver->notify(&sch->dev, event);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100244 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200245 if (ret)
246 action = NONE;
247 }
248 break;
249 case CIO_REVALIDATE:
250 /* Device will be removed, so no notify necessary. */
251 if (disc)
252 /* Reprobe because immediate unregister might block. */
253 action = REPROBE;
254 else
255 action = UNREGISTER_PROBE;
256 break;
257 case CIO_OPER:
258 if (disc)
259 /* Get device operational again. */
260 action = REPROBE;
261 break;
262 }
263 /* Perform action. */
264 ret = 0;
265 switch (action) {
266 case UNREGISTER:
267 case UNREGISTER_PROBE:
268 /* Unregister device (will use subchannel lock). */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100269 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200270 css_sch_device_unregister(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100271 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* Reset intparm to zeroes. */
274 sch->schib.pmcw.intparm = 0;
275 cio_modify(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 break;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200277 case REPROBE:
278 device_trigger_reprobe(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 break;
280 default:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200281 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282 }
Cornelia Huck2ec22982006-12-08 15:54:26 +0100283 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huckc2b14492006-10-27 12:39:17 +0200284 /* Probe if necessary. */
285 if (action == UNREGISTER_PROBE)
286 ret = css_probe_device(sch->schid);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200287
288 return ret;
289}
290
291static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
292{
293 struct schib schib;
294
295 if (!slow) {
296 /* Will be done on the slow path. */
297 return -EAGAIN;
298 }
Cornelia Huck758976f2007-02-05 21:17:36 +0100299 if (stsch_err(schid, &schib) || !schib.pmcw.dnv) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200300 /* Unusable - ignore. */
301 return 0;
302 }
303 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
304 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
305
306 return css_probe_device(schid);
307}
308
309static int css_evaluate_subchannel(struct subchannel_id schid, int slow)
310{
311 struct subchannel *sch;
312 int ret;
313
314 sch = get_subchannel_by_schid(schid);
315 if (sch) {
316 ret = css_evaluate_known_subchannel(sch, slow);
317 put_device(&sch->dev);
318 } else
319 ret = css_evaluate_new_subchannel(schid, slow);
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 return ret;
322}
323
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800324static int
325css_rescan_devices(struct subchannel_id schid, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800327 return css_evaluate_subchannel(schid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328}
329
330struct slow_subchannel {
331 struct list_head slow_list;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800332 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333};
334
335static LIST_HEAD(slow_subchannels_head);
336static DEFINE_SPINLOCK(slow_subchannel_lock);
337
338static void
Al Viro4927b3f2006-12-06 19:18:20 +0000339css_trigger_slow_path(struct work_struct *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
341 CIO_TRACE_EVENT(4, "slowpath");
342
343 if (need_rescan) {
344 need_rescan = 0;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800345 for_each_subchannel(css_rescan_devices, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 return;
347 }
348
349 spin_lock_irq(&slow_subchannel_lock);
350 while (!list_empty(&slow_subchannels_head)) {
351 struct slow_subchannel *slow_sch =
352 list_entry(slow_subchannels_head.next,
353 struct slow_subchannel, slow_list);
354
355 list_del_init(slow_subchannels_head.next);
356 spin_unlock_irq(&slow_subchannel_lock);
357 css_evaluate_subchannel(slow_sch->schid, 1);
358 spin_lock_irq(&slow_subchannel_lock);
359 kfree(slow_sch);
360 }
361 spin_unlock_irq(&slow_subchannel_lock);
362}
363
Al Viro4927b3f2006-12-06 19:18:20 +0000364DECLARE_WORK(slow_path_work, css_trigger_slow_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365struct workqueue_struct *slow_path_wq;
366
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200367/* Reprobe subchannel if unregistered. */
368static int reprobe_subchannel(struct subchannel_id schid, void *data)
369{
370 struct subchannel *sch;
371 int ret;
372
373 CIO_DEBUG(KERN_INFO, 6, "cio: reprobe 0.%x.%04x\n",
374 schid.ssid, schid.sch_no);
375 if (need_reprobe)
376 return -EAGAIN;
377
378 sch = get_subchannel_by_schid(schid);
379 if (sch) {
380 /* Already known. */
381 put_device(&sch->dev);
382 return 0;
383 }
384
385 ret = css_probe_device(schid);
386 switch (ret) {
387 case 0:
388 break;
389 case -ENXIO:
390 case -ENOMEM:
391 /* These should abort looping */
392 break;
393 default:
394 ret = 0;
395 }
396
397 return ret;
398}
399
400/* Work function used to reprobe all unregistered subchannels. */
Al Viro4927b3f2006-12-06 19:18:20 +0000401static void reprobe_all(struct work_struct *unused)
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200402{
403 int ret;
404
405 CIO_MSG_EVENT(2, "reprobe start\n");
406
407 need_reprobe = 0;
408 /* Make sure initial subchannel scan is done. */
409 wait_event(ccw_device_init_wq,
410 atomic_read(&ccw_device_init_count) == 0);
411 ret = for_each_subchannel(reprobe_subchannel, NULL);
412
413 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
414 need_reprobe);
415}
416
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100417static DECLARE_WORK(css_reprobe_work, reprobe_all);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200418
419/* Schedule reprobing of all unregistered subchannels. */
420void css_schedule_reprobe(void)
421{
422 need_reprobe = 1;
423 queue_work(ccw_device_work, &css_reprobe_work);
424}
425
426EXPORT_SYMBOL_GPL(css_schedule_reprobe);
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428/*
429 * Rescan for new devices. FIXME: This is slow.
430 * This function is called when we have lost CRWs due to overflows and we have
431 * to do subchannel housekeeping.
432 */
433void
434css_reiterate_subchannels(void)
435{
436 css_clear_subchannel_slow_list();
437 need_rescan = 1;
438}
439
440/*
441 * Called from the machine check handler for subchannel report words.
442 */
443int
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800444css_process_crw(int rsid1, int rsid2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
446 int ret;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800447 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800449 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
450 rsid1, rsid2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 if (need_rescan)
453 /* We need to iterate all subchannels anyway. */
454 return -EAGAIN;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800455
456 init_subchannel_id(&mchk_schid);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800457 mchk_schid.sch_no = rsid1;
458 if (rsid2 != 0)
459 mchk_schid.ssid = (rsid2 >> 8) & 3;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 /*
462 * Since we are always presented with IPI in the CRW, we have to
463 * use stsch() to find out if the subchannel in question has come
464 * or gone.
465 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800466 ret = css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 if (ret == -EAGAIN) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800468 if (css_enqueue_subchannel_slow(mchk_schid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 css_clear_subchannel_slow_list();
470 need_rescan = 1;
471 }
472 }
473 return ret;
474}
475
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800476static int __init
477__init_channel_subsystem(struct subchannel_id schid, void *data)
478{
479 struct subchannel *sch;
480 int ret;
481
482 if (cio_is_console(schid))
483 sch = cio_get_console_subchannel();
484 else {
485 sch = css_alloc_subchannel(schid);
486 if (IS_ERR(sch))
487 ret = PTR_ERR(sch);
488 else
489 ret = 0;
490 switch (ret) {
491 case 0:
492 break;
493 case -ENOMEM:
494 panic("Out of memory in init_channel_subsystem\n");
495 /* -ENXIO: no more subchannels. */
496 case -ENXIO:
497 return ret;
Greg Smithe843e282006-03-14 19:50:17 -0800498 /* -EIO: this subchannel set not supported. */
499 case -EIO:
500 return ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800501 default:
502 return 0;
503 }
504 }
505 /*
506 * We register ALL valid subchannels in ioinfo, even those
507 * that have been present before init_channel_subsystem.
508 * These subchannels can't have been registered yet (kmalloc
509 * not working) so we do it now. This is true e.g. for the
510 * console subchannel.
511 */
512 css_register_subchannel(sch);
513 return 0;
514}
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800517css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800519 if (css_characteristics_avail && css_general_characteristics.mcss) {
520 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
521 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
522 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523#ifdef CONFIG_SMP
Cornelia Hucka28c6942006-01-06 00:19:23 -0800524 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800526 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527#endif
528 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800529 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
530 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
531 css->global_pgid.tod_high = tod_high;
532
533}
534
Cornelia Huck3b793062006-01-06 00:19:26 -0800535static void
536channel_subsystem_release(struct device *dev)
537{
538 struct channel_subsystem *css;
539
540 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800541 mutex_destroy(&css->mutex);
Cornelia Huck3b793062006-01-06 00:19:26 -0800542 kfree(css);
543}
544
Cornelia Huck495a5b42006-03-24 03:15:14 -0800545static ssize_t
546css_cm_enable_show(struct device *dev, struct device_attribute *attr,
547 char *buf)
548{
549 struct channel_subsystem *css = to_css(dev);
550
551 if (!css)
552 return 0;
553 return sprintf(buf, "%x\n", css->cm_enabled);
554}
555
556static ssize_t
557css_cm_enable_store(struct device *dev, struct device_attribute *attr,
558 const char *buf, size_t count)
559{
560 struct channel_subsystem *css = to_css(dev);
561 int ret;
562
563 switch (buf[0]) {
564 case '0':
565 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
566 break;
567 case '1':
568 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
569 break;
570 default:
571 ret = -EINVAL;
572 }
573 return ret < 0 ? ret : count;
574}
575
576static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
577
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100578static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800579{
580 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100581 int ret;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800582
583 memset(css[nr], 0, sizeof(struct channel_subsystem));
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100584 css[nr]->pseudo_subchannel =
585 kzalloc(sizeof(*css[nr]->pseudo_subchannel), GFP_KERNEL);
586 if (!css[nr]->pseudo_subchannel)
587 return -ENOMEM;
588 css[nr]->pseudo_subchannel->dev.parent = &css[nr]->device;
589 css[nr]->pseudo_subchannel->dev.release = css_subchannel_release;
590 sprintf(css[nr]->pseudo_subchannel->dev.bus_id, "defunct");
591 ret = cio_create_sch_lock(css[nr]->pseudo_subchannel);
592 if (ret) {
593 kfree(css[nr]->pseudo_subchannel);
594 return ret;
595 }
Cornelia Huck495a5b42006-03-24 03:15:14 -0800596 mutex_init(&css[nr]->mutex);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800597 css[nr]->valid = 1;
598 css[nr]->cssid = nr;
599 sprintf(css[nr]->device.bus_id, "css%x", nr);
Cornelia Huck3b793062006-01-06 00:19:26 -0800600 css[nr]->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800601 tod_high = (u32) (get_clock() >> 32);
602 css_generate_pgid(css[nr], tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100603 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604}
605
606/*
607 * Now that the driver core is running, we can setup our channel subsystem.
608 * The struct subchannel's are created during probing (except for the
609 * static console subchannel).
610 */
611static int __init
612init_channel_subsystem (void)
613{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800614 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 if (chsc_determine_css_characteristics() == 0)
617 css_characteristics_avail = 1;
618
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 if ((ret = bus_register(&css_bus_type)))
620 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800622 /* Try to enable MSS. */
623 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
624 switch (ret) {
625 case 0: /* Success. */
626 max_ssid = __MAX_SSID;
627 break;
628 case -ENOMEM:
629 goto out_bus;
630 default:
631 max_ssid = 0;
632 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800633 /* Setup css structure. */
634 for (i = 0; i <= __MAX_CSSID; i++) {
635 css[i] = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
636 if (!css[i]) {
637 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800638 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800639 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100640 ret = setup_css(i);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800641 if (ret)
642 goto out_free;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100643 ret = device_register(&css[i]->device);
644 if (ret)
645 goto out_free_all;
Cornelia Huck7e560812006-07-12 16:40:19 +0200646 if (css_characteristics_avail &&
647 css_chsc_characteristics.secm) {
648 ret = device_create_file(&css[i]->device,
649 &dev_attr_cm_enable);
650 if (ret)
651 goto out_device;
652 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100653 ret = device_register(&css[i]->pseudo_subchannel->dev);
654 if (ret)
655 goto out_file;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800656 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 css_init_done = 1;
658
659 ctl_set_bit(6, 28);
660
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800661 for_each_subchannel(__init_channel_subsystem, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 return 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100663out_file:
664 device_remove_file(&css[i]->device, &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200665out_device:
666 device_unregister(&css[i]->device);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100667out_free_all:
668 kfree(css[i]->pseudo_subchannel->lock);
669 kfree(css[i]->pseudo_subchannel);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800670out_free:
671 kfree(css[i]);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800672out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800673 while (i > 0) {
674 i--;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100675 device_unregister(&css[i]->pseudo_subchannel->dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800676 if (css_characteristics_avail && css_chsc_characteristics.secm)
677 device_remove_file(&css[i]->device,
678 &dev_attr_cm_enable);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800679 device_unregister(&css[i]->device);
680 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800681out_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 bus_unregister(&css_bus_type);
683out:
684 return ret;
685}
686
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100687int sch_is_pseudo_sch(struct subchannel *sch)
688{
689 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
690}
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692/*
693 * find a driver for a subchannel. They identify by the subchannel
694 * type with the exception that the console subchannel driver has its own
695 * subchannel type although the device is an i/o subchannel
696 */
697static int
698css_bus_match (struct device *dev, struct device_driver *drv)
699{
700 struct subchannel *sch = container_of (dev, struct subchannel, dev);
701 struct css_driver *driver = container_of (drv, struct css_driver, drv);
702
703 if (sch->st == driver->subchannel_type)
704 return 1;
705
706 return 0;
707}
708
Cornelia Huck8bbace72006-01-11 10:56:22 +0100709static int
710css_probe (struct device *dev)
711{
712 struct subchannel *sch;
713
714 sch = to_subchannel(dev);
715 sch->driver = container_of (dev->driver, struct css_driver, drv);
716 return (sch->driver->probe ? sch->driver->probe(sch) : 0);
717}
718
719static int
720css_remove (struct device *dev)
721{
722 struct subchannel *sch;
723
724 sch = to_subchannel(dev);
725 return (sch->driver->remove ? sch->driver->remove(sch) : 0);
726}
727
728static void
729css_shutdown (struct device *dev)
730{
731 struct subchannel *sch;
732
733 sch = to_subchannel(dev);
734 if (sch->driver->shutdown)
735 sch->driver->shutdown(sch);
736}
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +0100739 .name = "css",
740 .match = css_bus_match,
741 .probe = css_probe,
742 .remove = css_remove,
743 .shutdown = css_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744};
745
746subsys_initcall(init_channel_subsystem);
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800749css_enqueue_subchannel_slow(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750{
751 struct slow_subchannel *new_slow_sch;
752 unsigned long flags;
753
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800754 new_slow_sch = kzalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 if (!new_slow_sch)
756 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 new_slow_sch->schid = schid;
758 spin_lock_irqsave(&slow_subchannel_lock, flags);
759 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
760 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
761 return 0;
762}
763
764void
765css_clear_subchannel_slow_list(void)
766{
767 unsigned long flags;
768
769 spin_lock_irqsave(&slow_subchannel_lock, flags);
770 while (!list_empty(&slow_subchannels_head)) {
771 struct slow_subchannel *slow_sch =
772 list_entry(slow_subchannels_head.next,
773 struct slow_subchannel, slow_list);
774
775 list_del_init(slow_subchannels_head.next);
776 kfree(slow_sch);
777 }
778 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
779}
780
781
782
783int
784css_slow_subchannels_exist(void)
785{
786 return (!list_empty(&slow_subchannels_head));
787}
788
789MODULE_LICENSE("GPL");
790EXPORT_SYMBOL(css_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791EXPORT_SYMBOL_GPL(css_characteristics_avail);