blob: 08f6e7b6c999f5c1ae2f2194634046db0744b262 [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 Huck7c9f4e32007-10-12 16:11:13 +020030struct channel_subsystem *channel_subsystems[__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) {
Cornelia Huck5693ce62007-08-10 14:32:26 +020082 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 kfree(sch);
84 return ERR_PTR(ret);
85 }
86 return sch;
87}
88
89static void
90css_free_subchannel(struct subchannel *sch)
91{
92 if (sch) {
93 /* Reset intparm to zeroes. */
94 sch->schib.pmcw.intparm = 0;
95 cio_modify(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +010096 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 kfree(sch);
98 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
101static void
102css_subchannel_release(struct device *dev)
103{
104 struct subchannel *sch;
105
106 sch = to_subchannel(dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100107 if (!cio_is_console(sch->schid)) {
108 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 kfree(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100110 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111}
112
Cornelia Huck07c6a332007-07-27 12:29:10 +0200113static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200114{
115 int ret;
116
117 mutex_lock(&sch->reg_mutex);
118 ret = device_register(&sch->dev);
119 mutex_unlock(&sch->reg_mutex);
120 return ret;
121}
122
123void css_sch_device_unregister(struct subchannel *sch)
124{
125 mutex_lock(&sch->reg_mutex);
126 device_unregister(&sch->dev);
127 mutex_unlock(&sch->reg_mutex);
128}
129
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200130static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
131{
132 int i;
133 int mask;
134
135 memset(ssd, 0, sizeof(struct chsc_ssd_info));
136 ssd->path_mask = pmcw->pim;
137 for (i = 0; i < 8; i++) {
138 mask = 0x80 >> i;
139 if (pmcw->pim & mask) {
140 chp_id_init(&ssd->chpid[i]);
141 ssd->chpid[i].id = pmcw->chpid[i];
142 }
143 }
144}
145
146static void ssd_register_chpids(struct chsc_ssd_info *ssd)
147{
148 int i;
149 int mask;
150
151 for (i = 0; i < 8; i++) {
152 mask = 0x80 >> i;
153 if (ssd->path_mask & mask)
154 if (!chp_is_registered(ssd->chpid[i]))
155 chp_new(ssd->chpid[i]);
156 }
157}
158
159void css_update_ssd_info(struct subchannel *sch)
160{
161 int ret;
162
163 if (cio_is_console(sch->schid)) {
164 /* Console is initialized too early for functions requiring
165 * memory allocation. */
166 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
167 } else {
168 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
169 if (ret)
170 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
171 ssd_register_chpids(&sch->ssd_info);
172 }
173}
174
175static int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
177 int ret;
178
179 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200180 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 sch->dev.bus = &css_bus_type;
182 sch->dev.release = &css_subchannel_release;
Cornelia Huck529192f2006-12-08 15:55:57 +0100183 sch->dev.groups = subch_attr_groups;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200184 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200186 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100187 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200188 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
189 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100190 return ret;
191 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return ret;
193}
194
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200195static int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 int ret;
198 struct subchannel *sch;
199
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800200 sch = css_alloc_subchannel(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 if (IS_ERR(sch))
202 return PTR_ERR(sch);
203 ret = css_register_subchannel(sch);
204 if (ret)
205 css_free_subchannel(sch);
206 return ret;
207}
208
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700209static int
210check_subchannel(struct device * dev, void * data)
211{
212 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800213 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700214
215 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800216 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700217}
218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800220get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 struct device *dev;
223
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700224 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200225 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700227 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228}
229
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100230static int css_get_subchannel_status(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
232 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200234 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 return CIO_GONE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200236 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 return CIO_REVALIDATE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200238 if (!sch->lpm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 return CIO_NO_PATH;
240 return CIO_OPER;
241}
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200242
243static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244{
245 int event, ret, disc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 unsigned long flags;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200247 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Cornelia Huck2ec22982006-12-08 15:54:26 +0100249 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200250 disc = device_is_disconnected(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 if (disc && slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200252 /* Disconnected devices are evaluated directly only.*/
Cornelia Huck2ec22982006-12-08 15:54:26 +0100253 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200254 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200256 /* No interrupt after machine check - kill pending timers. */
257 device_kill_pending_timer(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 if (!disc && !slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200259 /* Non-disconnected devices are evaluated on the slow path. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100260 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200261 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200263 event = css_get_subchannel_status(sch);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800264 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200265 sch->schid.ssid, sch->schid.sch_no, event,
266 disc ? "disconnected" : "normal",
267 slow ? "slow" : "fast");
268 /* Analyze subchannel status. */
269 action = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270 switch (event) {
271 case CIO_NO_PATH:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200272 if (disc) {
273 /* Check if paths have become available. */
274 action = REPROBE;
275 break;
276 }
277 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 case CIO_GONE:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200279 /* Prevent unwanted effects when opening lock. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 cio_disable_subchannel(sch);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200281 device_set_disconnected(sch);
282 /* Ask driver what to do with device. */
283 action = UNREGISTER;
284 if (sch->driver && sch->driver->notify) {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100285 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200286 ret = sch->driver->notify(&sch->dev, event);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100287 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200288 if (ret)
289 action = NONE;
290 }
291 break;
292 case CIO_REVALIDATE:
293 /* Device will be removed, so no notify necessary. */
294 if (disc)
295 /* Reprobe because immediate unregister might block. */
296 action = REPROBE;
297 else
298 action = UNREGISTER_PROBE;
299 break;
300 case CIO_OPER:
301 if (disc)
302 /* Get device operational again. */
303 action = REPROBE;
304 break;
305 }
306 /* Perform action. */
307 ret = 0;
308 switch (action) {
309 case UNREGISTER:
310 case UNREGISTER_PROBE:
311 /* Unregister device (will use subchannel lock). */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100312 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200313 css_sch_device_unregister(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100314 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 /* Reset intparm to zeroes. */
317 sch->schib.pmcw.intparm = 0;
318 cio_modify(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 break;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200320 case REPROBE:
321 device_trigger_reprobe(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 break;
323 default:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200324 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 }
Cornelia Huck2ec22982006-12-08 15:54:26 +0100326 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huckc2b14492006-10-27 12:39:17 +0200327 /* Probe if necessary. */
328 if (action == UNREGISTER_PROBE)
329 ret = css_probe_device(sch->schid);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200330
331 return ret;
332}
333
334static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
335{
336 struct schib schib;
337
338 if (!slow) {
339 /* Will be done on the slow path. */
340 return -EAGAIN;
341 }
Cornelia Huck758976f2007-02-05 21:17:36 +0100342 if (stsch_err(schid, &schib) || !schib.pmcw.dnv) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200343 /* Unusable - ignore. */
344 return 0;
345 }
346 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
347 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
348
349 return css_probe_device(schid);
350}
351
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200352static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200353{
354 struct subchannel *sch;
355 int ret;
356
357 sch = get_subchannel_by_schid(schid);
358 if (sch) {
359 ret = css_evaluate_known_subchannel(sch, slow);
360 put_device(&sch->dev);
361 } else
362 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200363 if (ret == -EAGAIN)
364 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365}
366
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200367static struct idset *slow_subchannel_set;
368static spinlock_t slow_subchannel_lock;
369
370static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200372 spin_lock_init(&slow_subchannel_lock);
373 slow_subchannel_set = idset_sch_new();
374 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200375 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200376 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200378 return 0;
379}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200381static void css_slow_path_func(struct work_struct *unused)
382{
383 struct subchannel_id schid;
384
385 CIO_TRACE_EVENT(4, "slowpath");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_lock_irq(&slow_subchannel_lock);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200387 init_subchannel_id(&schid);
388 while (idset_sch_get_first(slow_subchannel_set, &schid)) {
389 idset_sch_del(slow_subchannel_set, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 spin_unlock_irq(&slow_subchannel_lock);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200391 css_evaluate_subchannel(schid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 spin_lock_irq(&slow_subchannel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 }
394 spin_unlock_irq(&slow_subchannel_lock);
395}
396
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200397static DECLARE_WORK(slow_path_work, css_slow_path_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398struct workqueue_struct *slow_path_wq;
399
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200400void css_schedule_eval(struct subchannel_id schid)
401{
402 unsigned long flags;
403
404 spin_lock_irqsave(&slow_subchannel_lock, flags);
405 idset_sch_add(slow_subchannel_set, schid);
406 queue_work(slow_path_wq, &slow_path_work);
407 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
408}
409
410void css_schedule_eval_all(void)
411{
412 unsigned long flags;
413
414 spin_lock_irqsave(&slow_subchannel_lock, flags);
415 idset_fill(slow_subchannel_set);
416 queue_work(slow_path_wq, &slow_path_work);
417 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
418}
419
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200420/* Reprobe subchannel if unregistered. */
421static int reprobe_subchannel(struct subchannel_id schid, void *data)
422{
423 struct subchannel *sch;
424 int ret;
425
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200426 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
427 schid.ssid, schid.sch_no);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200428 if (need_reprobe)
429 return -EAGAIN;
430
431 sch = get_subchannel_by_schid(schid);
432 if (sch) {
433 /* Already known. */
434 put_device(&sch->dev);
435 return 0;
436 }
437
438 ret = css_probe_device(schid);
439 switch (ret) {
440 case 0:
441 break;
442 case -ENXIO:
443 case -ENOMEM:
444 /* These should abort looping */
445 break;
446 default:
447 ret = 0;
448 }
449
450 return ret;
451}
452
453/* Work function used to reprobe all unregistered subchannels. */
Al Viro4927b3f2006-12-06 19:18:20 +0000454static void reprobe_all(struct work_struct *unused)
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200455{
456 int ret;
457
458 CIO_MSG_EVENT(2, "reprobe start\n");
459
460 need_reprobe = 0;
461 /* Make sure initial subchannel scan is done. */
462 wait_event(ccw_device_init_wq,
463 atomic_read(&ccw_device_init_count) == 0);
464 ret = for_each_subchannel(reprobe_subchannel, NULL);
465
466 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
467 need_reprobe);
468}
469
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100470static DECLARE_WORK(css_reprobe_work, reprobe_all);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200471
472/* Schedule reprobing of all unregistered subchannels. */
473void css_schedule_reprobe(void)
474{
475 need_reprobe = 1;
476 queue_work(ccw_device_work, &css_reprobe_work);
477}
478
479EXPORT_SYMBOL_GPL(css_schedule_reprobe);
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 * Called from the machine check handler for subchannel report words.
483 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200484void css_process_crw(int rsid1, int rsid2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800486 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800488 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
489 rsid1, rsid2);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800490 init_subchannel_id(&mchk_schid);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800491 mchk_schid.sch_no = rsid1;
492 if (rsid2 != 0)
493 mchk_schid.ssid = (rsid2 >> 8) & 3;
494
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 /*
496 * Since we are always presented with IPI in the CRW, we have to
497 * use stsch() to find out if the subchannel in question has come
498 * or gone.
499 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200500 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501}
502
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800503static int __init
504__init_channel_subsystem(struct subchannel_id schid, void *data)
505{
506 struct subchannel *sch;
507 int ret;
508
509 if (cio_is_console(schid))
510 sch = cio_get_console_subchannel();
511 else {
512 sch = css_alloc_subchannel(schid);
513 if (IS_ERR(sch))
514 ret = PTR_ERR(sch);
515 else
516 ret = 0;
517 switch (ret) {
518 case 0:
519 break;
520 case -ENOMEM:
521 panic("Out of memory in init_channel_subsystem\n");
522 /* -ENXIO: no more subchannels. */
523 case -ENXIO:
524 return ret;
Greg Smithe843e282006-03-14 19:50:17 -0800525 /* -EIO: this subchannel set not supported. */
526 case -EIO:
527 return ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800528 default:
529 return 0;
530 }
531 }
532 /*
533 * We register ALL valid subchannels in ioinfo, even those
534 * that have been present before init_channel_subsystem.
535 * These subchannels can't have been registered yet (kmalloc
536 * not working) so we do it now. This is true e.g. for the
537 * console subchannel.
538 */
539 css_register_subchannel(sch);
540 return 0;
541}
542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800544css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800546 if (css_characteristics_avail && css_general_characteristics.mcss) {
547 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
548 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
549 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550#ifdef CONFIG_SMP
Cornelia Hucka28c6942006-01-06 00:19:23 -0800551 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800553 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554#endif
555 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800556 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
557 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
558 css->global_pgid.tod_high = tod_high;
559
560}
561
Cornelia Huck3b793062006-01-06 00:19:26 -0800562static void
563channel_subsystem_release(struct device *dev)
564{
565 struct channel_subsystem *css;
566
567 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800568 mutex_destroy(&css->mutex);
Cornelia Huck3b793062006-01-06 00:19:26 -0800569 kfree(css);
570}
571
Cornelia Huck495a5b42006-03-24 03:15:14 -0800572static ssize_t
573css_cm_enable_show(struct device *dev, struct device_attribute *attr,
574 char *buf)
575{
576 struct channel_subsystem *css = to_css(dev);
577
578 if (!css)
579 return 0;
580 return sprintf(buf, "%x\n", css->cm_enabled);
581}
582
583static ssize_t
584css_cm_enable_store(struct device *dev, struct device_attribute *attr,
585 const char *buf, size_t count)
586{
587 struct channel_subsystem *css = to_css(dev);
588 int ret;
589
590 switch (buf[0]) {
591 case '0':
592 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
593 break;
594 case '1':
595 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
596 break;
597 default:
598 ret = -EINVAL;
599 }
600 return ret < 0 ? ret : count;
601}
602
603static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
604
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100605static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800606{
607 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100608 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200609 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800610
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200611 css = channel_subsystems[nr];
612 memset(css, 0, sizeof(struct channel_subsystem));
613 css->pseudo_subchannel =
614 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
615 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100616 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200617 css->pseudo_subchannel->dev.parent = &css->device;
618 css->pseudo_subchannel->dev.release = css_subchannel_release;
619 sprintf(css->pseudo_subchannel->dev.bus_id, "defunct");
620 ret = cio_create_sch_lock(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100621 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200622 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100623 return ret;
624 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200625 mutex_init(&css->mutex);
626 css->valid = 1;
627 css->cssid = nr;
628 sprintf(css->device.bus_id, "css%x", nr);
629 css->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800630 tod_high = (u32) (get_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200631 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100632 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633}
634
635/*
636 * Now that the driver core is running, we can setup our channel subsystem.
637 * The struct subchannel's are created during probing (except for the
638 * static console subchannel).
639 */
640static int __init
641init_channel_subsystem (void)
642{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800643 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644
Cornelia Huck4434a382007-07-27 12:29:21 +0200645 ret = chsc_determine_css_characteristics();
646 if (ret == -ENOMEM)
647 goto out; /* No need to continue. */
648 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 css_characteristics_avail = 1;
650
Cornelia Huck4434a382007-07-27 12:29:21 +0200651 ret = chsc_alloc_sei_area();
652 if (ret)
653 goto out;
654
655 ret = slow_subchannel_init();
656 if (ret)
657 goto out;
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 if ((ret = bus_register(&css_bus_type)))
660 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800662 /* Try to enable MSS. */
663 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
664 switch (ret) {
665 case 0: /* Success. */
666 max_ssid = __MAX_SSID;
667 break;
668 case -ENOMEM:
669 goto out_bus;
670 default:
671 max_ssid = 0;
672 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800673 /* Setup css structure. */
674 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200675 struct channel_subsystem *css;
676
677 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
678 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800679 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800680 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800681 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200682 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100683 ret = setup_css(i);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800684 if (ret)
685 goto out_free;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200686 ret = device_register(&css->device);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100687 if (ret)
688 goto out_free_all;
Cornelia Huck7e560812006-07-12 16:40:19 +0200689 if (css_characteristics_avail &&
690 css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200691 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200692 &dev_attr_cm_enable);
693 if (ret)
694 goto out_device;
695 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200696 ret = device_register(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100697 if (ret)
698 goto out_file;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800699 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 css_init_done = 1;
701
702 ctl_set_bit(6, 28);
703
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800704 for_each_subchannel(__init_channel_subsystem, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 return 0;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100706out_file:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200707 device_remove_file(&channel_subsystems[i]->device,
708 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200709out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200710 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100711out_free_all:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200712 kfree(channel_subsystems[i]->pseudo_subchannel->lock);
713 kfree(channel_subsystems[i]->pseudo_subchannel);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800714out_free:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200715 kfree(channel_subsystems[i]);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800716out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800717 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200718 struct channel_subsystem *css;
719
Cornelia Hucka28c6942006-01-06 00:19:23 -0800720 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200721 css = channel_subsystems[i];
722 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800723 if (css_characteristics_avail && css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200724 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800725 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200726 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800727 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800728out_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 bus_unregister(&css_bus_type);
730out:
Cornelia Huck4434a382007-07-27 12:29:21 +0200731 chsc_free_sei_area();
732 kfree(slow_subchannel_set);
733 printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
734 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return ret;
736}
737
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100738int sch_is_pseudo_sch(struct subchannel *sch)
739{
740 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
741}
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743/*
744 * find a driver for a subchannel. They identify by the subchannel
745 * type with the exception that the console subchannel driver has its own
746 * subchannel type although the device is an i/o subchannel
747 */
748static int
749css_bus_match (struct device *dev, struct device_driver *drv)
750{
751 struct subchannel *sch = container_of (dev, struct subchannel, dev);
752 struct css_driver *driver = container_of (drv, struct css_driver, drv);
753
754 if (sch->st == driver->subchannel_type)
755 return 1;
756
757 return 0;
758}
759
Cornelia Huck8bbace72006-01-11 10:56:22 +0100760static int
761css_probe (struct device *dev)
762{
763 struct subchannel *sch;
764
765 sch = to_subchannel(dev);
766 sch->driver = container_of (dev->driver, struct css_driver, drv);
767 return (sch->driver->probe ? sch->driver->probe(sch) : 0);
768}
769
770static int
771css_remove (struct device *dev)
772{
773 struct subchannel *sch;
774
775 sch = to_subchannel(dev);
776 return (sch->driver->remove ? sch->driver->remove(sch) : 0);
777}
778
779static void
780css_shutdown (struct device *dev)
781{
782 struct subchannel *sch;
783
784 sch = to_subchannel(dev);
785 if (sch->driver->shutdown)
786 sch->driver->shutdown(sch);
787}
788
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +0100790 .name = "css",
791 .match = css_bus_match,
792 .probe = css_probe,
793 .remove = css_remove,
794 .shutdown = css_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795};
796
797subsys_initcall(init_channel_subsystem);
798
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799MODULE_LICENSE("GPL");
800EXPORT_SYMBOL(css_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801EXPORT_SYMBOL_GPL(css_characteristics_avail);