blob: 0bf7166193782241ae67d48cf991e747947583e9 [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 Huck7674da72006-12-08 15:54:21 +0100140
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 }
148 css_get_ssd_info(sch);
149 ret = subchannel_add_files(&sch->dev);
150 if (ret)
151 printk(KERN_WARNING "%s: could not add attributes to %s\n",
152 __func__, sch->dev.bus_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 return ret;
154}
155
156int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800157css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 int ret;
160 struct subchannel *sch;
161
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800162 sch = css_alloc_subchannel(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 if (IS_ERR(sch))
164 return PTR_ERR(sch);
165 ret = css_register_subchannel(sch);
166 if (ret)
167 css_free_subchannel(sch);
168 return ret;
169}
170
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700171static int
172check_subchannel(struct device * dev, void * data)
173{
174 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800175 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700176
177 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800178 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800182get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 struct device *dev;
185
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700186 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200187 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700189 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190}
191
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200192static inline int css_get_subchannel_status(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200196 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return CIO_GONE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200198 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 return CIO_REVALIDATE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200200 if (!sch->lpm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 return CIO_NO_PATH;
202 return CIO_OPER;
203}
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200204
205static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 int event, ret, disc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 unsigned long flags;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200209 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
Cornelia Huck2ec22982006-12-08 15:54:26 +0100211 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200212 disc = device_is_disconnected(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (disc && slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200214 /* Disconnected devices are evaluated directly only.*/
Cornelia Huck2ec22982006-12-08 15:54:26 +0100215 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200216 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200218 /* No interrupt after machine check - kill pending timers. */
219 device_kill_pending_timer(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 if (!disc && !slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200221 /* Non-disconnected devices are evaluated on the slow path. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100222 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200223 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200225 event = css_get_subchannel_status(sch);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800226 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200227 sch->schid.ssid, sch->schid.sch_no, event,
228 disc ? "disconnected" : "normal",
229 slow ? "slow" : "fast");
230 /* Analyze subchannel status. */
231 action = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 switch (event) {
233 case CIO_NO_PATH:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200234 if (disc) {
235 /* Check if paths have become available. */
236 action = REPROBE;
237 break;
238 }
239 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 case CIO_GONE:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200241 /* Prevent unwanted effects when opening lock. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 cio_disable_subchannel(sch);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200243 device_set_disconnected(sch);
244 /* Ask driver what to do with device. */
245 action = UNREGISTER;
246 if (sch->driver && sch->driver->notify) {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100247 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200248 ret = sch->driver->notify(&sch->dev, event);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100249 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200250 if (ret)
251 action = NONE;
252 }
253 break;
254 case CIO_REVALIDATE:
255 /* Device will be removed, so no notify necessary. */
256 if (disc)
257 /* Reprobe because immediate unregister might block. */
258 action = REPROBE;
259 else
260 action = UNREGISTER_PROBE;
261 break;
262 case CIO_OPER:
263 if (disc)
264 /* Get device operational again. */
265 action = REPROBE;
266 break;
267 }
268 /* Perform action. */
269 ret = 0;
270 switch (action) {
271 case UNREGISTER:
272 case UNREGISTER_PROBE:
273 /* Unregister device (will use subchannel lock). */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100274 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200275 css_sch_device_unregister(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100276 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200277
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 /* Reset intparm to zeroes. */
279 sch->schib.pmcw.intparm = 0;
280 cio_modify(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 break;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200282 case REPROBE:
283 device_trigger_reprobe(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284 break;
285 default:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200286 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 }
Cornelia Huck2ec22982006-12-08 15:54:26 +0100288 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huckc2b14492006-10-27 12:39:17 +0200289 /* Probe if necessary. */
290 if (action == UNREGISTER_PROBE)
291 ret = css_probe_device(sch->schid);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200292
293 return ret;
294}
295
296static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
297{
298 struct schib schib;
299
300 if (!slow) {
301 /* Will be done on the slow path. */
302 return -EAGAIN;
303 }
304 if (stsch(schid, &schib) || !schib.pmcw.dnv) {
305 /* Unusable - ignore. */
306 return 0;
307 }
308 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
309 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
310
311 return css_probe_device(schid);
312}
313
314static int css_evaluate_subchannel(struct subchannel_id schid, int slow)
315{
316 struct subchannel *sch;
317 int ret;
318
319 sch = get_subchannel_by_schid(schid);
320 if (sch) {
321 ret = css_evaluate_known_subchannel(sch, slow);
322 put_device(&sch->dev);
323 } else
324 ret = css_evaluate_new_subchannel(schid, slow);
325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 return ret;
327}
328
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800329static int
330css_rescan_devices(struct subchannel_id schid, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800332 return css_evaluate_subchannel(schid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333}
334
335struct slow_subchannel {
336 struct list_head slow_list;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800337 struct subchannel_id schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338};
339
340static LIST_HEAD(slow_subchannels_head);
341static DEFINE_SPINLOCK(slow_subchannel_lock);
342
343static void
Al Viro4927b3f2006-12-06 19:18:20 +0000344css_trigger_slow_path(struct work_struct *unused)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345{
346 CIO_TRACE_EVENT(4, "slowpath");
347
348 if (need_rescan) {
349 need_rescan = 0;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800350 for_each_subchannel(css_rescan_devices, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 return;
352 }
353
354 spin_lock_irq(&slow_subchannel_lock);
355 while (!list_empty(&slow_subchannels_head)) {
356 struct slow_subchannel *slow_sch =
357 list_entry(slow_subchannels_head.next,
358 struct slow_subchannel, slow_list);
359
360 list_del_init(slow_subchannels_head.next);
361 spin_unlock_irq(&slow_subchannel_lock);
362 css_evaluate_subchannel(slow_sch->schid, 1);
363 spin_lock_irq(&slow_subchannel_lock);
364 kfree(slow_sch);
365 }
366 spin_unlock_irq(&slow_subchannel_lock);
367}
368
Al Viro4927b3f2006-12-06 19:18:20 +0000369DECLARE_WORK(slow_path_work, css_trigger_slow_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370struct workqueue_struct *slow_path_wq;
371
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200372/* Reprobe subchannel if unregistered. */
373static int reprobe_subchannel(struct subchannel_id schid, void *data)
374{
375 struct subchannel *sch;
376 int ret;
377
378 CIO_DEBUG(KERN_INFO, 6, "cio: reprobe 0.%x.%04x\n",
379 schid.ssid, schid.sch_no);
380 if (need_reprobe)
381 return -EAGAIN;
382
383 sch = get_subchannel_by_schid(schid);
384 if (sch) {
385 /* Already known. */
386 put_device(&sch->dev);
387 return 0;
388 }
389
390 ret = css_probe_device(schid);
391 switch (ret) {
392 case 0:
393 break;
394 case -ENXIO:
395 case -ENOMEM:
396 /* These should abort looping */
397 break;
398 default:
399 ret = 0;
400 }
401
402 return ret;
403}
404
405/* Work function used to reprobe all unregistered subchannels. */
Al Viro4927b3f2006-12-06 19:18:20 +0000406static void reprobe_all(struct work_struct *unused)
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200407{
408 int ret;
409
410 CIO_MSG_EVENT(2, "reprobe start\n");
411
412 need_reprobe = 0;
413 /* Make sure initial subchannel scan is done. */
414 wait_event(ccw_device_init_wq,
415 atomic_read(&ccw_device_init_count) == 0);
416 ret = for_each_subchannel(reprobe_subchannel, NULL);
417
418 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
419 need_reprobe);
420}
421
Al Viro4927b3f2006-12-06 19:18:20 +0000422DECLARE_WORK(css_reprobe_work, reprobe_all);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200423
424/* Schedule reprobing of all unregistered subchannels. */
425void css_schedule_reprobe(void)
426{
427 need_reprobe = 1;
428 queue_work(ccw_device_work, &css_reprobe_work);
429}
430
431EXPORT_SYMBOL_GPL(css_schedule_reprobe);
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433/*
434 * Rescan for new devices. FIXME: This is slow.
435 * This function is called when we have lost CRWs due to overflows and we have
436 * to do subchannel housekeeping.
437 */
438void
439css_reiterate_subchannels(void)
440{
441 css_clear_subchannel_slow_list();
442 need_rescan = 1;
443}
444
445/*
446 * Called from the machine check handler for subchannel report words.
447 */
448int
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800449css_process_crw(int rsid1, int rsid2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450{
451 int ret;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800452 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800454 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
455 rsid1, rsid2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
457 if (need_rescan)
458 /* We need to iterate all subchannels anyway. */
459 return -EAGAIN;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800460
461 init_subchannel_id(&mchk_schid);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800462 mchk_schid.sch_no = rsid1;
463 if (rsid2 != 0)
464 mchk_schid.ssid = (rsid2 >> 8) & 3;
465
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 /*
467 * Since we are always presented with IPI in the CRW, we have to
468 * use stsch() to find out if the subchannel in question has come
469 * or gone.
470 */
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800471 ret = css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 if (ret == -EAGAIN) {
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800473 if (css_enqueue_subchannel_slow(mchk_schid)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 css_clear_subchannel_slow_list();
475 need_rescan = 1;
476 }
477 }
478 return ret;
479}
480
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800481static int __init
482__init_channel_subsystem(struct subchannel_id schid, void *data)
483{
484 struct subchannel *sch;
485 int ret;
486
487 if (cio_is_console(schid))
488 sch = cio_get_console_subchannel();
489 else {
490 sch = css_alloc_subchannel(schid);
491 if (IS_ERR(sch))
492 ret = PTR_ERR(sch);
493 else
494 ret = 0;
495 switch (ret) {
496 case 0:
497 break;
498 case -ENOMEM:
499 panic("Out of memory in init_channel_subsystem\n");
500 /* -ENXIO: no more subchannels. */
501 case -ENXIO:
502 return ret;
Greg Smithe843e282006-03-14 19:50:17 -0800503 /* -EIO: this subchannel set not supported. */
504 case -EIO:
505 return ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800506 default:
507 return 0;
508 }
509 }
510 /*
511 * We register ALL valid subchannels in ioinfo, even those
512 * that have been present before init_channel_subsystem.
513 * These subchannels can't have been registered yet (kmalloc
514 * not working) so we do it now. This is true e.g. for the
515 * console subchannel.
516 */
517 css_register_subchannel(sch);
518 return 0;
519}
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800522css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800524 if (css_characteristics_avail && css_general_characteristics.mcss) {
525 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
526 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
527 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528#ifdef CONFIG_SMP
Cornelia Hucka28c6942006-01-06 00:19:23 -0800529 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800531 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532#endif
533 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800534 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
535 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
536 css->global_pgid.tod_high = tod_high;
537
538}
539
Cornelia Huck3b793062006-01-06 00:19:26 -0800540static void
541channel_subsystem_release(struct device *dev)
542{
543 struct channel_subsystem *css;
544
545 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800546 mutex_destroy(&css->mutex);
Cornelia Huck3b793062006-01-06 00:19:26 -0800547 kfree(css);
548}
549
Cornelia Huck495a5b42006-03-24 03:15:14 -0800550static ssize_t
551css_cm_enable_show(struct device *dev, struct device_attribute *attr,
552 char *buf)
553{
554 struct channel_subsystem *css = to_css(dev);
555
556 if (!css)
557 return 0;
558 return sprintf(buf, "%x\n", css->cm_enabled);
559}
560
561static ssize_t
562css_cm_enable_store(struct device *dev, struct device_attribute *attr,
563 const char *buf, size_t count)
564{
565 struct channel_subsystem *css = to_css(dev);
566 int ret;
567
568 switch (buf[0]) {
569 case '0':
570 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
571 break;
572 case '1':
573 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
574 break;
575 default:
576 ret = -EINVAL;
577 }
578 return ret < 0 ? ret : count;
579}
580
581static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
582
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100583static inline int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800584{
585 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100586 int ret;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800587
588 memset(css[nr], 0, sizeof(struct channel_subsystem));
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100589 css[nr]->pseudo_subchannel =
590 kzalloc(sizeof(*css[nr]->pseudo_subchannel), GFP_KERNEL);
591 if (!css[nr]->pseudo_subchannel)
592 return -ENOMEM;
593 css[nr]->pseudo_subchannel->dev.parent = &css[nr]->device;
594 css[nr]->pseudo_subchannel->dev.release = css_subchannel_release;
595 sprintf(css[nr]->pseudo_subchannel->dev.bus_id, "defunct");
596 ret = cio_create_sch_lock(css[nr]->pseudo_subchannel);
597 if (ret) {
598 kfree(css[nr]->pseudo_subchannel);
599 return ret;
600 }
Cornelia Huck495a5b42006-03-24 03:15:14 -0800601 mutex_init(&css[nr]->mutex);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800602 css[nr]->valid = 1;
603 css[nr]->cssid = nr;
604 sprintf(css[nr]->device.bus_id, "css%x", nr);
Cornelia Huck3b793062006-01-06 00:19:26 -0800605 css[nr]->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800606 tod_high = (u32) (get_clock() >> 32);
607 css_generate_pgid(css[nr], tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100608 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611/*
612 * Now that the driver core is running, we can setup our channel subsystem.
613 * The struct subchannel's are created during probing (except for the
614 * static console subchannel).
615 */
616static int __init
617init_channel_subsystem (void)
618{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800619 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
621 if (chsc_determine_css_characteristics() == 0)
622 css_characteristics_avail = 1;
623
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if ((ret = bus_register(&css_bus_type)))
625 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800627 /* Try to enable MSS. */
628 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
629 switch (ret) {
630 case 0: /* Success. */
631 max_ssid = __MAX_SSID;
632 break;
633 case -ENOMEM:
634 goto out_bus;
635 default:
636 max_ssid = 0;
637 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800638 /* Setup css structure. */
639 for (i = 0; i <= __MAX_CSSID; i++) {
640 css[i] = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
641 if (!css[i]) {
642 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800643 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800644 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100645 ret = setup_css(i);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800646 if (ret)
647 goto out_free;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100648 ret = device_register(&css[i]->device);
649 if (ret)
650 goto out_free_all;
Cornelia Huck7e560812006-07-12 16:40:19 +0200651 if (css_characteristics_avail &&
652 css_chsc_characteristics.secm) {
653 ret = device_create_file(&css[i]->device,
654 &dev_attr_cm_enable);
655 if (ret)
656 goto out_device;
657 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100658 ret = device_register(&css[i]->pseudo_subchannel->dev);
659 if (ret)
660 goto out_file;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 css_init_done = 1;
663
664 ctl_set_bit(6, 28);
665
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800666 for_each_subchannel(__init_channel_subsystem, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 return 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100668out_file:
669 device_remove_file(&css[i]->device, &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200670out_device:
671 device_unregister(&css[i]->device);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100672out_free_all:
673 kfree(css[i]->pseudo_subchannel->lock);
674 kfree(css[i]->pseudo_subchannel);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800675out_free:
676 kfree(css[i]);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800677out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800678 while (i > 0) {
679 i--;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100680 device_unregister(&css[i]->pseudo_subchannel->dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800681 if (css_characteristics_avail && css_chsc_characteristics.secm)
682 device_remove_file(&css[i]->device,
683 &dev_attr_cm_enable);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800684 device_unregister(&css[i]->device);
685 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800686out_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 bus_unregister(&css_bus_type);
688out:
689 return ret;
690}
691
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100692int sch_is_pseudo_sch(struct subchannel *sch)
693{
694 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
695}
696
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697/*
698 * find a driver for a subchannel. They identify by the subchannel
699 * type with the exception that the console subchannel driver has its own
700 * subchannel type although the device is an i/o subchannel
701 */
702static int
703css_bus_match (struct device *dev, struct device_driver *drv)
704{
705 struct subchannel *sch = container_of (dev, struct subchannel, dev);
706 struct css_driver *driver = container_of (drv, struct css_driver, drv);
707
708 if (sch->st == driver->subchannel_type)
709 return 1;
710
711 return 0;
712}
713
Cornelia Huck8bbace72006-01-11 10:56:22 +0100714static int
715css_probe (struct device *dev)
716{
717 struct subchannel *sch;
718
719 sch = to_subchannel(dev);
720 sch->driver = container_of (dev->driver, struct css_driver, drv);
721 return (sch->driver->probe ? sch->driver->probe(sch) : 0);
722}
723
724static int
725css_remove (struct device *dev)
726{
727 struct subchannel *sch;
728
729 sch = to_subchannel(dev);
730 return (sch->driver->remove ? sch->driver->remove(sch) : 0);
731}
732
733static void
734css_shutdown (struct device *dev)
735{
736 struct subchannel *sch;
737
738 sch = to_subchannel(dev);
739 if (sch->driver->shutdown)
740 sch->driver->shutdown(sch);
741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +0100744 .name = "css",
745 .match = css_bus_match,
746 .probe = css_probe,
747 .remove = css_remove,
748 .shutdown = css_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749};
750
751subsys_initcall(init_channel_subsystem);
752
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753int
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800754css_enqueue_subchannel_slow(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755{
756 struct slow_subchannel *new_slow_sch;
757 unsigned long flags;
758
Eric Sesterhenn88abaab2006-03-24 03:15:31 -0800759 new_slow_sch = kzalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 if (!new_slow_sch)
761 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 new_slow_sch->schid = schid;
763 spin_lock_irqsave(&slow_subchannel_lock, flags);
764 list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
765 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
766 return 0;
767}
768
769void
770css_clear_subchannel_slow_list(void)
771{
772 unsigned long flags;
773
774 spin_lock_irqsave(&slow_subchannel_lock, flags);
775 while (!list_empty(&slow_subchannels_head)) {
776 struct slow_subchannel *slow_sch =
777 list_entry(slow_subchannels_head.next,
778 struct slow_subchannel, slow_list);
779
780 list_del_init(slow_subchannels_head.next);
781 kfree(slow_sch);
782 }
783 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
784}
785
786
787
788int
789css_slow_subchannels_exist(void)
790{
791 return (!list_empty(&slow_subchannels_head));
792}
793
794MODULE_LICENSE("GPL");
795EXPORT_SYMBOL(css_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796EXPORT_SYMBOL_GPL(css_characteristics_avail);