blob: 1c27a5a06b4982d6f754484f52ad4187b8f9162d [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"
Peter Oberparleiter83b33702007-04-27 16:01:34 +020023#include "idset.h"
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +020024#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Linus Torvalds1da177e2005-04-16 15:20:36 -070026int css_init_done = 0;
Peter Oberparleiter40154b82006-06-29 14:57:03 +020027static int need_reprobe = 0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -080028static int max_ssid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Cornelia Hucka28c6942006-01-06 00:19:23 -080030struct channel_subsystem *css[__MAX_CSSID + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070031
Cornelia Hucka28c6942006-01-06 00:19:23 -080032int css_characteristics_avail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Heiko Carstens4d284ca2007-02-05 21:18:53 +010034int
Cornelia Huckf97a56f2006-01-06 00:19:22 -080035for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
36{
37 struct subchannel_id schid;
38 int ret;
39
40 init_subchannel_id(&schid);
41 ret = -ENODEV;
42 do {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080043 do {
44 ret = fn(schid, data);
45 if (ret)
46 break;
47 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
48 schid.sch_no = 0;
49 } while (schid.ssid++ < max_ssid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080050 return ret;
51}
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053static struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -080054css_alloc_subchannel(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
56 struct subchannel *sch;
57 int ret;
58
59 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
60 if (sch == NULL)
61 return ERR_PTR(-ENOMEM);
Cornelia Hucka8237fc2006-01-06 00:19:21 -080062 ret = cio_validate_subchannel (sch, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 if (ret < 0) {
64 kfree(sch);
65 return ERR_PTR(ret);
66 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
68 if (sch->st != SUBCHANNEL_TYPE_IO) {
69 /* For now we ignore all non-io subchannels. */
70 kfree(sch);
71 return ERR_PTR(-EINVAL);
72 }
73
74 /*
75 * Set intparm to subchannel address.
76 * This is fine even on 64bit since the subchannel is always located
77 * under 2G.
78 */
79 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
80 ret = cio_modify(sch);
81 if (ret) {
82 kfree(sch);
83 return ERR_PTR(ret);
84 }
85 return sch;
86}
87
88static void
89css_free_subchannel(struct subchannel *sch)
90{
91 if (sch) {
92 /* Reset intparm to zeroes. */
93 sch->schib.pmcw.intparm = 0;
94 cio_modify(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +010095 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 kfree(sch);
97 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070098}
99
100static void
101css_subchannel_release(struct device *dev)
102{
103 struct subchannel *sch;
104
105 sch = to_subchannel(dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100106 if (!cio_is_console(sch->schid)) {
107 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 kfree(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110}
111
Cornelia Huck07c6a332007-07-27 12:29:10 +0200112static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200113{
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
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200129static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
130{
131 int i;
132 int mask;
133
134 memset(ssd, 0, sizeof(struct chsc_ssd_info));
135 ssd->path_mask = pmcw->pim;
136 for (i = 0; i < 8; i++) {
137 mask = 0x80 >> i;
138 if (pmcw->pim & mask) {
139 chp_id_init(&ssd->chpid[i]);
140 ssd->chpid[i].id = pmcw->chpid[i];
141 }
142 }
143}
144
145static void ssd_register_chpids(struct chsc_ssd_info *ssd)
146{
147 int i;
148 int mask;
149
150 for (i = 0; i < 8; i++) {
151 mask = 0x80 >> i;
152 if (ssd->path_mask & mask)
153 if (!chp_is_registered(ssd->chpid[i]))
154 chp_new(ssd->chpid[i]);
155 }
156}
157
158void css_update_ssd_info(struct subchannel *sch)
159{
160 int ret;
161
162 if (cio_is_console(sch->schid)) {
163 /* Console is initialized too early for functions requiring
164 * memory allocation. */
165 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
166 } else {
167 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
168 if (ret)
169 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
170 ssd_register_chpids(&sch->ssd_info);
171 }
172}
173
174static int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175{
176 int ret;
177
178 /* Initialize the subchannel structure */
Cornelia Hucka28c6942006-01-06 00:19:23 -0800179 sch->dev.parent = &css[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 sch->dev.bus = &css_bus_type;
181 sch->dev.release = &css_subchannel_release;
Cornelia Huck529192f2006-12-08 15:55:57 +0100182 sch->dev.groups = subch_attr_groups;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200183 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200185 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100186 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200187 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
188 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100189 return ret;
190 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 return ret;
192}
193
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200194static int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 int ret;
197 struct subchannel *sch;
198
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800199 sch = css_alloc_subchannel(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (IS_ERR(sch))
201 return PTR_ERR(sch);
202 ret = css_register_subchannel(sch);
203 if (ret)
204 css_free_subchannel(sch);
205 return ret;
206}
207
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700208static int
209check_subchannel(struct device * dev, void * data)
210{
211 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800212 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700213
214 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800215 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800219get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 struct device *dev;
222
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700223 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200224 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700226 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100229static int css_get_subchannel_status(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
231 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200233 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return CIO_GONE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200235 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return CIO_REVALIDATE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200237 if (!sch->lpm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return CIO_NO_PATH;
239 return CIO_OPER;
240}
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200241
242static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 int event, ret, disc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned long flags;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200246 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Cornelia Huck2ec22982006-12-08 15:54:26 +0100248 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200249 disc = device_is_disconnected(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 if (disc && slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200251 /* Disconnected devices are evaluated directly only.*/
Cornelia Huck2ec22982006-12-08 15:54:26 +0100252 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200253 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200255 /* No interrupt after machine check - kill pending timers. */
256 device_kill_pending_timer(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 if (!disc && !slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200258 /* Non-disconnected devices are evaluated on the slow path. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100259 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200260 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200262 event = css_get_subchannel_status(sch);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800263 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200264 sch->schid.ssid, sch->schid.sch_no, event,
265 disc ? "disconnected" : "normal",
266 slow ? "slow" : "fast");
267 /* Analyze subchannel status. */
268 action = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 switch (event) {
270 case CIO_NO_PATH:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200271 if (disc) {
272 /* Check if paths have become available. */
273 action = REPROBE;
274 break;
275 }
276 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 case CIO_GONE:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200278 /* Prevent unwanted effects when opening lock. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279 cio_disable_subchannel(sch);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200280 device_set_disconnected(sch);
281 /* Ask driver what to do with device. */
282 action = UNREGISTER;
283 if (sch->driver && sch->driver->notify) {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100284 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200285 ret = sch->driver->notify(&sch->dev, event);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100286 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200287 if (ret)
288 action = NONE;
289 }
290 break;
291 case CIO_REVALIDATE:
292 /* Device will be removed, so no notify necessary. */
293 if (disc)
294 /* Reprobe because immediate unregister might block. */
295 action = REPROBE;
296 else
297 action = UNREGISTER_PROBE;
298 break;
299 case CIO_OPER:
300 if (disc)
301 /* Get device operational again. */
302 action = REPROBE;
303 break;
304 }
305 /* Perform action. */
306 ret = 0;
307 switch (action) {
308 case UNREGISTER:
309 case UNREGISTER_PROBE:
310 /* Unregister device (will use subchannel lock). */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100311 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200312 css_sch_device_unregister(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100313 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* Reset intparm to zeroes. */
316 sch->schib.pmcw.intparm = 0;
317 cio_modify(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 break;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200319 case REPROBE:
320 device_trigger_reprobe(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 break;
322 default:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200323 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Cornelia Huck2ec22982006-12-08 15:54:26 +0100325 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huckc2b14492006-10-27 12:39:17 +0200326 /* Probe if necessary. */
327 if (action == UNREGISTER_PROBE)
328 ret = css_probe_device(sch->schid);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200329
330 return ret;
331}
332
333static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
334{
335 struct schib schib;
336
337 if (!slow) {
338 /* Will be done on the slow path. */
339 return -EAGAIN;
340 }
Cornelia Huck758976f2007-02-05 21:17:36 +0100341 if (stsch_err(schid, &schib) || !schib.pmcw.dnv) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200342 /* Unusable - ignore. */
343 return 0;
344 }
345 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
346 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
347
348 return css_probe_device(schid);
349}
350
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200351static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200352{
353 struct subchannel *sch;
354 int ret;
355
356 sch = get_subchannel_by_schid(schid);
357 if (sch) {
358 ret = css_evaluate_known_subchannel(sch, slow);
359 put_device(&sch->dev);
360 } else
361 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200362 if (ret == -EAGAIN)
363 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364}
365
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200366static struct idset *slow_subchannel_set;
367static spinlock_t slow_subchannel_lock;
368
369static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200371 spin_lock_init(&slow_subchannel_lock);
372 slow_subchannel_set = idset_sch_new();
373 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200374 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200375 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200377 return 0;
378}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200380static void css_slow_path_func(struct work_struct *unused)
381{
382 struct subchannel_id schid;
383
384 CIO_TRACE_EVENT(4, "slowpath");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 spin_lock_irq(&slow_subchannel_lock);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200386 init_subchannel_id(&schid);
387 while (idset_sch_get_first(slow_subchannel_set, &schid)) {
388 idset_sch_del(slow_subchannel_set, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 spin_unlock_irq(&slow_subchannel_lock);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200390 css_evaluate_subchannel(schid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 spin_lock_irq(&slow_subchannel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 }
393 spin_unlock_irq(&slow_subchannel_lock);
394}
395
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200396static DECLARE_WORK(slow_path_work, css_slow_path_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397struct workqueue_struct *slow_path_wq;
398
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200399void css_schedule_eval(struct subchannel_id schid)
400{
401 unsigned long flags;
402
403 spin_lock_irqsave(&slow_subchannel_lock, flags);
404 idset_sch_add(slow_subchannel_set, schid);
405 queue_work(slow_path_wq, &slow_path_work);
406 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
407}
408
409void css_schedule_eval_all(void)
410{
411 unsigned long flags;
412
413 spin_lock_irqsave(&slow_subchannel_lock, flags);
414 idset_fill(slow_subchannel_set);
415 queue_work(slow_path_wq, &slow_path_work);
416 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
417}
418
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200419/* Reprobe subchannel if unregistered. */
420static int reprobe_subchannel(struct subchannel_id schid, void *data)
421{
422 struct subchannel *sch;
423 int ret;
424
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200425 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
426 schid.ssid, schid.sch_no);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200427 if (need_reprobe)
428 return -EAGAIN;
429
430 sch = get_subchannel_by_schid(schid);
431 if (sch) {
432 /* Already known. */
433 put_device(&sch->dev);
434 return 0;
435 }
436
437 ret = css_probe_device(schid);
438 switch (ret) {
439 case 0:
440 break;
441 case -ENXIO:
442 case -ENOMEM:
443 /* These should abort looping */
444 break;
445 default:
446 ret = 0;
447 }
448
449 return ret;
450}
451
452/* Work function used to reprobe all unregistered subchannels. */
Al Viro4927b3f2006-12-06 19:18:20 +0000453static void reprobe_all(struct work_struct *unused)
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200454{
455 int ret;
456
457 CIO_MSG_EVENT(2, "reprobe start\n");
458
459 need_reprobe = 0;
460 /* Make sure initial subchannel scan is done. */
461 wait_event(ccw_device_init_wq,
462 atomic_read(&ccw_device_init_count) == 0);
463 ret = for_each_subchannel(reprobe_subchannel, NULL);
464
465 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
466 need_reprobe);
467}
468
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100469static DECLARE_WORK(css_reprobe_work, reprobe_all);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200470
471/* Schedule reprobing of all unregistered subchannels. */
472void css_schedule_reprobe(void)
473{
474 need_reprobe = 1;
475 queue_work(ccw_device_work, &css_reprobe_work);
476}
477
478EXPORT_SYMBOL_GPL(css_schedule_reprobe);
479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 * Called from the machine check handler for subchannel report words.
482 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200483void css_process_crw(int rsid1, int rsid2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800485 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800487 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
488 rsid1, rsid2);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800489 init_subchannel_id(&mchk_schid);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800490 mchk_schid.sch_no = rsid1;
491 if (rsid2 != 0)
492 mchk_schid.ssid = (rsid2 >> 8) & 3;
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /*
495 * Since we are always presented with IPI in the CRW, we have to
496 * use stsch() to find out if the subchannel in question has come
497 * or gone.
498 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200499 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500}
501
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800502static int __init
503__init_channel_subsystem(struct subchannel_id schid, void *data)
504{
505 struct subchannel *sch;
506 int ret;
507
508 if (cio_is_console(schid))
509 sch = cio_get_console_subchannel();
510 else {
511 sch = css_alloc_subchannel(schid);
512 if (IS_ERR(sch))
513 ret = PTR_ERR(sch);
514 else
515 ret = 0;
516 switch (ret) {
517 case 0:
518 break;
519 case -ENOMEM:
520 panic("Out of memory in init_channel_subsystem\n");
521 /* -ENXIO: no more subchannels. */
522 case -ENXIO:
523 return ret;
Greg Smithe843e282006-03-14 19:50:17 -0800524 /* -EIO: this subchannel set not supported. */
525 case -EIO:
526 return ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800527 default:
528 return 0;
529 }
530 }
531 /*
532 * We register ALL valid subchannels in ioinfo, even those
533 * that have been present before init_channel_subsystem.
534 * These subchannels can't have been registered yet (kmalloc
535 * not working) so we do it now. This is true e.g. for the
536 * console subchannel.
537 */
538 css_register_subchannel(sch);
539 return 0;
540}
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800543css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800545 if (css_characteristics_avail && css_general_characteristics.mcss) {
546 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
547 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
548 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549#ifdef CONFIG_SMP
Cornelia Hucka28c6942006-01-06 00:19:23 -0800550 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800552 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553#endif
554 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800555 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
556 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
557 css->global_pgid.tod_high = tod_high;
558
559}
560
Cornelia Huck3b793062006-01-06 00:19:26 -0800561static void
562channel_subsystem_release(struct device *dev)
563{
564 struct channel_subsystem *css;
565
566 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800567 mutex_destroy(&css->mutex);
Cornelia Huck3b793062006-01-06 00:19:26 -0800568 kfree(css);
569}
570
Cornelia Huck495a5b42006-03-24 03:15:14 -0800571static ssize_t
572css_cm_enable_show(struct device *dev, struct device_attribute *attr,
573 char *buf)
574{
575 struct channel_subsystem *css = to_css(dev);
576
577 if (!css)
578 return 0;
579 return sprintf(buf, "%x\n", css->cm_enabled);
580}
581
582static ssize_t
583css_cm_enable_store(struct device *dev, struct device_attribute *attr,
584 const char *buf, size_t count)
585{
586 struct channel_subsystem *css = to_css(dev);
587 int ret;
588
589 switch (buf[0]) {
590 case '0':
591 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
592 break;
593 case '1':
594 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
595 break;
596 default:
597 ret = -EINVAL;
598 }
599 return ret < 0 ? ret : count;
600}
601
602static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
603
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100604static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800605{
606 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100607 int ret;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800608
609 memset(css[nr], 0, sizeof(struct channel_subsystem));
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100610 css[nr]->pseudo_subchannel =
611 kzalloc(sizeof(*css[nr]->pseudo_subchannel), GFP_KERNEL);
612 if (!css[nr]->pseudo_subchannel)
613 return -ENOMEM;
614 css[nr]->pseudo_subchannel->dev.parent = &css[nr]->device;
615 css[nr]->pseudo_subchannel->dev.release = css_subchannel_release;
616 sprintf(css[nr]->pseudo_subchannel->dev.bus_id, "defunct");
617 ret = cio_create_sch_lock(css[nr]->pseudo_subchannel);
618 if (ret) {
619 kfree(css[nr]->pseudo_subchannel);
620 return ret;
621 }
Cornelia Huck495a5b42006-03-24 03:15:14 -0800622 mutex_init(&css[nr]->mutex);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800623 css[nr]->valid = 1;
624 css[nr]->cssid = nr;
625 sprintf(css[nr]->device.bus_id, "css%x", nr);
Cornelia Huck3b793062006-01-06 00:19:26 -0800626 css[nr]->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800627 tod_high = (u32) (get_clock() >> 32);
628 css_generate_pgid(css[nr], tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100629 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630}
631
632/*
633 * Now that the driver core is running, we can setup our channel subsystem.
634 * The struct subchannel's are created during probing (except for the
635 * static console subchannel).
636 */
637static int __init
638init_channel_subsystem (void)
639{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800640 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
Cornelia Huck4434a382007-07-27 12:29:21 +0200642 ret = chsc_determine_css_characteristics();
643 if (ret == -ENOMEM)
644 goto out; /* No need to continue. */
645 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 css_characteristics_avail = 1;
647
Cornelia Huck4434a382007-07-27 12:29:21 +0200648 ret = chsc_alloc_sei_area();
649 if (ret)
650 goto out;
651
652 ret = slow_subchannel_init();
653 if (ret)
654 goto out;
655
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if ((ret = bus_register(&css_bus_type)))
657 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800659 /* Try to enable MSS. */
660 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
661 switch (ret) {
662 case 0: /* Success. */
663 max_ssid = __MAX_SSID;
664 break;
665 case -ENOMEM:
666 goto out_bus;
667 default:
668 max_ssid = 0;
669 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800670 /* Setup css structure. */
671 for (i = 0; i <= __MAX_CSSID; i++) {
672 css[i] = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
673 if (!css[i]) {
674 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800675 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800676 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100677 ret = setup_css(i);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800678 if (ret)
679 goto out_free;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100680 ret = device_register(&css[i]->device);
681 if (ret)
682 goto out_free_all;
Cornelia Huck7e560812006-07-12 16:40:19 +0200683 if (css_characteristics_avail &&
684 css_chsc_characteristics.secm) {
685 ret = device_create_file(&css[i]->device,
686 &dev_attr_cm_enable);
687 if (ret)
688 goto out_device;
689 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100690 ret = device_register(&css[i]->pseudo_subchannel->dev);
691 if (ret)
692 goto out_file;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800693 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 css_init_done = 1;
695
696 ctl_set_bit(6, 28);
697
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800698 for_each_subchannel(__init_channel_subsystem, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 return 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100700out_file:
701 device_remove_file(&css[i]->device, &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200702out_device:
703 device_unregister(&css[i]->device);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100704out_free_all:
705 kfree(css[i]->pseudo_subchannel->lock);
706 kfree(css[i]->pseudo_subchannel);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800707out_free:
708 kfree(css[i]);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800709out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800710 while (i > 0) {
711 i--;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100712 device_unregister(&css[i]->pseudo_subchannel->dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800713 if (css_characteristics_avail && css_chsc_characteristics.secm)
714 device_remove_file(&css[i]->device,
715 &dev_attr_cm_enable);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800716 device_unregister(&css[i]->device);
717 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800718out_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 bus_unregister(&css_bus_type);
720out:
Cornelia Huck4434a382007-07-27 12:29:21 +0200721 chsc_free_sei_area();
722 kfree(slow_subchannel_set);
723 printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
724 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 return ret;
726}
727
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100728int sch_is_pseudo_sch(struct subchannel *sch)
729{
730 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
731}
732
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733/*
734 * find a driver for a subchannel. They identify by the subchannel
735 * type with the exception that the console subchannel driver has its own
736 * subchannel type although the device is an i/o subchannel
737 */
738static int
739css_bus_match (struct device *dev, struct device_driver *drv)
740{
741 struct subchannel *sch = container_of (dev, struct subchannel, dev);
742 struct css_driver *driver = container_of (drv, struct css_driver, drv);
743
744 if (sch->st == driver->subchannel_type)
745 return 1;
746
747 return 0;
748}
749
Cornelia Huck8bbace72006-01-11 10:56:22 +0100750static int
751css_probe (struct device *dev)
752{
753 struct subchannel *sch;
754
755 sch = to_subchannel(dev);
756 sch->driver = container_of (dev->driver, struct css_driver, drv);
757 return (sch->driver->probe ? sch->driver->probe(sch) : 0);
758}
759
760static int
761css_remove (struct device *dev)
762{
763 struct subchannel *sch;
764
765 sch = to_subchannel(dev);
766 return (sch->driver->remove ? sch->driver->remove(sch) : 0);
767}
768
769static void
770css_shutdown (struct device *dev)
771{
772 struct subchannel *sch;
773
774 sch = to_subchannel(dev);
775 if (sch->driver->shutdown)
776 sch->driver->shutdown(sch);
777}
778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +0100780 .name = "css",
781 .match = css_bus_match,
782 .probe = css_probe,
783 .remove = css_remove,
784 .shutdown = css_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785};
786
787subsys_initcall(init_channel_subsystem);
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789MODULE_LICENSE("GPL");
790EXPORT_SYMBOL(css_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791EXPORT_SYMBOL_GPL(css_characteristics_avail);