blob: 26cf2f5ae2e75bfedcd22397b69850f7eebd229c [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);
94 kfree(sch);
95 }
96
97}
98
99static void
100css_subchannel_release(struct device *dev)
101{
102 struct subchannel *sch;
103
104 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800105 if (!cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 kfree(sch);
107}
108
109extern int css_get_ssd_info(struct subchannel *sch);
110
Cornelia Huck6ab48792006-07-12 16:39:50 +0200111
112int css_sch_device_register(struct subchannel *sch)
113{
114 int ret;
115
116 mutex_lock(&sch->reg_mutex);
117 ret = device_register(&sch->dev);
118 mutex_unlock(&sch->reg_mutex);
119 return ret;
120}
121
122void css_sch_device_unregister(struct subchannel *sch)
123{
124 mutex_lock(&sch->reg_mutex);
125 device_unregister(&sch->dev);
126 mutex_unlock(&sch->reg_mutex);
127}
128
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129static int
130css_register_subchannel(struct subchannel *sch)
131{
132 int ret;
133
134 /* Initialize the subchannel structure */
Cornelia Hucka28c6942006-01-06 00:19:23 -0800135 sch->dev.parent = &css[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 sch->dev.bus = &css_bus_type;
137 sch->dev.release = &css_subchannel_release;
138
139 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200140 ret = css_sch_device_register(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (ret)
142 printk (KERN_WARNING "%s: could not register %s\n",
143 __func__, sch->dev.bus_id);
144 else
145 css_get_ssd_info(sch);
146 return ret;
147}
148
149int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800150css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
152 int ret;
153 struct subchannel *sch;
154
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800155 sch = css_alloc_subchannel(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 if (IS_ERR(sch))
157 return PTR_ERR(sch);
158 ret = css_register_subchannel(sch);
159 if (ret)
160 css_free_subchannel(sch);
161 return ret;
162}
163
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700164static int
165check_subchannel(struct device * dev, void * data)
166{
167 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800168 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700169
170 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800171 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700172}
173
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800175get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177 struct device *dev;
178
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700179 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200180 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700182 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200185static inline int css_get_subchannel_status(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
187 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200189 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 return CIO_GONE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200191 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return CIO_REVALIDATE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200193 if (!sch->lpm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return CIO_NO_PATH;
195 return CIO_OPER;
196}
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200197
198static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
200 int event, ret, disc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 unsigned long flags;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200202 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200204 spin_lock_irqsave(&sch->lock, flags);
205 disc = device_is_disconnected(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 if (disc && slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200207 /* Disconnected devices are evaluated directly only.*/
208 spin_unlock_irqrestore(&sch->lock, flags);
209 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200211 /* No interrupt after machine check - kill pending timers. */
212 device_kill_pending_timer(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (!disc && !slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200214 /* Non-disconnected devices are evaluated on the slow path. */
215 spin_unlock_irqrestore(&sch->lock, flags);
216 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200218 event = css_get_subchannel_status(sch);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800219 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200220 sch->schid.ssid, sch->schid.sch_no, event,
221 disc ? "disconnected" : "normal",
222 slow ? "slow" : "fast");
223 /* Analyze subchannel status. */
224 action = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 switch (event) {
226 case CIO_NO_PATH:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200227 if (disc) {
228 /* Check if paths have become available. */
229 action = REPROBE;
230 break;
231 }
232 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 case CIO_GONE:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200234 /* Prevent unwanted effects when opening lock. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 cio_disable_subchannel(sch);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200236 device_set_disconnected(sch);
237 /* Ask driver what to do with device. */
238 action = UNREGISTER;
239 if (sch->driver && sch->driver->notify) {
240 spin_unlock_irqrestore(&sch->lock, flags);
241 ret = sch->driver->notify(&sch->dev, event);
242 spin_lock_irqsave(&sch->lock, flags);
243 if (ret)
244 action = NONE;
245 }
246 break;
247 case CIO_REVALIDATE:
248 /* Device will be removed, so no notify necessary. */
249 if (disc)
250 /* Reprobe because immediate unregister might block. */
251 action = REPROBE;
252 else
253 action = UNREGISTER_PROBE;
254 break;
255 case CIO_OPER:
256 if (disc)
257 /* Get device operational again. */
258 action = REPROBE;
259 break;
260 }
261 /* Perform action. */
262 ret = 0;
263 switch (action) {
264 case UNREGISTER:
265 case UNREGISTER_PROBE:
266 /* Unregister device (will use subchannel lock). */
267 spin_unlock_irqrestore(&sch->lock, flags);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200268 css_sch_device_unregister(sch);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200269 spin_lock_irqsave(&sch->lock, flags);
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 /* Reset intparm to zeroes. */
272 sch->schib.pmcw.intparm = 0;
273 cio_modify(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 break;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200275 case REPROBE:
276 device_trigger_reprobe(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 break;
278 default:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200279 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200281 spin_unlock_irqrestore(&sch->lock, flags);
Cornelia Huckc2b14492006-10-27 12:39:17 +0200282 /* Probe if necessary. */
283 if (action == UNREGISTER_PROBE)
284 ret = css_probe_device(sch->schid);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200285
286 return ret;
287}
288
289static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
290{
291 struct schib schib;
292
293 if (!slow) {
294 /* Will be done on the slow path. */
295 return -EAGAIN;
296 }
297 if (stsch(schid, &schib) || !schib.pmcw.dnv) {
298 /* Unusable - ignore. */
299 return 0;
300 }
301 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
302 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
303
304 return css_probe_device(schid);
305}
306
307static int css_evaluate_subchannel(struct subchannel_id schid, int slow)
308{
309 struct subchannel *sch;
310 int ret;
311
312 sch = get_subchannel_by_schid(schid);
313 if (sch) {
314 ret = css_evaluate_known_subchannel(sch, slow);
315 put_device(&sch->dev);
316 } else
317 ret = css_evaluate_new_subchannel(schid, slow);
318
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 return ret;
320}
321
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800322static int
323css_rescan_devices(struct subchannel_id schid, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800325 return css_evaluate_subchannel(schid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326}
327
328struct slow_subchannel {
329 struct list_head slow_list;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800330 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331};
332
333static LIST_HEAD(slow_subchannels_head);
334static DEFINE_SPINLOCK(slow_subchannel_lock);
335
336static void
Al Viro4927b3f2006-12-06 19:18:20 +0000337css_trigger_slow_path(struct work_struct *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338{
339 CIO_TRACE_EVENT(4, "slowpath");
340
341 if (need_rescan) {
342 need_rescan = 0;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800343 for_each_subchannel(css_rescan_devices, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 return;
345 }
346
347 spin_lock_irq(&slow_subchannel_lock);
348 while (!list_empty(&slow_subchannels_head)) {
349 struct slow_subchannel *slow_sch =
350 list_entry(slow_subchannels_head.next,
351 struct slow_subchannel, slow_list);
352
353 list_del_init(slow_subchannels_head.next);
354 spin_unlock_irq(&slow_subchannel_lock);
355 css_evaluate_subchannel(slow_sch->schid, 1);
356 spin_lock_irq(&slow_subchannel_lock);
357 kfree(slow_sch);
358 }
359 spin_unlock_irq(&slow_subchannel_lock);
360}
361
Al Viro4927b3f2006-12-06 19:18:20 +0000362DECLARE_WORK(slow_path_work, css_trigger_slow_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363struct workqueue_struct *slow_path_wq;
364
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200365/* Reprobe subchannel if unregistered. */
366static int reprobe_subchannel(struct subchannel_id schid, void *data)
367{
368 struct subchannel *sch;
369 int ret;
370
371 CIO_DEBUG(KERN_INFO, 6, "cio: reprobe 0.%x.%04x\n",
372 schid.ssid, schid.sch_no);
373 if (need_reprobe)
374 return -EAGAIN;
375
376 sch = get_subchannel_by_schid(schid);
377 if (sch) {
378 /* Already known. */
379 put_device(&sch->dev);
380 return 0;
381 }
382
383 ret = css_probe_device(schid);
384 switch (ret) {
385 case 0:
386 break;
387 case -ENXIO:
388 case -ENOMEM:
389 /* These should abort looping */
390 break;
391 default:
392 ret = 0;
393 }
394
395 return ret;
396}
397
398/* Work function used to reprobe all unregistered subchannels. */
Al Viro4927b3f2006-12-06 19:18:20 +0000399static void reprobe_all(struct work_struct *unused)
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200400{
401 int ret;
402
403 CIO_MSG_EVENT(2, "reprobe start\n");
404
405 need_reprobe = 0;
406 /* Make sure initial subchannel scan is done. */
407 wait_event(ccw_device_init_wq,
408 atomic_read(&ccw_device_init_count) == 0);
409 ret = for_each_subchannel(reprobe_subchannel, NULL);
410
411 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
412 need_reprobe);
413}
414
Al Viro4927b3f2006-12-06 19:18:20 +0000415DECLARE_WORK(css_reprobe_work, reprobe_all);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200416
417/* Schedule reprobing of all unregistered subchannels. */
418void css_schedule_reprobe(void)
419{
420 need_reprobe = 1;
421 queue_work(ccw_device_work, &css_reprobe_work);
422}
423
424EXPORT_SYMBOL_GPL(css_schedule_reprobe);
425
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426/*
427 * Rescan for new devices. FIXME: This is slow.
428 * This function is called when we have lost CRWs due to overflows and we have
429 * to do subchannel housekeeping.
430 */
431void
432css_reiterate_subchannels(void)
433{
434 css_clear_subchannel_slow_list();
435 need_rescan = 1;
436}
437
438/*
439 * Called from the machine check handler for subchannel report words.
440 */
441int
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800442css_process_crw(int rsid1, int rsid2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443{
444 int ret;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800445 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800447 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
448 rsid1, rsid2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
450 if (need_rescan)
451 /* We need to iterate all subchannels anyway. */
452 return -EAGAIN;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800453
454 init_subchannel_id(&mchk_schid);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800455 mchk_schid.sch_no = rsid1;
456 if (rsid2 != 0)
457 mchk_schid.ssid = (rsid2 >> 8) & 3;
458
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 /*
460 * Since we are always presented with IPI in the CRW, we have to
461 * use stsch() to find out if the subchannel in question has come
462 * or gone.
463 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800464 ret = css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (ret == -EAGAIN) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800466 if (css_enqueue_subchannel_slow(mchk_schid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 css_clear_subchannel_slow_list();
468 need_rescan = 1;
469 }
470 }
471 return ret;
472}
473
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800474static int __init
475__init_channel_subsystem(struct subchannel_id schid, void *data)
476{
477 struct subchannel *sch;
478 int ret;
479
480 if (cio_is_console(schid))
481 sch = cio_get_console_subchannel();
482 else {
483 sch = css_alloc_subchannel(schid);
484 if (IS_ERR(sch))
485 ret = PTR_ERR(sch);
486 else
487 ret = 0;
488 switch (ret) {
489 case 0:
490 break;
491 case -ENOMEM:
492 panic("Out of memory in init_channel_subsystem\n");
493 /* -ENXIO: no more subchannels. */
494 case -ENXIO:
495 return ret;
Greg Smithe843e282006-03-14 19:50:17 -0800496 /* -EIO: this subchannel set not supported. */
497 case -EIO:
498 return ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800499 default:
500 return 0;
501 }
502 }
503 /*
504 * We register ALL valid subchannels in ioinfo, even those
505 * that have been present before init_channel_subsystem.
506 * These subchannels can't have been registered yet (kmalloc
507 * not working) so we do it now. This is true e.g. for the
508 * console subchannel.
509 */
510 css_register_subchannel(sch);
511 return 0;
512}
513
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800515css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800517 if (css_characteristics_avail && css_general_characteristics.mcss) {
518 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
519 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
520 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521#ifdef CONFIG_SMP
Cornelia Hucka28c6942006-01-06 00:19:23 -0800522 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800524 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525#endif
526 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800527 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
528 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
529 css->global_pgid.tod_high = tod_high;
530
531}
532
Cornelia Huck3b793062006-01-06 00:19:26 -0800533static void
534channel_subsystem_release(struct device *dev)
535{
536 struct channel_subsystem *css;
537
538 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800539 mutex_destroy(&css->mutex);
Cornelia Huck3b793062006-01-06 00:19:26 -0800540 kfree(css);
541}
542
Cornelia Huck495a5b42006-03-24 03:15:14 -0800543static ssize_t
544css_cm_enable_show(struct device *dev, struct device_attribute *attr,
545 char *buf)
546{
547 struct channel_subsystem *css = to_css(dev);
548
549 if (!css)
550 return 0;
551 return sprintf(buf, "%x\n", css->cm_enabled);
552}
553
554static ssize_t
555css_cm_enable_store(struct device *dev, struct device_attribute *attr,
556 const char *buf, size_t count)
557{
558 struct channel_subsystem *css = to_css(dev);
559 int ret;
560
561 switch (buf[0]) {
562 case '0':
563 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
564 break;
565 case '1':
566 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
567 break;
568 default:
569 ret = -EINVAL;
570 }
571 return ret < 0 ? ret : count;
572}
573
574static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
575
Cornelia Hucka28c6942006-01-06 00:19:23 -0800576static inline void __init
577setup_css(int nr)
578{
579 u32 tod_high;
580
581 memset(css[nr], 0, sizeof(struct channel_subsystem));
Cornelia Huck495a5b42006-03-24 03:15:14 -0800582 mutex_init(&css[nr]->mutex);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800583 css[nr]->valid = 1;
584 css[nr]->cssid = nr;
585 sprintf(css[nr]->device.bus_id, "css%x", nr);
Cornelia Huck3b793062006-01-06 00:19:26 -0800586 css[nr]->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800587 tod_high = (u32) (get_clock() >> 32);
588 css_generate_pgid(css[nr], tod_high);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589}
590
591/*
592 * Now that the driver core is running, we can setup our channel subsystem.
593 * The struct subchannel's are created during probing (except for the
594 * static console subchannel).
595 */
596static int __init
597init_channel_subsystem (void)
598{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800599 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 if (chsc_determine_css_characteristics() == 0)
602 css_characteristics_avail = 1;
603
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 if ((ret = bus_register(&css_bus_type)))
605 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800607 /* Try to enable MSS. */
608 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
609 switch (ret) {
610 case 0: /* Success. */
611 max_ssid = __MAX_SSID;
612 break;
613 case -ENOMEM:
614 goto out_bus;
615 default:
616 max_ssid = 0;
617 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800618 /* Setup css structure. */
619 for (i = 0; i <= __MAX_CSSID; i++) {
620 css[i] = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
621 if (!css[i]) {
622 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800623 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800624 }
625 setup_css(i);
626 ret = device_register(&css[i]->device);
627 if (ret)
628 goto out_free;
Cornelia Huck7e560812006-07-12 16:40:19 +0200629 if (css_characteristics_avail &&
630 css_chsc_characteristics.secm) {
631 ret = device_create_file(&css[i]->device,
632 &dev_attr_cm_enable);
633 if (ret)
634 goto out_device;
635 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 css_init_done = 1;
638
639 ctl_set_bit(6, 28);
640
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800641 for_each_subchannel(__init_channel_subsystem, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 return 0;
Cornelia Huck7e560812006-07-12 16:40:19 +0200643out_device:
644 device_unregister(&css[i]->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800645out_free:
646 kfree(css[i]);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800647out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800648 while (i > 0) {
649 i--;
Cornelia Huck495a5b42006-03-24 03:15:14 -0800650 if (css_characteristics_avail && css_chsc_characteristics.secm)
651 device_remove_file(&css[i]->device,
652 &dev_attr_cm_enable);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800653 device_unregister(&css[i]->device);
654 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800655out_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 bus_unregister(&css_bus_type);
657out:
658 return ret;
659}
660
661/*
662 * find a driver for a subchannel. They identify by the subchannel
663 * type with the exception that the console subchannel driver has its own
664 * subchannel type although the device is an i/o subchannel
665 */
666static int
667css_bus_match (struct device *dev, struct device_driver *drv)
668{
669 struct subchannel *sch = container_of (dev, struct subchannel, dev);
670 struct css_driver *driver = container_of (drv, struct css_driver, drv);
671
672 if (sch->st == driver->subchannel_type)
673 return 1;
674
675 return 0;
676}
677
Cornelia Huck8bbace72006-01-11 10:56:22 +0100678static int
679css_probe (struct device *dev)
680{
681 struct subchannel *sch;
682
683 sch = to_subchannel(dev);
684 sch->driver = container_of (dev->driver, struct css_driver, drv);
685 return (sch->driver->probe ? sch->driver->probe(sch) : 0);
686}
687
688static int
689css_remove (struct device *dev)
690{
691 struct subchannel *sch;
692
693 sch = to_subchannel(dev);
694 return (sch->driver->remove ? sch->driver->remove(sch) : 0);
695}
696
697static void
698css_shutdown (struct device *dev)
699{
700 struct subchannel *sch;
701
702 sch = to_subchannel(dev);
703 if (sch->driver->shutdown)
704 sch->driver->shutdown(sch);
705}
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +0100708 .name = "css",
709 .match = css_bus_match,
710 .probe = css_probe,
711 .remove = css_remove,
712 .shutdown = css_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713};
714
715subsys_initcall(init_channel_subsystem);
716
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800718css_enqueue_subchannel_slow(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719{
720 struct slow_subchannel *new_slow_sch;
721 unsigned long flags;
722
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800723 new_slow_sch = kzalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (!new_slow_sch)
725 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 new_slow_sch->schid = schid;
727 spin_lock_irqsave(&slow_subchannel_lock, flags);
728 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
729 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
730 return 0;
731}
732
733void
734css_clear_subchannel_slow_list(void)
735{
736 unsigned long flags;
737
738 spin_lock_irqsave(&slow_subchannel_lock, flags);
739 while (!list_empty(&slow_subchannels_head)) {
740 struct slow_subchannel *slow_sch =
741 list_entry(slow_subchannels_head.next,
742 struct slow_subchannel, slow_list);
743
744 list_del_init(slow_subchannels_head.next);
745 kfree(slow_sch);
746 }
747 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
748}
749
750
751
752int
753css_slow_subchannels_exist(void)
754{
755 return (!list_empty(&slow_subchannels_head));
756}
757
758MODULE_LICENSE("GPL");
759EXPORT_SYMBOL(css_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760EXPORT_SYMBOL_GPL(css_characteristics_avail);