blob: f558686a66dd97a9e8bf467354570d4ab536e49b [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>
Cornelia Hucka55360d2007-10-12 16:11:20 +020016#include <linux/reboot.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017
18#include "css.h"
19#include "cio.h"
20#include "cio_debug.h"
21#include "ioasm.h"
22#include "chsc.h"
Peter Oberparleiter40154b82006-06-29 14:57:03 +020023#include "device.h"
Peter Oberparleiter83b33702007-04-27 16:01:34 +020024#include "idset.h"
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +020025#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Linus Torvalds1da177e2005-04-16 15:20:36 -070027int css_init_done = 0;
Peter Oberparleiter40154b82006-06-29 14:57:03 +020028static int need_reprobe = 0;
Cornelia Huckfb6958a2006-01-06 00:19:25 -080029static int max_ssid = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
Cornelia Huck7c9f4e32007-10-12 16:11:13 +020031struct channel_subsystem *channel_subsystems[__MAX_CSSID + 1];
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
Cornelia Hucka28c6942006-01-06 00:19:23 -080033int css_characteristics_avail = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Heiko Carstens4d284ca2007-02-05 21:18:53 +010035int
Cornelia Huckf97a56f2006-01-06 00:19:22 -080036for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
37{
38 struct subchannel_id schid;
39 int ret;
40
41 init_subchannel_id(&schid);
42 ret = -ENODEV;
43 do {
Cornelia Huckfb6958a2006-01-06 00:19:25 -080044 do {
45 ret = fn(schid, data);
46 if (ret)
47 break;
48 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
49 schid.sch_no = 0;
50 } while (schid.ssid++ < max_ssid);
Cornelia Huckf97a56f2006-01-06 00:19:22 -080051 return ret;
52}
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054static struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -080055css_alloc_subchannel(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
57 struct subchannel *sch;
58 int ret;
59
60 sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
61 if (sch == NULL)
62 return ERR_PTR(-ENOMEM);
Cornelia Hucka8237fc2006-01-06 00:19:21 -080063 ret = cio_validate_subchannel (sch, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 if (ret < 0) {
65 kfree(sch);
66 return ERR_PTR(ret);
67 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
69 if (sch->st != SUBCHANNEL_TYPE_IO) {
70 /* For now we ignore all non-io subchannels. */
71 kfree(sch);
72 return ERR_PTR(-EINVAL);
73 }
74
75 /*
76 * Set intparm to subchannel address.
77 * This is fine even on 64bit since the subchannel is always located
78 * under 2G.
79 */
80 sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
81 ret = cio_modify(sch);
82 if (ret) {
Cornelia Huck5693ce62007-08-10 14:32:26 +020083 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 kfree(sch);
85 return ERR_PTR(ret);
86 }
87 return sch;
88}
89
90static void
91css_free_subchannel(struct subchannel *sch)
92{
93 if (sch) {
94 /* Reset intparm to zeroes. */
95 sch->schib.pmcw.intparm = 0;
96 cio_modify(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +010097 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 kfree(sch);
99 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
102static void
103css_subchannel_release(struct device *dev)
104{
105 struct subchannel *sch;
106
107 sch = to_subchannel(dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100108 if (!cio_is_console(sch->schid)) {
109 kfree(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 kfree(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100111 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Cornelia Huck07c6a332007-07-27 12:29:10 +0200114static int css_sch_device_register(struct subchannel *sch)
Cornelia Huck6ab48792006-07-12 16:39:50 +0200115{
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
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200131static void ssd_from_pmcw(struct chsc_ssd_info *ssd, struct pmcw *pmcw)
132{
133 int i;
134 int mask;
135
136 memset(ssd, 0, sizeof(struct chsc_ssd_info));
137 ssd->path_mask = pmcw->pim;
138 for (i = 0; i < 8; i++) {
139 mask = 0x80 >> i;
140 if (pmcw->pim & mask) {
141 chp_id_init(&ssd->chpid[i]);
142 ssd->chpid[i].id = pmcw->chpid[i];
143 }
144 }
145}
146
147static void ssd_register_chpids(struct chsc_ssd_info *ssd)
148{
149 int i;
150 int mask;
151
152 for (i = 0; i < 8; i++) {
153 mask = 0x80 >> i;
154 if (ssd->path_mask & mask)
155 if (!chp_is_registered(ssd->chpid[i]))
156 chp_new(ssd->chpid[i]);
157 }
158}
159
160void css_update_ssd_info(struct subchannel *sch)
161{
162 int ret;
163
164 if (cio_is_console(sch->schid)) {
165 /* Console is initialized too early for functions requiring
166 * memory allocation. */
167 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
168 } else {
169 ret = chsc_get_ssd_info(sch->schid, &sch->ssd_info);
170 if (ret)
171 ssd_from_pmcw(&sch->ssd_info, &sch->schib.pmcw);
172 ssd_register_chpids(&sch->ssd_info);
173 }
174}
175
176static int css_register_subchannel(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 int ret;
179
180 /* Initialize the subchannel structure */
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200181 sch->dev.parent = &channel_subsystems[0]->device;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 sch->dev.bus = &css_bus_type;
183 sch->dev.release = &css_subchannel_release;
Cornelia Huck529192f2006-12-08 15:55:57 +0100184 sch->dev.groups = subch_attr_groups;
Cornelia Huck5bf04b22007-10-22 12:52:41 +0200185 /*
186 * We don't want to generate uevents for I/O subchannels that don't
187 * have a working ccw device behind them since they will be
188 * unregistered before they can be used anyway, so we delay the add
189 * uevent until after device recognition was successful.
190 */
191 if (!cio_is_console(sch->schid))
192 /* Console is special, no need to suppress. */
193 sch->dev.uevent_suppress = 1;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200194 css_update_ssd_info(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 /* make it known to the system */
Cornelia Huck6ab48792006-07-12 16:39:50 +0200196 ret = css_sch_device_register(sch);
Cornelia Huck7674da72006-12-08 15:54:21 +0100197 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200198 CIO_MSG_EVENT(0, "Could not register sch 0.%x.%04x: %d\n",
199 sch->schid.ssid, sch->schid.sch_no, ret);
Cornelia Huck7674da72006-12-08 15:54:21 +0100200 return ret;
201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 return ret;
203}
204
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200205static int css_probe_device(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206{
207 int ret;
208 struct subchannel *sch;
209
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800210 sch = css_alloc_subchannel(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 if (IS_ERR(sch))
212 return PTR_ERR(sch);
213 ret = css_register_subchannel(sch);
214 if (ret)
215 css_free_subchannel(sch);
216 return ret;
217}
218
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700219static int
220check_subchannel(struct device * dev, void * data)
221{
222 struct subchannel *sch;
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800223 struct subchannel_id *schid = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700224
225 sch = to_subchannel(dev);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800226 return schid_equal(&sch->schid, schid);
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700227}
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229struct subchannel *
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800230get_subchannel_by_schid(struct subchannel_id schid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct device *dev;
233
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700234 dev = bus_find_device(&css_bus_type, NULL,
Cornelia Huck12975ae2006-10-11 15:31:47 +0200235 &schid, check_subchannel);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700237 return dev ? to_subchannel(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100240static int css_get_subchannel_status(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241{
242 struct schib schib;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200244 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 return CIO_GONE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200246 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return CIO_REVALIDATE;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200248 if (!sch->lpm)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 return CIO_NO_PATH;
250 return CIO_OPER;
251}
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200252
253static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 int event, ret, disc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 unsigned long flags;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200257 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
Cornelia Huck2ec22982006-12-08 15:54:26 +0100259 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200260 disc = device_is_disconnected(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 if (disc && slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200262 /* Disconnected devices are evaluated directly only.*/
Cornelia Huck2ec22982006-12-08 15:54:26 +0100263 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200264 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200266 /* No interrupt after machine check - kill pending timers. */
267 device_kill_pending_timer(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (!disc && !slow) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200269 /* Non-disconnected devices are evaluated on the slow path. */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100270 spin_unlock_irqrestore(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200271 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 }
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200273 event = css_get_subchannel_status(sch);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800274 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200275 sch->schid.ssid, sch->schid.sch_no, event,
276 disc ? "disconnected" : "normal",
277 slow ? "slow" : "fast");
278 /* Analyze subchannel status. */
279 action = NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 switch (event) {
281 case CIO_NO_PATH:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200282 if (disc) {
283 /* Check if paths have become available. */
284 action = REPROBE;
285 break;
286 }
287 /* fall through */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288 case CIO_GONE:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200289 /* Prevent unwanted effects when opening lock. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700290 cio_disable_subchannel(sch);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200291 device_set_disconnected(sch);
292 /* Ask driver what to do with device. */
293 action = UNREGISTER;
294 if (sch->driver && sch->driver->notify) {
Cornelia Huck2ec22982006-12-08 15:54:26 +0100295 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck602b20f2008-01-26 14:10:39 +0100296 ret = sch->driver->notify(sch, event);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100297 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200298 if (ret)
299 action = NONE;
300 }
301 break;
302 case CIO_REVALIDATE:
303 /* Device will be removed, so no notify necessary. */
304 if (disc)
305 /* Reprobe because immediate unregister might block. */
306 action = REPROBE;
307 else
308 action = UNREGISTER_PROBE;
309 break;
310 case CIO_OPER:
311 if (disc)
312 /* Get device operational again. */
313 action = REPROBE;
314 break;
315 }
316 /* Perform action. */
317 ret = 0;
318 switch (action) {
319 case UNREGISTER:
320 case UNREGISTER_PROBE:
321 /* Unregister device (will use subchannel lock). */
Cornelia Huck2ec22982006-12-08 15:54:26 +0100322 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200323 css_sch_device_unregister(sch);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100324 spin_lock_irqsave(sch->lock, flags);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200325
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 /* Reset intparm to zeroes. */
327 sch->schib.pmcw.intparm = 0;
328 cio_modify(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 break;
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200330 case REPROBE:
331 device_trigger_reprobe(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 break;
333 default:
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200334 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 }
Cornelia Huck2ec22982006-12-08 15:54:26 +0100336 spin_unlock_irqrestore(sch->lock, flags);
Cornelia Huckc2b14492006-10-27 12:39:17 +0200337 /* Probe if necessary. */
338 if (action == UNREGISTER_PROBE)
339 ret = css_probe_device(sch->schid);
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200340
341 return ret;
342}
343
344static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
345{
346 struct schib schib;
347
348 if (!slow) {
349 /* Will be done on the slow path. */
350 return -EAGAIN;
351 }
Cornelia Huck758976f2007-02-05 21:17:36 +0100352 if (stsch_err(schid, &schib) || !schib.pmcw.dnv) {
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200353 /* Unusable - ignore. */
354 return 0;
355 }
356 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
357 "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
358
359 return css_probe_device(schid);
360}
361
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200362static void css_evaluate_subchannel(struct subchannel_id schid, int slow)
Peter Oberparleiter564337f2006-09-20 16:00:01 +0200363{
364 struct subchannel *sch;
365 int ret;
366
367 sch = get_subchannel_by_schid(schid);
368 if (sch) {
369 ret = css_evaluate_known_subchannel(sch, slow);
370 put_device(&sch->dev);
371 } else
372 ret = css_evaluate_new_subchannel(schid, slow);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200373 if (ret == -EAGAIN)
374 css_schedule_eval(schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375}
376
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200377static struct idset *slow_subchannel_set;
378static spinlock_t slow_subchannel_lock;
379
380static int __init slow_subchannel_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381{
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200382 spin_lock_init(&slow_subchannel_lock);
383 slow_subchannel_set = idset_sch_new();
384 if (!slow_subchannel_set) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200385 CIO_MSG_EVENT(0, "could not allocate slow subchannel set\n");
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200386 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200388 return 0;
389}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200391static void css_slow_path_func(struct work_struct *unused)
392{
393 struct subchannel_id schid;
394
395 CIO_TRACE_EVENT(4, "slowpath");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 spin_lock_irq(&slow_subchannel_lock);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200397 init_subchannel_id(&schid);
398 while (idset_sch_get_first(slow_subchannel_set, &schid)) {
399 idset_sch_del(slow_subchannel_set, schid);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 spin_unlock_irq(&slow_subchannel_lock);
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200401 css_evaluate_subchannel(schid, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 spin_lock_irq(&slow_subchannel_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
404 spin_unlock_irq(&slow_subchannel_lock);
405}
406
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200407static DECLARE_WORK(slow_path_work, css_slow_path_func);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408struct workqueue_struct *slow_path_wq;
409
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200410void css_schedule_eval(struct subchannel_id schid)
411{
412 unsigned long flags;
413
414 spin_lock_irqsave(&slow_subchannel_lock, flags);
415 idset_sch_add(slow_subchannel_set, schid);
416 queue_work(slow_path_wq, &slow_path_work);
417 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
418}
419
420void css_schedule_eval_all(void)
421{
422 unsigned long flags;
423
424 spin_lock_irqsave(&slow_subchannel_lock, flags);
425 idset_fill(slow_subchannel_set);
426 queue_work(slow_path_wq, &slow_path_work);
427 spin_unlock_irqrestore(&slow_subchannel_lock, flags);
428}
429
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200430/* Reprobe subchannel if unregistered. */
431static int reprobe_subchannel(struct subchannel_id schid, void *data)
432{
433 struct subchannel *sch;
434 int ret;
435
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200436 CIO_MSG_EVENT(6, "cio: reprobe 0.%x.%04x\n",
437 schid.ssid, schid.sch_no);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200438 if (need_reprobe)
439 return -EAGAIN;
440
441 sch = get_subchannel_by_schid(schid);
442 if (sch) {
443 /* Already known. */
444 put_device(&sch->dev);
445 return 0;
446 }
447
448 ret = css_probe_device(schid);
449 switch (ret) {
450 case 0:
451 break;
452 case -ENXIO:
453 case -ENOMEM:
Peter Oberparleiter671756162007-12-04 16:09:02 +0100454 case -EIO:
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200455 /* These should abort looping */
456 break;
457 default:
458 ret = 0;
459 }
460
461 return ret;
462}
463
464/* Work function used to reprobe all unregistered subchannels. */
Al Viro4927b3f2006-12-06 19:18:20 +0000465static void reprobe_all(struct work_struct *unused)
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200466{
467 int ret;
468
469 CIO_MSG_EVENT(2, "reprobe start\n");
470
471 need_reprobe = 0;
472 /* Make sure initial subchannel scan is done. */
473 wait_event(ccw_device_init_wq,
474 atomic_read(&ccw_device_init_count) == 0);
475 ret = for_each_subchannel(reprobe_subchannel, NULL);
476
477 CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
478 need_reprobe);
479}
480
Heiko Carstens2b67fc42007-02-05 21:16:47 +0100481static DECLARE_WORK(css_reprobe_work, reprobe_all);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200482
483/* Schedule reprobing of all unregistered subchannels. */
484void css_schedule_reprobe(void)
485{
486 need_reprobe = 1;
Cornelia Huckc5d4a992007-11-20 11:13:41 +0100487 queue_work(slow_path_wq, &css_reprobe_work);
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200488}
489
490EXPORT_SYMBOL_GPL(css_schedule_reprobe);
491
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 * Called from the machine check handler for subchannel report words.
494 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200495void css_process_crw(int rsid1, int rsid2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800497 struct subchannel_id mchk_schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800499 CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
500 rsid1, rsid2);
Cornelia Hucka8237fc2006-01-06 00:19:21 -0800501 init_subchannel_id(&mchk_schid);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800502 mchk_schid.sch_no = rsid1;
503 if (rsid2 != 0)
504 mchk_schid.ssid = (rsid2 >> 8) & 3;
505
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506 /*
507 * Since we are always presented with IPI in the CRW, we have to
508 * use stsch() to find out if the subchannel in question has come
509 * or gone.
510 */
Peter Oberparleiter83b33702007-04-27 16:01:34 +0200511 css_evaluate_subchannel(mchk_schid, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512}
513
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800514static int __init
515__init_channel_subsystem(struct subchannel_id schid, void *data)
516{
517 struct subchannel *sch;
518 int ret;
519
520 if (cio_is_console(schid))
521 sch = cio_get_console_subchannel();
522 else {
523 sch = css_alloc_subchannel(schid);
524 if (IS_ERR(sch))
525 ret = PTR_ERR(sch);
526 else
527 ret = 0;
528 switch (ret) {
529 case 0:
530 break;
531 case -ENOMEM:
532 panic("Out of memory in init_channel_subsystem\n");
533 /* -ENXIO: no more subchannels. */
534 case -ENXIO:
535 return ret;
Greg Smithe843e282006-03-14 19:50:17 -0800536 /* -EIO: this subchannel set not supported. */
537 case -EIO:
538 return ret;
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800539 default:
540 return 0;
541 }
542 }
543 /*
544 * We register ALL valid subchannels in ioinfo, even those
545 * that have been present before init_channel_subsystem.
546 * These subchannels can't have been registered yet (kmalloc
547 * not working) so we do it now. This is true e.g. for the
548 * console subchannel.
549 */
550 css_register_subchannel(sch);
551 return 0;
552}
553
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554static void __init
Cornelia Hucka28c6942006-01-06 00:19:23 -0800555css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800557 if (css_characteristics_avail && css_general_characteristics.mcss) {
558 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
559 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
560 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561#ifdef CONFIG_SMP
Cornelia Hucka28c6942006-01-06 00:19:23 -0800562 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563#else
Cornelia Hucka28c6942006-01-06 00:19:23 -0800564 css->global_pgid.pgid_high.cpu_addr = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565#endif
566 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800567 css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
568 css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
569 css->global_pgid.tod_high = tod_high;
570
571}
572
Cornelia Huck3b793062006-01-06 00:19:26 -0800573static void
574channel_subsystem_release(struct device *dev)
575{
576 struct channel_subsystem *css;
577
578 css = to_css(dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800579 mutex_destroy(&css->mutex);
Cornelia Huck3b793062006-01-06 00:19:26 -0800580 kfree(css);
581}
582
Cornelia Huck495a5b42006-03-24 03:15:14 -0800583static ssize_t
584css_cm_enable_show(struct device *dev, struct device_attribute *attr,
585 char *buf)
586{
587 struct channel_subsystem *css = to_css(dev);
588
589 if (!css)
590 return 0;
591 return sprintf(buf, "%x\n", css->cm_enabled);
592}
593
594static ssize_t
595css_cm_enable_store(struct device *dev, struct device_attribute *attr,
596 const char *buf, size_t count)
597{
598 struct channel_subsystem *css = to_css(dev);
599 int ret;
600
601 switch (buf[0]) {
602 case '0':
603 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
604 break;
605 case '1':
606 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
607 break;
608 default:
609 ret = -EINVAL;
610 }
611 return ret < 0 ? ret : count;
612}
613
614static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
615
Heiko Carstens4d284ca2007-02-05 21:18:53 +0100616static int __init setup_css(int nr)
Cornelia Hucka28c6942006-01-06 00:19:23 -0800617{
618 u32 tod_high;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100619 int ret;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200620 struct channel_subsystem *css;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800621
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200622 css = channel_subsystems[nr];
623 memset(css, 0, sizeof(struct channel_subsystem));
624 css->pseudo_subchannel =
625 kzalloc(sizeof(*css->pseudo_subchannel), GFP_KERNEL);
626 if (!css->pseudo_subchannel)
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100627 return -ENOMEM;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200628 css->pseudo_subchannel->dev.parent = &css->device;
629 css->pseudo_subchannel->dev.release = css_subchannel_release;
630 sprintf(css->pseudo_subchannel->dev.bus_id, "defunct");
631 ret = cio_create_sch_lock(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100632 if (ret) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200633 kfree(css->pseudo_subchannel);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100634 return ret;
635 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200636 mutex_init(&css->mutex);
637 css->valid = 1;
638 css->cssid = nr;
639 sprintf(css->device.bus_id, "css%x", nr);
640 css->device.release = channel_subsystem_release;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800641 tod_high = (u32) (get_clock() >> 32);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200642 css_generate_pgid(css, tod_high);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100643 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644}
645
Cornelia Hucka55360d2007-10-12 16:11:20 +0200646static int css_reboot_event(struct notifier_block *this,
647 unsigned long event,
648 void *ptr)
649{
650 int ret, i;
651
652 ret = NOTIFY_DONE;
653 for (i = 0; i <= __MAX_CSSID; i++) {
654 struct channel_subsystem *css;
655
656 css = channel_subsystems[i];
657 if (css->cm_enabled)
658 if (chsc_secm(css, 0))
659 ret = NOTIFY_BAD;
660 }
661
662 return ret;
663}
664
665static struct notifier_block css_reboot_notifier = {
666 .notifier_call = css_reboot_event,
667};
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669/*
670 * Now that the driver core is running, we can setup our channel subsystem.
671 * The struct subchannel's are created during probing (except for the
672 * static console subchannel).
673 */
674static int __init
675init_channel_subsystem (void)
676{
Cornelia Hucka28c6942006-01-06 00:19:23 -0800677 int ret, i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Cornelia Huck4434a382007-07-27 12:29:21 +0200679 ret = chsc_determine_css_characteristics();
680 if (ret == -ENOMEM)
681 goto out; /* No need to continue. */
682 if (ret == 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 css_characteristics_avail = 1;
684
Cornelia Huck4434a382007-07-27 12:29:21 +0200685 ret = chsc_alloc_sei_area();
686 if (ret)
687 goto out;
688
689 ret = slow_subchannel_init();
690 if (ret)
691 goto out;
692
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 if ((ret = bus_register(&css_bus_type)))
694 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800696 /* Try to enable MSS. */
697 ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
698 switch (ret) {
699 case 0: /* Success. */
700 max_ssid = __MAX_SSID;
701 break;
702 case -ENOMEM:
703 goto out_bus;
704 default:
705 max_ssid = 0;
706 }
Cornelia Hucka28c6942006-01-06 00:19:23 -0800707 /* Setup css structure. */
708 for (i = 0; i <= __MAX_CSSID; i++) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200709 struct channel_subsystem *css;
710
711 css = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
712 if (!css) {
Cornelia Hucka28c6942006-01-06 00:19:23 -0800713 ret = -ENOMEM;
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800714 goto out_unregister;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800715 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200716 channel_subsystems[i] = css;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100717 ret = setup_css(i);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800718 if (ret)
719 goto out_free;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200720 ret = device_register(&css->device);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100721 if (ret)
722 goto out_free_all;
Cornelia Huck7e560812006-07-12 16:40:19 +0200723 if (css_characteristics_avail &&
724 css_chsc_characteristics.secm) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200725 ret = device_create_file(&css->device,
Cornelia Huck7e560812006-07-12 16:40:19 +0200726 &dev_attr_cm_enable);
727 if (ret)
728 goto out_device;
729 }
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200730 ret = device_register(&css->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100731 if (ret)
732 goto out_file;
Cornelia Hucka28c6942006-01-06 00:19:23 -0800733 }
Cornelia Hucka55360d2007-10-12 16:11:20 +0200734 ret = register_reboot_notifier(&css_reboot_notifier);
735 if (ret)
736 goto out_pseudo;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 css_init_done = 1;
738
739 ctl_set_bit(6, 28);
740
Cornelia Huckf97a56f2006-01-06 00:19:22 -0800741 for_each_subchannel(__init_channel_subsystem, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 return 0;
Cornelia Hucka55360d2007-10-12 16:11:20 +0200743out_pseudo:
744 device_unregister(&channel_subsystems[i]->pseudo_subchannel->dev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100745out_file:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200746 device_remove_file(&channel_subsystems[i]->device,
747 &dev_attr_cm_enable);
Cornelia Huck7e560812006-07-12 16:40:19 +0200748out_device:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200749 device_unregister(&channel_subsystems[i]->device);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100750out_free_all:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200751 kfree(channel_subsystems[i]->pseudo_subchannel->lock);
752 kfree(channel_subsystems[i]->pseudo_subchannel);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800753out_free:
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200754 kfree(channel_subsystems[i]);
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800755out_unregister:
Cornelia Hucka28c6942006-01-06 00:19:23 -0800756 while (i > 0) {
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200757 struct channel_subsystem *css;
758
Cornelia Hucka28c6942006-01-06 00:19:23 -0800759 i--;
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200760 css = channel_subsystems[i];
761 device_unregister(&css->pseudo_subchannel->dev);
Cornelia Huck495a5b42006-03-24 03:15:14 -0800762 if (css_characteristics_avail && css_chsc_characteristics.secm)
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200763 device_remove_file(&css->device,
Cornelia Huck495a5b42006-03-24 03:15:14 -0800764 &dev_attr_cm_enable);
Cornelia Huck7c9f4e32007-10-12 16:11:13 +0200765 device_unregister(&css->device);
Cornelia Hucka28c6942006-01-06 00:19:23 -0800766 }
Cornelia Huckfb6958a2006-01-06 00:19:25 -0800767out_bus:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 bus_unregister(&css_bus_type);
769out:
Cornelia Huck4434a382007-07-27 12:29:21 +0200770 chsc_free_sei_area();
771 kfree(slow_subchannel_set);
772 printk(KERN_WARNING"cio: failed to initialize css driver (%d)!\n",
773 ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774 return ret;
775}
776
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100777int sch_is_pseudo_sch(struct subchannel *sch)
778{
779 return sch == to_css(sch->dev.parent)->pseudo_subchannel;
780}
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782/*
783 * find a driver for a subchannel. They identify by the subchannel
784 * type with the exception that the console subchannel driver has its own
785 * subchannel type although the device is an i/o subchannel
786 */
787static int
788css_bus_match (struct device *dev, struct device_driver *drv)
789{
Cornelia Huck084325d2008-01-26 14:10:38 +0100790 struct subchannel *sch = to_subchannel(dev);
791 struct css_driver *driver = to_cssdriver(drv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793 if (sch->st == driver->subchannel_type)
794 return 1;
795
796 return 0;
797}
798
Cornelia Huck98c13c22008-01-26 14:10:40 +0100799static int css_probe(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +0100800{
801 struct subchannel *sch;
Cornelia Huck98c13c22008-01-26 14:10:40 +0100802 int ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +0100803
804 sch = to_subchannel(dev);
Cornelia Huck084325d2008-01-26 14:10:38 +0100805 sch->driver = to_cssdriver(dev->driver);
Cornelia Huck98c13c22008-01-26 14:10:40 +0100806 ret = sch->driver->probe ? sch->driver->probe(sch) : 0;
807 if (ret)
808 sch->driver = NULL;
809 return ret;
Cornelia Huck8bbace72006-01-11 10:56:22 +0100810}
811
Cornelia Huck98c13c22008-01-26 14:10:40 +0100812static int css_remove(struct device *dev)
813{
814 struct subchannel *sch;
815 int ret;
816
817 sch = to_subchannel(dev);
818 ret = sch->driver->remove ? sch->driver->remove(sch) : 0;
819 sch->driver = NULL;
820 return ret;
821}
822
823static void css_shutdown(struct device *dev)
Cornelia Huck8bbace72006-01-11 10:56:22 +0100824{
825 struct subchannel *sch;
826
827 sch = to_subchannel(dev);
Cornelia Huck98c13c22008-01-26 14:10:40 +0100828 if (sch->driver && sch->driver->shutdown)
Cornelia Huck8bbace72006-01-11 10:56:22 +0100829 sch->driver->shutdown(sch);
830}
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832struct bus_type css_bus_type = {
Cornelia Huck8bbace72006-01-11 10:56:22 +0100833 .name = "css",
834 .match = css_bus_match,
835 .probe = css_probe,
836 .remove = css_remove,
837 .shutdown = css_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838};
839
840subsys_initcall(init_channel_subsystem);
841
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842MODULE_LICENSE("GPL");
843EXPORT_SYMBOL(css_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844EXPORT_SYMBOL_GPL(css_characteristics_avail);