blob: 4c81d890791e48d1c04b001d842af56a49301c00 [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
Cornelia Huckf97a56f2006-01-06 00:19:22 -080033inline int
34for_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
111extern int css_get_ssd_info(struct subchannel *sch);
112
Cornelia Huck6ab48792006-07-12 16:39:50 +0200113
114int css_sch_device_register(struct subchannel *sch)
115{
116 int ret;
117
118 mutex_lock(&sch->reg_mutex);
119 ret = device_register(&sch->dev);
120 mutex_unlock(&sch->reg_mutex);
121 return ret;
122}
123
124void css_sch_device_unregister(struct subchannel *sch)
125{
126 mutex_lock(&sch->reg_mutex);
127 device_unregister(&sch->dev);
128 mutex_unlock(&sch->reg_mutex);
129}
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131static int
132css_register_subchannel(struct subchannel *sch)
133{
134 int ret;
135
136 /* Initialize the subchannel structure */
Cornelia Hucka28c6942006-01-06 00:19:23 -0800137 sch->dev.parent = &css[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 sch->dev.bus = &css_bus_type;
139 sch->dev.release = &css_subchannel_release;
Cornelia Huck529192f2006-12-08 15:55:57 +0100140 sch->dev.groups = subch_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100141
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200143 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100144 if (ret) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 printk (KERN_WARNING "%s: could not register %s\n",
146 __func__, sch->dev.bus_id);
Cornelia Huck7674da72006-12-08 15:54:21 +0100147 return ret;
148 }
149 css_get_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 return ret;
151}
152
153int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800154css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 int ret;
157 struct subchannel *sch;
158
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800159 sch = css_alloc_subchannel(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 if (IS_ERR(sch))
161 return PTR_ERR(sch);
162 ret = css_register_subchannel(sch);
163 if (ret)
164 css_free_subchannel(sch);
165 return ret;
166}
167
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700168static int
169check_subchannel(struct device * dev, void * data)
170{
171 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800172 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700173
174 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800175 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700176}
177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800179get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 struct device *dev;
182
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700183 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200184 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700186 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200189static inline int css_get_subchannel_status(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200193 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return CIO_GONE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200195 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 return CIO_REVALIDATE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200197 if (!sch->lpm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 return CIO_NO_PATH;
199 return CIO_OPER;
200}
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200201
202static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
204 int event, ret, disc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 unsigned long flags;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200206 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
Cornelia Huck2ec22982006-12-08 15:54:26 +0100208 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200209 disc = device_is_disconnected(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 if (disc && slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200211 /* Disconnected devices are evaluated directly only.*/
Cornelia Huck2ec22982006-12-08 15:54:26 +0100212 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200213 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200215 /* No interrupt after machine check - kill pending timers. */
216 device_kill_pending_timer(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!disc && !slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200218 /* Non-disconnected devices are evaluated on the slow path. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100219 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200220 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200222 event = css_get_subchannel_status(sch);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800223 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200224 sch->schid.ssid, sch->schid.sch_no, event,
225 disc ? "disconnected" : "normal",
226 slow ? "slow" : "fast");
227 /* Analyze subchannel status. */
228 action = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 switch (event) {
230 case CIO_NO_PATH:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200231 if (disc) {
232 /* Check if paths have become available. */
233 action = REPROBE;
234 break;
235 }
236 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 case CIO_GONE:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200238 /* Prevent unwanted effects when opening lock. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 cio_disable_subchannel(sch);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200240 device_set_disconnected(sch);
241 /* Ask driver what to do with device. */
242 action = UNREGISTER;
243 if (sch->driver && sch->driver->notify) {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100244 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200245 ret = sch->driver->notify(&sch->dev, event);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100246 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200247 if (ret)
248 action = NONE;
249 }
250 break;
251 case CIO_REVALIDATE:
252 /* Device will be removed, so no notify necessary. */
253 if (disc)
254 /* Reprobe because immediate unregister might block. */
255 action = REPROBE;
256 else
257 action = UNREGISTER_PROBE;
258 break;
259 case CIO_OPER:
260 if (disc)
261 /* Get device operational again. */
262 action = REPROBE;
263 break;
264 }
265 /* Perform action. */
266 ret = 0;
267 switch (action) {
268 case UNREGISTER:
269 case UNREGISTER_PROBE:
270 /* Unregister device (will use subchannel lock). */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100271 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200272 css_sch_device_unregister(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100273 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 /* Reset intparm to zeroes. */
276 sch->schib.pmcw.intparm = 0;
277 cio_modify(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 break;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200279 case REPROBE:
280 device_trigger_reprobe(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 break;
282 default:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200283 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 }
Cornelia Huck2ec22982006-12-08 15:54:26 +0100285 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huckc2b14492006-10-27 12:39:17 +0200286 /* Probe if necessary. */
287 if (action == UNREGISTER_PROBE)
288 ret = css_probe_device(sch->schid);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200289
290 return ret;
291}
292
293static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
294{
295 struct schib schib;
296
297 if (!slow) {
298 /* Will be done on the slow path. */
299 return -EAGAIN;
300 }
301 if (stsch(schid, &schib) || !schib.pmcw.dnv) {
302 /* Unusable - ignore. */
303 return 0;
304 }
305 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
306 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
307
308 return css_probe_device(schid);
309}
310
311static int css_evaluate_subchannel(struct subchannel_id schid, int slow)
312{
313 struct subchannel *sch;
314 int ret;
315
316 sch = get_subchannel_by_schid(schid);
317 if (sch) {
318 ret = css_evaluate_known_subchannel(sch, slow);
319 put_device(&sch->dev);
320 } else
321 ret = css_evaluate_new_subchannel(schid, slow);
322
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return ret;
324}
325
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800326static int
327css_rescan_devices(struct subchannel_id schid, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800329 return css_evaluate_subchannel(schid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330}
331
332struct slow_subchannel {
333 struct list_head slow_list;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800334 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335};
336
337static LIST_HEAD(slow_subchannels_head);
338static DEFINE_SPINLOCK(slow_subchannel_lock);
339
340static void
Al Viro4927b3f2006-12-06 19:18:20 +0000341css_trigger_slow_path(struct work_struct *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342{
343 CIO_TRACE_EVENT(4, "slowpath");
344
345 if (need_rescan) {
346 need_rescan = 0;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800347 for_each_subchannel(css_rescan_devices, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 return;
349 }
350
351 spin_lock_irq(&slow_subchannel_lock);
352 while (!list_empty(&slow_subchannels_head)) {
353 struct slow_subchannel *slow_sch =
354 list_entry(slow_subchannels_head.next,
355 struct slow_subchannel, slow_list);
356
357 list_del_init(slow_subchannels_head.next);
358 spin_unlock_irq(&slow_subchannel_lock);
359 css_evaluate_subchannel(slow_sch->schid, 1);
360 spin_lock_irq(&slow_subchannel_lock);
361 kfree(slow_sch);
362 }
363 spin_unlock_irq(&slow_subchannel_lock);
364}
365
Al Viro4927b3f2006-12-06 19:18:20 +0000366DECLARE_WORK(slow_path_work, css_trigger_slow_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367struct workqueue_struct *slow_path_wq;
368
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200369/* Reprobe subchannel if unregistered. */
370static int reprobe_subchannel(struct subchannel_id schid, void *data)
371{
372 struct subchannel *sch;
373 int ret;
374
375 CIO_DEBUG(KERN_INFO, 6, "cio: reprobe 0.%x.%04x\n",
376 schid.ssid, schid.sch_no);
377 if (need_reprobe)
378 return -EAGAIN;
379
380 sch = get_subchannel_by_schid(schid);
381 if (sch) {
382 /* Already known. */
383 put_device(&sch->dev);
384 return 0;
385 }
386
387 ret = css_probe_device(schid);
388 switch (ret) {
389 case 0:
390 break;
391 case -ENXIO:
392 case -ENOMEM:
393 /* These should abort looping */
394 break;
395 default:
396 ret = 0;
397 }
398
399 return ret;
400}
401
402/* Work function used to reprobe all unregistered subchannels. */
Al Viro4927b3f2006-12-06 19:18:20 +0000403static void reprobe_all(struct work_struct *unused)
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200404{
405 int ret;
406
407 CIO_MSG_EVENT(2, "reprobe start\n");
408
409 need_reprobe = 0;
410 /* Make sure initial subchannel scan is done. */
411 wait_event(ccw_device_init_wq,
412 atomic_read(&ccw_device_init_count) == 0);
413 ret = for_each_subchannel(reprobe_subchannel, NULL);
414
415 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
416 need_reprobe);
417}
418
Al Viro4927b3f2006-12-06 19:18:20 +0000419DECLARE_WORK(css_reprobe_work, reprobe_all);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200420
421/* Schedule reprobing of all unregistered subchannels. */
422void css_schedule_reprobe(void)
423{
424 need_reprobe = 1;
425 queue_work(ccw_device_work, &css_reprobe_work);
426}
427
428EXPORT_SYMBOL_GPL(css_schedule_reprobe);
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430/*
431 * Rescan for new devices. FIXME: This is slow.
432 * This function is called when we have lost CRWs due to overflows and we have
433 * to do subchannel housekeeping.
434 */
435void
436css_reiterate_subchannels(void)
437{
438 css_clear_subchannel_slow_list();
439 need_rescan = 1;
440}
441
442/*
443 * Called from the machine check handler for subchannel report words.
444 */
445int
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800446css_process_crw(int rsid1, int rsid2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447{
448 int ret;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800449 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800451 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
452 rsid1, rsid2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
454 if (need_rescan)
455 /* We need to iterate all subchannels anyway. */
456 return -EAGAIN;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800457
458 init_subchannel_id(&mchk_schid);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800459 mchk_schid.sch_no = rsid1;
460 if (rsid2 != 0)
461 mchk_schid.ssid = (rsid2 >> 8) & 3;
462
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 /*
464 * Since we are always presented with IPI in the CRW, we have to
465 * use stsch() to find out if the subchannel in question has come
466 * or gone.
467 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800468 ret = css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 if (ret == -EAGAIN) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800470 if (css_enqueue_subchannel_slow(mchk_schid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 css_clear_subchannel_slow_list();
472 need_rescan = 1;
473 }
474 }
475 return ret;
476}
477
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800478static int __init
479__init_channel_subsystem(struct subchannel_id schid, void *data)
480{
481 struct subchannel *sch;
482 int ret;
483
484 if (cio_is_console(schid))
485 sch = cio_get_console_subchannel();
486 else {
487 sch = css_alloc_subchannel(schid);
488 if (IS_ERR(sch))
489 ret = PTR_ERR(sch);
490 else
491 ret = 0;
492 switch (ret) {
493 case 0:
494 break;
495 case -ENOMEM:
496 panic("Out of memory in init_channel_subsystem\n");
497 /* -ENXIO: no more subchannels. */
498 case -ENXIO:
499 return ret;
Greg Smithe843e282006-03-14 19:50:17 -0800500 /* -EIO: this subchannel set not supported. */
501 case -EIO:
502 return ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800503 default:
504 return 0;
505 }
506 }
507 /*
508 * We register ALL valid subchannels in ioinfo, even those
509 * that have been present before init_channel_subsystem.
510 * These subchannels can't have been registered yet (kmalloc
511 * not working) so we do it now. This is true e.g. for the
512 * console subchannel.
513 */
514 css_register_subchannel(sch);
515 return 0;
516}
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800519css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800521 if (css_characteristics_avail && css_general_characteristics.mcss) {
522 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
523 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
524 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#ifdef CONFIG_SMP
Cornelia Hucka28c6942006-01-06 00:19:23 -0800526 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800528 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529#endif
530 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800531 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
532 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
533 css->global_pgid.tod_high = tod_high;
534
535}
536
Cornelia Huck3b793062006-01-06 00:19:26 -0800537static void
538channel_subsystem_release(struct device *dev)
539{
540 struct channel_subsystem *css;
541
542 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800543 mutex_destroy(&css->mutex);
Cornelia Huck3b793062006-01-06 00:19:26 -0800544 kfree(css);
545}
546
Cornelia Huck495a5b42006-03-24 03:15:14 -0800547static ssize_t
548css_cm_enable_show(struct device *dev, struct device_attribute *attr,
549 char *buf)
550{
551 struct channel_subsystem *css = to_css(dev);
552
553 if (!css)
554 return 0;
555 return sprintf(buf, "%x\n", css->cm_enabled);
556}
557
558static ssize_t
559css_cm_enable_store(struct device *dev, struct device_attribute *attr,
560 const char *buf, size_t count)
561{
562 struct channel_subsystem *css = to_css(dev);
563 int ret;
564
565 switch (buf[0]) {
566 case '0':
567 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
568 break;
569 case '1':
570 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
571 break;
572 default:
573 ret = -EINVAL;
574 }
575 return ret < 0 ? ret : count;
576}
577
578static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
579
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100580static inline int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800581{
582 u32 tod_high;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100583 int ret;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800584
585 memset(css[nr], 0, sizeof(struct channel_subsystem));
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100586 css[nr]->pseudo_subchannel =
587 kzalloc(sizeof(*css[nr]->pseudo_subchannel), GFP_KERNEL);
588 if (!css[nr]->pseudo_subchannel)
589 return -ENOMEM;
590 css[nr]->pseudo_subchannel->dev.parent = &css[nr]->device;
591 css[nr]->pseudo_subchannel->dev.release = css_subchannel_release;
592 sprintf(css[nr]->pseudo_subchannel->dev.bus_id, "defunct");
593 ret = cio_create_sch_lock(css[nr]->pseudo_subchannel);
594 if (ret) {
595 kfree(css[nr]->pseudo_subchannel);
596 return ret;
597 }
Cornelia Huck495a5b42006-03-24 03:15:14 -0800598 mutex_init(&css[nr]->mutex);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800599 css[nr]->valid = 1;
600 css[nr]->cssid = nr;
601 sprintf(css[nr]->device.bus_id, "css%x", nr);
Cornelia Huck3b793062006-01-06 00:19:26 -0800602 css[nr]->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800603 tod_high = (u32) (get_clock() >> 32);
604 css_generate_pgid(css[nr], tod_high);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100605 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606}
607
608/*
609 * Now that the driver core is running, we can setup our channel subsystem.
610 * The struct subchannel's are created during probing (except for the
611 * static console subchannel).
612 */
613static int __init
614init_channel_subsystem (void)
615{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800616 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 if (chsc_determine_css_characteristics() == 0)
619 css_characteristics_avail = 1;
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 if ((ret = bus_register(&css_bus_type)))
622 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800624 /* Try to enable MSS. */
625 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
626 switch (ret) {
627 case 0: /* Success. */
628 max_ssid = __MAX_SSID;
629 break;
630 case -ENOMEM:
631 goto out_bus;
632 default:
633 max_ssid = 0;
634 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800635 /* Setup css structure. */
636 for (i = 0; i <= __MAX_CSSID; i++) {
637 css[i] = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
638 if (!css[i]) {
639 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800640 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800641 }
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100642 ret = setup_css(i);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800643 if (ret)
644 goto out_free;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100645 ret = device_register(&css[i]->device);
646 if (ret)
647 goto out_free_all;
Cornelia Huck7e560812006-07-12 16:40:19 +0200648 if (css_characteristics_avail &&
649 css_chsc_characteristics.secm) {
650 ret = device_create_file(&css[i]->device,
651 &dev_attr_cm_enable);
652 if (ret)
653 goto out_device;
654 }
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100655 ret = device_register(&css[i]->pseudo_subchannel->dev);
656 if (ret)
657 goto out_file;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800658 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 css_init_done = 1;
660
661 ctl_set_bit(6, 28);
662
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800663 for_each_subchannel(__init_channel_subsystem, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 return 0;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100665out_file:
666 device_remove_file(&css[i]->device, &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200667out_device:
668 device_unregister(&css[i]->device);
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100669out_free_all:
670 kfree(css[i]->pseudo_subchannel->lock);
671 kfree(css[i]->pseudo_subchannel);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800672out_free:
673 kfree(css[i]);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800674out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800675 while (i > 0) {
676 i--;
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100677 device_unregister(&css[i]->pseudo_subchannel->dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800678 if (css_characteristics_avail && css_chsc_characteristics.secm)
679 device_remove_file(&css[i]->device,
680 &dev_attr_cm_enable);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800681 device_unregister(&css[i]->device);
682 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800683out_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684 bus_unregister(&css_bus_type);
685out:
686 return ret;
687}
688
Cornelia Huckd7b5a4c2006-12-08 15:54:28 +0100689int sch_is_pseudo_sch(struct subchannel *sch)
690{
691 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
692}
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694/*
695 * find a driver for a subchannel. They identify by the subchannel
696 * type with the exception that the console subchannel driver has its own
697 * subchannel type although the device is an i/o subchannel
698 */
699static int
700css_bus_match (struct device *dev, struct device_driver *drv)
701{
702 struct subchannel *sch = container_of (dev, struct subchannel, dev);
703 struct css_driver *driver = container_of (drv, struct css_driver, drv);
704
705 if (sch->st == driver->subchannel_type)
706 return 1;
707
708 return 0;
709}
710
Cornelia Huck8bbace72006-01-11 10:56:22 +0100711static int
712css_probe (struct device *dev)
713{
714 struct subchannel *sch;
715
716 sch = to_subchannel(dev);
717 sch->driver = container_of (dev->driver, struct css_driver, drv);
718 return (sch->driver->probe ? sch->driver->probe(sch) : 0);
719}
720
721static int
722css_remove (struct device *dev)
723{
724 struct subchannel *sch;
725
726 sch = to_subchannel(dev);
727 return (sch->driver->remove ? sch->driver->remove(sch) : 0);
728}
729
730static void
731css_shutdown (struct device *dev)
732{
733 struct subchannel *sch;
734
735 sch = to_subchannel(dev);
736 if (sch->driver->shutdown)
737 sch->driver->shutdown(sch);
738}
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +0100741 .name = "css",
742 .match = css_bus_match,
743 .probe = css_probe,
744 .remove = css_remove,
745 .shutdown = css_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746};
747
748subsys_initcall(init_channel_subsystem);
749
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800751css_enqueue_subchannel_slow(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct slow_subchannel *new_slow_sch;
754 unsigned long flags;
755
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800756 new_slow_sch = kzalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 if (!new_slow_sch)
758 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 new_slow_sch->schid = schid;
760 spin_lock_irqsave(&slow_subchannel_lock, flags);
761 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
762 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
763 return 0;
764}
765
766void
767css_clear_subchannel_slow_list(void)
768{
769 unsigned long flags;
770
771 spin_lock_irqsave(&slow_subchannel_lock, flags);
772 while (!list_empty(&slow_subchannels_head)) {
773 struct slow_subchannel *slow_sch =
774 list_entry(slow_subchannels_head.next,
775 struct slow_subchannel, slow_list);
776
777 list_del_init(slow_subchannels_head.next);
778 kfree(slow_sch);
779 }
780 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
781}
782
783
784
785int
786css_slow_subchannels_exist(void)
787{
788 return (!list_empty(&slow_subchannels_head));
789}
790
791MODULE_LICENSE("GPL");
792EXPORT_SYMBOL(css_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793EXPORT_SYMBOL_GPL(css_characteristics_avail);