[S390] Cleanup cio printk messages.
[deliverable/linux.git] / drivers / s390 / cio / device.c
CommitLineData
1da177e4
LT
1/*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
1da177e4 4 *
c820de39 5 * Copyright IBM Corp. 2002,2008
1da177e4 6 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
4ce3b30c 7 * Cornelia Huck (cornelia.huck@de.ibm.com)
1da177e4
LT
8 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
1da177e4
LT
10#include <linux/module.h>
11#include <linux/init.h>
12#include <linux/spinlock.h>
13#include <linux/errno.h>
14#include <linux/err.h>
15#include <linux/slab.h>
16#include <linux/list.h>
17#include <linux/device.h>
18#include <linux/workqueue.h>
90ab1336 19#include <linux/timer.h>
1da177e4
LT
20
21#include <asm/ccwdev.h>
22#include <asm/cio.h>
4e57b681 23#include <asm/param.h> /* HZ */
1842f2b1 24#include <asm/cmb.h>
3a3fc29a 25#include <asm/isc.h>
1da177e4 26
0ae7a7b2 27#include "chp.h"
1da177e4 28#include "cio.h"
d7b5a4c9 29#include "cio_debug.h"
1da177e4
LT
30#include "css.h"
31#include "device.h"
32#include "ioasm.h"
cd6b4f27 33#include "io_sch.h"
1da177e4 34
90ab1336 35static struct timer_list recovery_timer;
486d0a00 36static DEFINE_SPINLOCK(recovery_lock);
90ab1336
PO
37static int recovery_phase;
38static const unsigned long recovery_delay[] = { 3, 30, 300 };
39
1da177e4
LT
40/******************* bus type handling ***********************/
41
42/* The Linux driver model distinguishes between a bus type and
43 * the bus itself. Of course we only have one channel
44 * subsystem driver and one channel system per machine, but
45 * we still use the abstraction. T.R. says it's a good idea. */
46static int
47ccw_bus_match (struct device * dev, struct device_driver * drv)
48{
49 struct ccw_device *cdev = to_ccwdev(dev);
50 struct ccw_driver *cdrv = to_ccwdrv(drv);
51 const struct ccw_device_id *ids = cdrv->ids, *found;
52
53 if (!ids)
54 return 0;
55
56 found = ccw_device_id_match(ids, &cdev->id);
57 if (!found)
58 return 0;
59
60 cdev->id.driver_info = found->driver_info;
61
62 return 1;
63}
64
db0c2d59
PO
65/* Store modalias string delimited by prefix/suffix string into buffer with
66 * specified size. Return length of resulting string (excluding trailing '\0')
67 * even if string doesn't fit buffer (snprintf semantics). */
cfbe9bb2 68static int snprint_alias(char *buf, size_t size,
db0c2d59 69 struct ccw_device_id *id, const char *suffix)
1da177e4 70{
db0c2d59 71 int len;
1da177e4 72
cfbe9bb2 73 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
db0c2d59
PO
74 if (len > size)
75 return len;
76 buf += len;
77 size -= len;
1da177e4 78
db0c2d59
PO
79 if (id->dev_type != 0)
80 len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
81 id->dev_model, suffix);
82 else
83 len += snprintf(buf, size, "dtdm%s", suffix);
1da177e4 84
db0c2d59
PO
85 return len;
86}
87
88/* Set up environment variables for ccw device uevent. Return 0 on success,
89 * non-zero otherwise. */
7eff2e7a 90static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
db0c2d59
PO
91{
92 struct ccw_device *cdev = to_ccwdev(dev);
93 struct ccw_device_id *id = &(cdev->id);
cfbe9bb2
CH
94 int ret;
95 char modalias_buf[30];
1da177e4 96
db0c2d59 97 /* CU_TYPE= */
7eff2e7a 98 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
cfbe9bb2
CH
99 if (ret)
100 return ret;
db0c2d59
PO
101
102 /* CU_MODEL= */
7eff2e7a 103 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
cfbe9bb2
CH
104 if (ret)
105 return ret;
1da177e4
LT
106
107 /* The next two can be zero, that's ok for us */
db0c2d59 108 /* DEV_TYPE= */
7eff2e7a 109 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
cfbe9bb2
CH
110 if (ret)
111 return ret;
1da177e4 112
db0c2d59 113 /* DEV_MODEL= */
7eff2e7a 114 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
cfbe9bb2
CH
115 if (ret)
116 return ret;
db0c2d59
PO
117
118 /* MODALIAS= */
cfbe9bb2 119 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
7eff2e7a
KS
120 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
121 return ret;
1da177e4
LT
122}
123
8bbace7e 124struct bus_type ccw_bus_type;
1da177e4 125
602b20f2
CH
126static void io_subchannel_irq(struct subchannel *);
127static int io_subchannel_probe(struct subchannel *);
128static int io_subchannel_remove(struct subchannel *);
8bbace7e 129static void io_subchannel_shutdown(struct subchannel *);
c820de39 130static int io_subchannel_sch_event(struct subchannel *, int);
99611f87
CH
131static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
132 int);
1da177e4 133
f08adc00
CH
134static struct css_device_id io_subchannel_ids[] = {
135 { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
136 { /* end of list */ },
137};
138MODULE_DEVICE_TABLE(css, io_subchannel_ids);
139
f7e5d67c 140static struct css_driver io_subchannel_driver = {
4beee646 141 .owner = THIS_MODULE,
f08adc00 142 .subchannel_type = io_subchannel_ids,
25b7bb58 143 .name = "io_subchannel",
1da177e4 144 .irq = io_subchannel_irq,
c820de39
CH
145 .sch_event = io_subchannel_sch_event,
146 .chp_event = io_subchannel_chp_event,
8bbace7e
CH
147 .probe = io_subchannel_probe,
148 .remove = io_subchannel_remove,
149 .shutdown = io_subchannel_shutdown,
1da177e4
LT
150};
151
152struct workqueue_struct *ccw_device_work;
153struct workqueue_struct *ccw_device_notify_work;
40154b82
PO
154wait_queue_head_t ccw_device_init_wq;
155atomic_t ccw_device_init_count;
1da177e4 156
90ab1336
PO
157static void recovery_func(unsigned long data);
158
1da177e4
LT
159static int __init
160init_ccw_bus_type (void)
161{
162 int ret;
163
164 init_waitqueue_head(&ccw_device_init_wq);
165 atomic_set(&ccw_device_init_count, 0);
90ab1336 166 setup_timer(&recovery_timer, recovery_func, 0);
1da177e4
LT
167
168 ccw_device_work = create_singlethread_workqueue("cio");
169 if (!ccw_device_work)
170 return -ENOMEM; /* FIXME: better errno ? */
171 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
172 if (!ccw_device_notify_work) {
173 ret = -ENOMEM; /* FIXME: better errno ? */
174 goto out_err;
175 }
176 slow_path_wq = create_singlethread_workqueue("kslowcrw");
177 if (!slow_path_wq) {
178 ret = -ENOMEM; /* FIXME: better errno ? */
179 goto out_err;
180 }
181 if ((ret = bus_register (&ccw_bus_type)))
182 goto out_err;
183
25b7bb58
CH
184 ret = css_driver_register(&io_subchannel_driver);
185 if (ret)
1da177e4
LT
186 goto out_err;
187
188 wait_event(ccw_device_init_wq,
189 atomic_read(&ccw_device_init_count) == 0);
190 flush_workqueue(ccw_device_work);
191 return 0;
192out_err:
193 if (ccw_device_work)
194 destroy_workqueue(ccw_device_work);
195 if (ccw_device_notify_work)
196 destroy_workqueue(ccw_device_notify_work);
197 if (slow_path_wq)
198 destroy_workqueue(slow_path_wq);
199 return ret;
200}
201
202static void __exit
203cleanup_ccw_bus_type (void)
204{
25b7bb58 205 css_driver_unregister(&io_subchannel_driver);
1da177e4
LT
206 bus_unregister(&ccw_bus_type);
207 destroy_workqueue(ccw_device_notify_work);
208 destroy_workqueue(ccw_device_work);
209}
210
211subsys_initcall(init_ccw_bus_type);
212module_exit(cleanup_ccw_bus_type);
213
214/************************ device handling **************************/
215
216/*
217 * A ccw_device has some interfaces in sysfs in addition to the
218 * standard ones.
219 * The following entries are designed to export the information which
220 * resided in 2.4 in /proc/subchannels. Subchannel and device number
221 * are obvious, so they don't have an entry :)
222 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
223 */
224static ssize_t
3fd3c0a5 225chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
1da177e4
LT
226{
227 struct subchannel *sch = to_subchannel(dev);
7ad6a249 228 struct chsc_ssd_info *ssd = &sch->ssd_info;
1da177e4
LT
229 ssize_t ret = 0;
230 int chp;
7ad6a249
PO
231 int mask;
232
233 for (chp = 0; chp < 8; chp++) {
234 mask = 0x80 >> chp;
235 if (ssd->path_mask & mask)
236 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
237 else
238 ret += sprintf(buf + ret, "00 ");
239 }
1da177e4
LT
240 ret += sprintf (buf+ret, "\n");
241 return min((ssize_t)PAGE_SIZE, ret);
242}
243
244static ssize_t
3fd3c0a5 245pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
1da177e4
LT
246{
247 struct subchannel *sch = to_subchannel(dev);
248 struct pmcw *pmcw = &sch->schib.pmcw;
249
250 return sprintf (buf, "%02x %02x %02x\n",
251 pmcw->pim, pmcw->pam, pmcw->pom);
252}
253
254static ssize_t
3fd3c0a5 255devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
256{
257 struct ccw_device *cdev = to_ccwdev(dev);
258 struct ccw_device_id *id = &(cdev->id);
259
260 if (id->dev_type != 0)
261 return sprintf(buf, "%04x/%02x\n",
262 id->dev_type, id->dev_model);
263 else
264 return sprintf(buf, "n/a\n");
265}
266
267static ssize_t
3fd3c0a5 268cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
269{
270 struct ccw_device *cdev = to_ccwdev(dev);
271 struct ccw_device_id *id = &(cdev->id);
272
273 return sprintf(buf, "%04x/%02x\n",
274 id->cu_type, id->cu_model);
275}
276
f1fc78a8
BB
277static ssize_t
278modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
279{
280 struct ccw_device *cdev = to_ccwdev(dev);
281 struct ccw_device_id *id = &(cdev->id);
db0c2d59 282 int len;
f1fc78a8 283
086a6c62 284 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
db0c2d59
PO
285
286 return len > PAGE_SIZE ? PAGE_SIZE : len;
f1fc78a8
BB
287}
288
1da177e4 289static ssize_t
3fd3c0a5 290online_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
291{
292 struct ccw_device *cdev = to_ccwdev(dev);
293
294 return sprintf(buf, cdev->online ? "1\n" : "0\n");
295}
296
d7b5a4c9
CH
297int ccw_device_is_orphan(struct ccw_device *cdev)
298{
299 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
300}
301
ef99516c 302static void ccw_device_unregister(struct ccw_device *cdev)
7674da77 303{
7674da77 304 if (test_and_clear_bit(1, &cdev->private->registered))
ef99516c 305 device_del(&cdev->dev);
7674da77
CH
306}
307
59a8a6e2
CH
308static void ccw_device_remove_orphan_cb(struct device *dev)
309{
310 struct ccw_device *cdev = to_ccwdev(dev);
311
312 ccw_device_unregister(cdev);
313 put_device(&cdev->dev);
314}
315
316static void ccw_device_remove_sch_cb(struct device *dev)
317{
318 struct subchannel *sch;
319
320 sch = to_subchannel(dev);
321 css_sch_device_unregister(sch);
322 /* Reset intparm to zeroes. */
323 sch->schib.pmcw.intparm = 0;
324 cio_modify(sch);
325 put_device(&sch->dev);
326}
327
1da177e4
LT
328static void
329ccw_device_remove_disconnected(struct ccw_device *cdev)
330{
d7b5a4c9 331 unsigned long flags;
59a8a6e2
CH
332 int rc;
333
1da177e4
LT
334 /*
335 * Forced offline in disconnected state means
336 * 'throw away device'.
337 */
d7b5a4c9 338 if (ccw_device_is_orphan(cdev)) {
59a8a6e2
CH
339 /*
340 * Deregister ccw device.
341 * Unfortunately, we cannot do this directly from the
342 * attribute method.
343 */
d7b5a4c9
CH
344 spin_lock_irqsave(cdev->ccwlock, flags);
345 cdev->private->state = DEV_STATE_NOT_OPER;
346 spin_unlock_irqrestore(cdev->ccwlock, flags);
59a8a6e2
CH
347 rc = device_schedule_callback(&cdev->dev,
348 ccw_device_remove_orphan_cb);
349 if (rc)
139b83dd 350 CIO_MSG_EVENT(0, "Couldn't unregister orphan "
e556bbbd
CH
351 "0.%x.%04x\n",
352 cdev->private->dev_id.ssid,
353 cdev->private->dev_id.devno);
59a8a6e2 354 return;
d7b5a4c9 355 }
59a8a6e2
CH
356 /* Deregister subchannel, which will kill the ccw device. */
357 rc = device_schedule_callback(cdev->dev.parent,
358 ccw_device_remove_sch_cb);
359 if (rc)
139b83dd 360 CIO_MSG_EVENT(0, "Couldn't unregister disconnected device "
e556bbbd
CH
361 "0.%x.%04x\n",
362 cdev->private->dev_id.ssid,
363 cdev->private->dev_id.devno);
1da177e4
LT
364}
365
b2ffd8e9
CH
366/**
367 * ccw_device_set_offline() - disable a ccw device for I/O
368 * @cdev: target ccw device
369 *
370 * This function calls the driver's set_offline() function for @cdev, if
371 * given, and then disables @cdev.
372 * Returns:
373 * %0 on success and a negative error value on failure.
374 * Context:
375 * enabled, ccw device lock not held
376 */
377int ccw_device_set_offline(struct ccw_device *cdev)
1da177e4
LT
378{
379 int ret;
380
381 if (!cdev)
382 return -ENODEV;
383 if (!cdev->online || !cdev->drv)
384 return -EINVAL;
385
386 if (cdev->drv->set_offline) {
387 ret = cdev->drv->set_offline(cdev);
388 if (ret != 0)
389 return ret;
390 }
391 cdev->online = 0;
392 spin_lock_irq(cdev->ccwlock);
393 ret = ccw_device_offline(cdev);
394 if (ret == -ENODEV) {
395 if (cdev->private->state != DEV_STATE_NOT_OPER) {
396 cdev->private->state = DEV_STATE_OFFLINE;
397 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
398 }
399 spin_unlock_irq(cdev->ccwlock);
400 return ret;
401 }
402 spin_unlock_irq(cdev->ccwlock);
403 if (ret == 0)
404 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
405 else {
139b83dd 406 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
e556bbbd
CH
407 "device 0.%x.%04x\n",
408 ret, cdev->private->dev_id.ssid,
409 cdev->private->dev_id.devno);
1da177e4
LT
410 cdev->online = 1;
411 }
412 return ret;
413}
414
b2ffd8e9
CH
415/**
416 * ccw_device_set_online() - enable a ccw device for I/O
417 * @cdev: target ccw device
418 *
419 * This function first enables @cdev and then calls the driver's set_online()
420 * function for @cdev, if given. If set_online() returns an error, @cdev is
421 * disabled again.
422 * Returns:
423 * %0 on success and a negative error value on failure.
424 * Context:
425 * enabled, ccw device lock not held
426 */
427int ccw_device_set_online(struct ccw_device *cdev)
1da177e4
LT
428{
429 int ret;
430
431 if (!cdev)
432 return -ENODEV;
433 if (cdev->online || !cdev->drv)
434 return -EINVAL;
435
436 spin_lock_irq(cdev->ccwlock);
437 ret = ccw_device_online(cdev);
438 spin_unlock_irq(cdev->ccwlock);
439 if (ret == 0)
440 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
441 else {
139b83dd 442 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
e556bbbd
CH
443 "device 0.%x.%04x\n",
444 ret, cdev->private->dev_id.ssid,
445 cdev->private->dev_id.devno);
1da177e4
LT
446 return ret;
447 }
448 if (cdev->private->state != DEV_STATE_ONLINE)
449 return -ENODEV;
450 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
451 cdev->online = 1;
452 return 0;
453 }
454 spin_lock_irq(cdev->ccwlock);
455 ret = ccw_device_offline(cdev);
456 spin_unlock_irq(cdev->ccwlock);
457 if (ret == 0)
458 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
e556bbbd 459 else
139b83dd 460 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
e556bbbd
CH
461 "device 0.%x.%04x\n",
462 ret, cdev->private->dev_id.ssid,
463 cdev->private->dev_id.devno);
6cadb78b 464 return (ret == 0) ? -ENODEV : ret;
1da177e4
LT
465}
466
f5ba6c86
CH
467static void online_store_handle_offline(struct ccw_device *cdev)
468{
469 if (cdev->private->state == DEV_STATE_DISCONNECTED)
470 ccw_device_remove_disconnected(cdev);
471 else if (cdev->drv && cdev->drv->set_offline)
472 ccw_device_set_offline(cdev);
473}
474
475static int online_store_recog_and_online(struct ccw_device *cdev)
476{
477 int ret;
478
479 /* Do device recognition, if needed. */
480 if (cdev->id.cu_type == 0) {
481 ret = ccw_device_recognition(cdev);
482 if (ret) {
e556bbbd
CH
483 CIO_MSG_EVENT(0, "Couldn't start recognition "
484 "for device 0.%x.%04x (ret=%d)\n",
485 cdev->private->dev_id.ssid,
486 cdev->private->dev_id.devno, ret);
f5ba6c86
CH
487 return ret;
488 }
489 wait_event(cdev->private->wait_q,
490 cdev->private->flags.recog_done);
491 }
492 if (cdev->drv && cdev->drv->set_online)
493 ccw_device_set_online(cdev);
494 return 0;
495}
c78aa6cb 496static int online_store_handle_online(struct ccw_device *cdev, int force)
f5ba6c86
CH
497{
498 int ret;
499
500 ret = online_store_recog_and_online(cdev);
501 if (ret)
c78aa6cb 502 return ret;
f5ba6c86
CH
503 if (force && cdev->private->state == DEV_STATE_BOXED) {
504 ret = ccw_device_stlck(cdev);
c78aa6cb
ME
505 if (ret)
506 return ret;
f5ba6c86
CH
507 if (cdev->id.cu_type == 0)
508 cdev->private->state = DEV_STATE_NOT_OPER;
509 online_store_recog_and_online(cdev);
510 }
c78aa6cb 511 return 0;
f5ba6c86
CH
512}
513
514static ssize_t online_store (struct device *dev, struct device_attribute *attr,
515 const char *buf, size_t count)
1da177e4
LT
516{
517 struct ccw_device *cdev = to_ccwdev(dev);
2f972202
CH
518 int force, ret;
519 unsigned long i;
1da177e4 520
973bd993 521 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
1da177e4
LT
522 return -EAGAIN;
523
524 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
525 atomic_set(&cdev->private->onoff, 0);
526 return -EINVAL;
527 }
528 if (!strncmp(buf, "force\n", count)) {
529 force = 1;
530 i = 1;
2f972202 531 ret = 0;
1da177e4
LT
532 } else {
533 force = 0;
2f972202 534 ret = strict_strtoul(buf, 16, &i);
1da177e4 535 }
2f972202
CH
536 if (ret)
537 goto out;
f5ba6c86
CH
538 switch (i) {
539 case 0:
540 online_store_handle_offline(cdev);
2f972202 541 ret = count;
f5ba6c86
CH
542 break;
543 case 1:
c78aa6cb
ME
544 ret = online_store_handle_online(cdev, force);
545 if (!ret)
546 ret = count;
f5ba6c86
CH
547 break;
548 default:
2f972202 549 ret = -EINVAL;
1da177e4 550 }
2f972202 551out:
1da177e4
LT
552 if (cdev->drv)
553 module_put(cdev->drv->owner);
554 atomic_set(&cdev->private->onoff, 0);
2f972202 555 return ret;
1da177e4
LT
556}
557
558static ssize_t
3fd3c0a5 559available_show (struct device *dev, struct device_attribute *attr, char *buf)
1da177e4
LT
560{
561 struct ccw_device *cdev = to_ccwdev(dev);
562 struct subchannel *sch;
563
d7b5a4c9
CH
564 if (ccw_device_is_orphan(cdev))
565 return sprintf(buf, "no device\n");
1da177e4
LT
566 switch (cdev->private->state) {
567 case DEV_STATE_BOXED:
568 return sprintf(buf, "boxed\n");
569 case DEV_STATE_DISCONNECTED:
570 case DEV_STATE_DISCONNECTED_SENSE_ID:
571 case DEV_STATE_NOT_OPER:
572 sch = to_subchannel(dev->parent);
573 if (!sch->lpm)
574 return sprintf(buf, "no path\n");
575 else
576 return sprintf(buf, "no device\n");
577 default:
578 /* All other states considered fine. */
579 return sprintf(buf, "good\n");
580 }
581}
582
583static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
584static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
585static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
586static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
f1fc78a8 587static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
1da177e4 588static DEVICE_ATTR(online, 0644, online_show, online_store);
1da177e4
LT
589static DEVICE_ATTR(availability, 0444, available_show, NULL);
590
7e9db9ea 591static struct attribute *io_subchannel_attrs[] = {
1da177e4
LT
592 &dev_attr_chpids.attr,
593 &dev_attr_pimpampom.attr,
594 NULL,
595};
596
7e9db9ea
CH
597static struct attribute_group io_subchannel_attr_group = {
598 .attrs = io_subchannel_attrs,
529192f3 599};
1da177e4
LT
600
601static struct attribute * ccwdev_attrs[] = {
602 &dev_attr_devtype.attr,
603 &dev_attr_cutype.attr,
f1fc78a8 604 &dev_attr_modalias.attr,
1da177e4
LT
605 &dev_attr_online.attr,
606 &dev_attr_cmb_enable.attr,
607 &dev_attr_availability.attr,
608 NULL,
609};
610
611static struct attribute_group ccwdev_attr_group = {
612 .attrs = ccwdev_attrs,
613};
614
f7e5d67c 615static struct attribute_group *ccwdev_attr_groups[] = {
ef99516c
CH
616 &ccwdev_attr_group,
617 NULL,
618};
1da177e4
LT
619
620/* this is a simple abstraction for device_register that sets the
621 * correct bus type and adds the bus specific files */
3c9da7ba 622static int ccw_device_register(struct ccw_device *cdev)
1da177e4
LT
623{
624 struct device *dev = &cdev->dev;
625 int ret;
626
627 dev->bus = &ccw_bus_type;
628
629 if ((ret = device_add(dev)))
630 return ret;
631
632 set_bit(1, &cdev->private->registered);
1da177e4
LT
633 return ret;
634}
635
b0744bd2 636struct match_data {
78964268 637 struct ccw_dev_id dev_id;
b0744bd2
CH
638 struct ccw_device * sibling;
639};
640
641static int
642match_devno(struct device * dev, void * data)
643{
78964268 644 struct match_data * d = data;
b0744bd2
CH
645 struct ccw_device * cdev;
646
647 cdev = to_ccwdev(dev);
648 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
d7b5a4c9 649 !ccw_device_is_orphan(cdev) &&
78964268 650 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
d7b5a4c9 651 (cdev != d->sibling))
b0744bd2 652 return 1;
b0744bd2
CH
653 return 0;
654}
655
78964268
CH
656static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
657 struct ccw_device *sibling)
1da177e4 658{
1da177e4 659 struct device *dev;
292888c8 660 struct match_data data;
1da177e4 661
78964268 662 data.dev_id = *dev_id;
292888c8 663 data.sibling = sibling;
e5945b4f 664 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
1da177e4 665
b0744bd2 666 return dev ? to_ccwdev(dev) : NULL;
1da177e4
LT
667}
668
d7b5a4c9
CH
669static int match_orphan(struct device *dev, void *data)
670{
671 struct ccw_dev_id *dev_id;
672 struct ccw_device *cdev;
673
674 dev_id = data;
675 cdev = to_ccwdev(dev);
676 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
677}
678
679static struct ccw_device *
680get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
681 struct ccw_dev_id *dev_id)
682{
683 struct device *dev;
684
685 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
686 match_orphan);
687
688 return dev ? to_ccwdev(dev) : NULL;
689}
690
1da177e4 691static void
c1637532 692ccw_device_add_changed(struct work_struct *work)
1da177e4 693{
c1637532 694 struct ccw_device_private *priv;
1da177e4
LT
695 struct ccw_device *cdev;
696
c1637532
MS
697 priv = container_of(work, struct ccw_device_private, kick_work);
698 cdev = priv->cdev;
1da177e4
LT
699 if (device_add(&cdev->dev)) {
700 put_device(&cdev->dev);
701 return;
702 }
703 set_bit(1, &cdev->private->registered);
1da177e4
LT
704}
705
d7b5a4c9 706void ccw_device_do_unreg_rereg(struct work_struct *work)
1da177e4 707{
c1637532 708 struct ccw_device_private *priv;
1da177e4
LT
709 struct ccw_device *cdev;
710 struct subchannel *sch;
1da177e4 711
c1637532
MS
712 priv = container_of(work, struct ccw_device_private, kick_work);
713 cdev = priv->cdev;
1da177e4 714 sch = to_subchannel(cdev->dev.parent);
d7b5a4c9 715
ef99516c 716 ccw_device_unregister(cdev);
1da177e4 717 PREPARE_WORK(&cdev->private->kick_work,
c1637532 718 ccw_device_add_changed);
1da177e4
LT
719 queue_work(ccw_device_work, &cdev->private->kick_work);
720}
721
722static void
723ccw_device_release(struct device *dev)
724{
725 struct ccw_device *cdev;
726
727 cdev = to_ccwdev(dev);
728 kfree(cdev->private);
729 kfree(cdev);
730}
731
7674da77
CH
732static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
733{
734 struct ccw_device *cdev;
735
736 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
737 if (cdev) {
738 cdev->private = kzalloc(sizeof(struct ccw_device_private),
739 GFP_KERNEL | GFP_DMA);
740 if (cdev->private)
741 return cdev;
742 }
743 kfree(cdev);
744 return ERR_PTR(-ENOMEM);
745}
746
747static int io_subchannel_initialize_dev(struct subchannel *sch,
748 struct ccw_device *cdev)
749{
750 cdev->private->cdev = cdev;
751 atomic_set(&cdev->private->onoff, 0);
752 cdev->dev.parent = &sch->dev;
753 cdev->dev.release = ccw_device_release;
33583c36 754 INIT_WORK(&cdev->private->kick_work, NULL);
ef99516c 755 cdev->dev.groups = ccwdev_attr_groups;
7674da77
CH
756 /* Do first half of device_register. */
757 device_initialize(&cdev->dev);
758 if (!get_device(&sch->dev)) {
759 if (cdev->dev.release)
760 cdev->dev.release(&cdev->dev);
761 return -ENODEV;
762 }
763 return 0;
764}
765
766static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
767{
768 struct ccw_device *cdev;
769 int ret;
770
771 cdev = io_subchannel_allocate_dev(sch);
772 if (!IS_ERR(cdev)) {
773 ret = io_subchannel_initialize_dev(sch, cdev);
774 if (ret) {
775 kfree(cdev);
776 cdev = ERR_PTR(ret);
777 }
778 }
779 return cdev;
780}
781
d7b5a4c9
CH
782static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
783
784static void sch_attach_device(struct subchannel *sch,
785 struct ccw_device *cdev)
786{
82b7ac05 787 css_update_ssd_info(sch);
d7b5a4c9 788 spin_lock_irq(sch->lock);
db6a6423 789 sch_set_cdev(sch, cdev);
d7b5a4c9
CH
790 cdev->private->schid = sch->schid;
791 cdev->ccwlock = sch->lock;
c820de39 792 ccw_device_trigger_reprobe(cdev);
d7b5a4c9
CH
793 spin_unlock_irq(sch->lock);
794}
795
796static void sch_attach_disconnected_device(struct subchannel *sch,
797 struct ccw_device *cdev)
798{
799 struct subchannel *other_sch;
800 int ret;
801
802 other_sch = to_subchannel(get_device(cdev->dev.parent));
803 ret = device_move(&cdev->dev, &sch->dev);
804 if (ret) {
139b83dd 805 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
d7b5a4c9
CH
806 "(ret=%d)!\n", cdev->private->dev_id.ssid,
807 cdev->private->dev_id.devno, ret);
808 put_device(&other_sch->dev);
809 return;
810 }
db6a6423 811 sch_set_cdev(other_sch, NULL);
d7b5a4c9
CH
812 /* No need to keep a subchannel without ccw device around. */
813 css_sch_device_unregister(other_sch);
814 put_device(&other_sch->dev);
815 sch_attach_device(sch, cdev);
816}
817
818static void sch_attach_orphaned_device(struct subchannel *sch,
819 struct ccw_device *cdev)
820{
821 int ret;
822
823 /* Try to move the ccw device to its new subchannel. */
824 ret = device_move(&cdev->dev, &sch->dev);
825 if (ret) {
826 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
827 "failed (ret=%d)!\n",
828 cdev->private->dev_id.ssid,
829 cdev->private->dev_id.devno, ret);
830 return;
831 }
832 sch_attach_device(sch, cdev);
833}
834
835static void sch_create_and_recog_new_device(struct subchannel *sch)
836{
837 struct ccw_device *cdev;
838
839 /* Need to allocate a new ccw device. */
840 cdev = io_subchannel_create_ccwdev(sch);
841 if (IS_ERR(cdev)) {
842 /* OK, we did everything we could... */
843 css_sch_device_unregister(sch);
844 return;
845 }
846 spin_lock_irq(sch->lock);
db6a6423 847 sch_set_cdev(sch, cdev);
d7b5a4c9
CH
848 spin_unlock_irq(sch->lock);
849 /* Start recognition for the new ccw device. */
850 if (io_subchannel_recog(cdev, sch)) {
851 spin_lock_irq(sch->lock);
db6a6423 852 sch_set_cdev(sch, NULL);
d7b5a4c9
CH
853 spin_unlock_irq(sch->lock);
854 if (cdev->dev.release)
855 cdev->dev.release(&cdev->dev);
856 css_sch_device_unregister(sch);
857 }
858}
859
860
861void ccw_device_move_to_orphanage(struct work_struct *work)
862{
863 struct ccw_device_private *priv;
864 struct ccw_device *cdev;
865 struct ccw_device *replacing_cdev;
866 struct subchannel *sch;
867 int ret;
868 struct channel_subsystem *css;
869 struct ccw_dev_id dev_id;
870
871 priv = container_of(work, struct ccw_device_private, kick_work);
872 cdev = priv->cdev;
873 sch = to_subchannel(cdev->dev.parent);
874 css = to_css(sch->dev.parent);
875 dev_id.devno = sch->schib.pmcw.dev;
876 dev_id.ssid = sch->schid.ssid;
877
878 /*
879 * Move the orphaned ccw device to the orphanage so the replacing
880 * ccw device can take its place on the subchannel.
881 */
882 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
883 if (ret) {
884 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
885 "(ret=%d)!\n", cdev->private->dev_id.ssid,
886 cdev->private->dev_id.devno, ret);
887 return;
888 }
889 cdev->ccwlock = css->pseudo_subchannel->lock;
890 /*
891 * Search for the replacing ccw device
892 * - among the disconnected devices
893 * - in the orphanage
894 */
895 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
896 if (replacing_cdev) {
897 sch_attach_disconnected_device(sch, replacing_cdev);
898 return;
899 }
900 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
901 if (replacing_cdev) {
902 sch_attach_orphaned_device(sch, replacing_cdev);
903 return;
904 }
905 sch_create_and_recog_new_device(sch);
906}
907
1da177e4
LT
908/*
909 * Register recognized device.
910 */
911static void
c1637532 912io_subchannel_register(struct work_struct *work)
1da177e4 913{
c1637532 914 struct ccw_device_private *priv;
1da177e4
LT
915 struct ccw_device *cdev;
916 struct subchannel *sch;
917 int ret;
918 unsigned long flags;
919
c1637532
MS
920 priv = container_of(work, struct ccw_device_private, kick_work);
921 cdev = priv->cdev;
1da177e4 922 sch = to_subchannel(cdev->dev.parent);
82b7ac05 923 css_update_ssd_info(sch);
47af5518
CH
924 /*
925 * io_subchannel_register() will also be called after device
926 * recognition has been done for a boxed device (which will already
927 * be registered). We need to reprobe since we may now have sense id
928 * information.
929 */
b0744bd2 930 if (klist_node_attached(&cdev->dev.knode_parent)) {
47af5518
CH
931 if (!cdev->drv) {
932 ret = device_reprobe(&cdev->dev);
933 if (ret)
934 /* We can't do much here. */
139b83dd 935 CIO_MSG_EVENT(0, "device_reprobe() returned"
e556bbbd
CH
936 " %d for 0.%x.%04x\n", ret,
937 cdev->private->dev_id.ssid,
938 cdev->private->dev_id.devno);
47af5518 939 }
1da177e4
LT
940 goto out;
941 }
fa1a8c23
CH
942 /*
943 * Now we know this subchannel will stay, we can throw
944 * our delayed uevent.
945 */
946 sch->dev.uevent_suppress = 0;
947 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1da177e4
LT
948 /* make it known to the system */
949 ret = ccw_device_register(cdev);
950 if (ret) {
e556bbbd
CH
951 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
952 cdev->private->dev_id.ssid,
953 cdev->private->dev_id.devno, ret);
1da177e4 954 put_device(&cdev->dev);
2ec22984 955 spin_lock_irqsave(sch->lock, flags);
db6a6423 956 sch_set_cdev(sch, NULL);
2ec22984 957 spin_unlock_irqrestore(sch->lock, flags);
1da177e4
LT
958 kfree (cdev->private);
959 kfree (cdev);
960 put_device(&sch->dev);
961 if (atomic_dec_and_test(&ccw_device_init_count))
962 wake_up(&ccw_device_init_wq);
963 return;
964 }
1da177e4
LT
965 put_device(&cdev->dev);
966out:
967 cdev->private->flags.recog_done = 1;
968 put_device(&sch->dev);
969 wake_up(&cdev->private->wait_q);
970 if (atomic_dec_and_test(&ccw_device_init_count))
971 wake_up(&ccw_device_init_wq);
972}
973
3f4cf6e7 974static void ccw_device_call_sch_unregister(struct work_struct *work)
1da177e4 975{
c1637532
MS
976 struct ccw_device_private *priv;
977 struct ccw_device *cdev;
1da177e4
LT
978 struct subchannel *sch;
979
c1637532
MS
980 priv = container_of(work, struct ccw_device_private, kick_work);
981 cdev = priv->cdev;
1da177e4 982 sch = to_subchannel(cdev->dev.parent);
6ab4879a 983 css_sch_device_unregister(sch);
1da177e4
LT
984 /* Reset intparm to zeroes. */
985 sch->schib.pmcw.intparm = 0;
986 cio_modify(sch);
987 put_device(&cdev->dev);
988 put_device(&sch->dev);
989}
990
991/*
992 * subchannel recognition done. Called from the state machine.
993 */
994void
995io_subchannel_recog_done(struct ccw_device *cdev)
996{
997 struct subchannel *sch;
998
999 if (css_init_done == 0) {
1000 cdev->private->flags.recog_done = 1;
1001 return;
1002 }
1003 switch (cdev->private->state) {
1004 case DEV_STATE_NOT_OPER:
1005 cdev->private->flags.recog_done = 1;
1006 /* Remove device found not operational. */
1007 if (!get_device(&cdev->dev))
1008 break;
1009 sch = to_subchannel(cdev->dev.parent);
1010 PREPARE_WORK(&cdev->private->kick_work,
c1637532 1011 ccw_device_call_sch_unregister);
1da177e4
LT
1012 queue_work(slow_path_wq, &cdev->private->kick_work);
1013 if (atomic_dec_and_test(&ccw_device_init_count))
1014 wake_up(&ccw_device_init_wq);
1015 break;
1016 case DEV_STATE_BOXED:
1017 /* Device did not respond in time. */
1018 case DEV_STATE_OFFLINE:
1019 /*
1020 * We can't register the device in interrupt context so
1021 * we schedule a work item.
1022 */
1023 if (!get_device(&cdev->dev))
1024 break;
1025 PREPARE_WORK(&cdev->private->kick_work,
c1637532 1026 io_subchannel_register);
1da177e4
LT
1027 queue_work(slow_path_wq, &cdev->private->kick_work);
1028 break;
1029 }
1030}
1031
1032static int
1033io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1034{
1035 int rc;
1036 struct ccw_device_private *priv;
1037
db6a6423 1038 sch_set_cdev(sch, cdev);
2ec22984 1039 cdev->ccwlock = sch->lock;
fb6958a5 1040
1da177e4
LT
1041 /* Init private data. */
1042 priv = cdev->private;
78964268
CH
1043 priv->dev_id.devno = sch->schib.pmcw.dev;
1044 priv->dev_id.ssid = sch->schid.ssid;
1045 priv->schid = sch->schid;
1da177e4
LT
1046 priv->state = DEV_STATE_NOT_OPER;
1047 INIT_LIST_HEAD(&priv->cmb_list);
1048 init_waitqueue_head(&priv->wait_q);
1049 init_timer(&priv->timer);
1050
1051 /* Set an initial name for the device. */
fb6958a5
CH
1052 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1053 sch->schid.ssid, sch->schib.pmcw.dev);
1da177e4
LT
1054
1055 /* Increase counter of devices currently in recognition. */
1056 atomic_inc(&ccw_device_init_count);
1057
1058 /* Start async. device sensing. */
2ec22984 1059 spin_lock_irq(sch->lock);
1da177e4 1060 rc = ccw_device_recognition(cdev);
2ec22984 1061 spin_unlock_irq(sch->lock);
1da177e4
LT
1062 if (rc) {
1063 if (atomic_dec_and_test(&ccw_device_init_count))
1064 wake_up(&ccw_device_init_wq);
1065 }
1066 return rc;
1067}
1068
d7b5a4c9
CH
1069static void ccw_device_move_to_sch(struct work_struct *work)
1070{
1071 struct ccw_device_private *priv;
1072 int rc;
1073 struct subchannel *sch;
1074 struct ccw_device *cdev;
1075 struct subchannel *former_parent;
1076
1077 priv = container_of(work, struct ccw_device_private, kick_work);
1078 sch = priv->sch;
1079 cdev = priv->cdev;
1080 former_parent = ccw_device_is_orphan(cdev) ?
1081 NULL : to_subchannel(get_device(cdev->dev.parent));
1082 mutex_lock(&sch->reg_mutex);
1083 /* Try to move the ccw device to its new subchannel. */
1084 rc = device_move(&cdev->dev, &sch->dev);
1085 mutex_unlock(&sch->reg_mutex);
1086 if (rc) {
139b83dd 1087 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
d7b5a4c9
CH
1088 "0.%x.%04x failed (ret=%d)!\n",
1089 cdev->private->dev_id.ssid,
1090 cdev->private->dev_id.devno, sch->schid.ssid,
1091 sch->schid.sch_no, rc);
1092 css_sch_device_unregister(sch);
1093 goto out;
1094 }
1095 if (former_parent) {
1096 spin_lock_irq(former_parent->lock);
db6a6423 1097 sch_set_cdev(former_parent, NULL);
d7b5a4c9
CH
1098 spin_unlock_irq(former_parent->lock);
1099 css_sch_device_unregister(former_parent);
1100 /* Reset intparm to zeroes. */
1101 former_parent->schib.pmcw.intparm = 0;
1102 cio_modify(former_parent);
1103 }
1104 sch_attach_device(sch, cdev);
1105out:
1106 if (former_parent)
1107 put_device(&former_parent->dev);
1108 put_device(&cdev->dev);
1109}
1110
602b20f2
CH
1111static void io_subchannel_irq(struct subchannel *sch)
1112{
1113 struct ccw_device *cdev;
1114
db6a6423 1115 cdev = sch_get_cdev(sch);
602b20f2
CH
1116
1117 CIO_TRACE_EVENT(3, "IRQ");
1118 CIO_TRACE_EVENT(3, sch->dev.bus_id);
1119 if (cdev)
1120 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1121}
1122
0ae7a7b2
CH
1123static void io_subchannel_init_fields(struct subchannel *sch)
1124{
1125 if (cio_is_console(sch->schid))
1126 sch->opm = 0xff;
1127 else
1128 sch->opm = chp_get_sch_opm(sch);
1129 sch->lpm = sch->schib.pmcw.pam & sch->opm;
3a3fc29a 1130 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
0ae7a7b2
CH
1131
1132 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1133 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1134 sch->schib.pmcw.dev, sch->schid.ssid,
1135 sch->schid.sch_no, sch->schib.pmcw.pim,
1136 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1137 /* Initially set up some fields in the pmcw. */
1138 sch->schib.pmcw.ena = 0;
1139 sch->schib.pmcw.csense = 1; /* concurrent sense */
1140 if ((sch->lpm & (sch->lpm - 1)) != 0)
1141 sch->schib.pmcw.mp = 1; /* multipath mode */
1142 /* clean up possible residual cmf stuff */
1143 sch->schib.pmcw.mme = 0;
1144 sch->schib.pmcw.mbfc = 0;
1145 sch->schib.pmcw.mbi = 0;
1146 sch->schib.mba = 0;
1147}
1148
1149static int io_subchannel_probe(struct subchannel *sch)
1da177e4 1150{
1da177e4
LT
1151 struct ccw_device *cdev;
1152 int rc;
1153 unsigned long flags;
d7b5a4c9 1154 struct ccw_dev_id dev_id;
1da177e4 1155
db6a6423
CH
1156 cdev = sch_get_cdev(sch);
1157 if (cdev) {
7e9db9ea
CH
1158 rc = sysfs_create_group(&sch->dev.kobj,
1159 &io_subchannel_attr_group);
1160 if (rc)
1161 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1162 "attributes for subchannel "
1163 "0.%x.%04x (rc=%d)\n",
1164 sch->schid.ssid, sch->schid.sch_no, rc);
1da177e4
LT
1165 /*
1166 * This subchannel already has an associated ccw_device.
7e9db9ea
CH
1167 * Throw the delayed uevent for the subchannel, register
1168 * the ccw_device and exit. This happens for all early
1169 * devices, e.g. the console.
1da177e4 1170 */
7e9db9ea
CH
1171 sch->dev.uevent_suppress = 0;
1172 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
e1031786 1173 cdev->dev.groups = ccwdev_attr_groups;
1da177e4
LT
1174 device_initialize(&cdev->dev);
1175 ccw_device_register(cdev);
1da177e4
LT
1176 /*
1177 * Check if the device is already online. If it is
1178 * the reference count needs to be corrected
1179 * (see ccw_device_online and css_init_done for the
1180 * ugly details).
1181 */
1182 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1183 cdev->private->state != DEV_STATE_OFFLINE &&
1184 cdev->private->state != DEV_STATE_BOXED)
1185 get_device(&cdev->dev);
1186 return 0;
1187 }
0ae7a7b2 1188 io_subchannel_init_fields(sch);
d7b5a4c9
CH
1189 /*
1190 * First check if a fitting device may be found amongst the
1191 * disconnected devices or in the orphanage.
1192 */
1193 dev_id.devno = sch->schib.pmcw.dev;
1194 dev_id.ssid = sch->schid.ssid;
7e9db9ea
CH
1195 rc = sysfs_create_group(&sch->dev.kobj,
1196 &io_subchannel_attr_group);
1197 if (rc)
1198 return rc;
cd6b4f27
CH
1199 /* Allocate I/O subchannel private data. */
1200 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1201 GFP_KERNEL | GFP_DMA);
7e9db9ea
CH
1202 if (!sch->private) {
1203 rc = -ENOMEM;
1204 goto out_err;
1205 }
d7b5a4c9
CH
1206 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1207 if (!cdev)
1208 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1209 &dev_id);
1210 if (cdev) {
1211 /*
1212 * Schedule moving the device until when we have a registered
1213 * subchannel to move to and succeed the probe. We can
1214 * unregister later again, when the probe is through.
1215 */
1216 cdev->private->sch = sch;
1217 PREPARE_WORK(&cdev->private->kick_work,
1218 ccw_device_move_to_sch);
1219 queue_work(slow_path_wq, &cdev->private->kick_work);
1220 return 0;
1221 }
7674da77 1222 cdev = io_subchannel_create_ccwdev(sch);
cd6b4f27 1223 if (IS_ERR(cdev)) {
7e9db9ea
CH
1224 rc = PTR_ERR(cdev);
1225 goto out_err;
cd6b4f27 1226 }
8bbace7e 1227 rc = io_subchannel_recog(cdev, sch);
1da177e4 1228 if (rc) {
2ec22984 1229 spin_lock_irqsave(sch->lock, flags);
db6a6423 1230 sch_set_cdev(sch, NULL);
2ec22984 1231 spin_unlock_irqrestore(sch->lock, flags);
1da177e4
LT
1232 if (cdev->dev.release)
1233 cdev->dev.release(&cdev->dev);
7e9db9ea 1234 goto out_err;
1da177e4 1235 }
7e9db9ea
CH
1236 return 0;
1237out_err:
1238 kfree(sch->private);
1239 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1da177e4
LT
1240 return rc;
1241}
1242
1da177e4 1243static int
8bbace7e 1244io_subchannel_remove (struct subchannel *sch)
1da177e4
LT
1245{
1246 struct ccw_device *cdev;
1247 unsigned long flags;
1248
db6a6423
CH
1249 cdev = sch_get_cdev(sch);
1250 if (!cdev)
1da177e4 1251 return 0;
1da177e4
LT
1252 /* Set ccw device to not operational and drop reference. */
1253 spin_lock_irqsave(cdev->ccwlock, flags);
db6a6423 1254 sch_set_cdev(sch, NULL);
1da177e4
LT
1255 cdev->private->state = DEV_STATE_NOT_OPER;
1256 spin_unlock_irqrestore(cdev->ccwlock, flags);
ef99516c
CH
1257 ccw_device_unregister(cdev);
1258 put_device(&cdev->dev);
cd6b4f27 1259 kfree(sch->private);
7e9db9ea 1260 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1da177e4
LT
1261 return 0;
1262}
1263
602b20f2 1264static int io_subchannel_notify(struct subchannel *sch, int event)
1da177e4
LT
1265{
1266 struct ccw_device *cdev;
1267
db6a6423 1268 cdev = sch_get_cdev(sch);
1da177e4
LT
1269 if (!cdev)
1270 return 0;
c820de39 1271 return ccw_device_notify(cdev, event);
1da177e4
LT
1272}
1273
602b20f2 1274static void io_subchannel_verify(struct subchannel *sch)
1da177e4
LT
1275{
1276 struct ccw_device *cdev;
1277
db6a6423 1278 cdev = sch_get_cdev(sch);
1da177e4
LT
1279 if (cdev)
1280 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1281}
1282
c820de39 1283static int check_for_io_on_path(struct subchannel *sch, int mask)
1da177e4 1284{
c820de39 1285 int cc;
1da177e4 1286
c820de39
CH
1287 cc = stsch(sch->schid, &sch->schib);
1288 if (cc)
1289 return 0;
23d805b6 1290 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
c820de39
CH
1291 return 1;
1292 return 0;
1293}
1294
1295static void terminate_internal_io(struct subchannel *sch,
1296 struct ccw_device *cdev)
1297{
1298 if (cio_clear(sch)) {
1299 /* Recheck device in case clear failed. */
1300 sch->lpm = 0;
1301 if (cdev->online)
1302 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1303 else
1304 css_schedule_eval(sch->schid);
d23861ff 1305 return;
c820de39 1306 }
1da177e4 1307 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
c820de39
CH
1308 /* Request retry of internal operation. */
1309 cdev->private->flags.intretry = 1;
1310 /* Call handler. */
1da177e4
LT
1311 if (cdev->handler)
1312 cdev->handler(cdev, cdev->private->intparm,
1313 ERR_PTR(-EIO));
1314}
1315
c820de39
CH
1316static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1317{
1318 struct ccw_device *cdev;
1319
1320 cdev = sch_get_cdev(sch);
1321 if (!cdev)
1322 return;
1323 if (check_for_io_on_path(sch, mask)) {
1324 if (cdev->private->state == DEV_STATE_ONLINE)
1325 ccw_device_kill_io(cdev);
1326 else {
1327 terminate_internal_io(sch, cdev);
1328 /* Re-start path verification. */
1329 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1330 }
1331 } else
1332 /* trigger path verification. */
1333 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1334
1335}
1336
99611f87
CH
1337static int io_subchannel_chp_event(struct subchannel *sch,
1338 struct chp_link *link, int event)
c820de39
CH
1339{
1340 int mask;
c820de39 1341
99611f87 1342 mask = chp_ssd_get_mask(&sch->ssd_info, link);
c820de39
CH
1343 if (!mask)
1344 return 0;
1345 switch (event) {
1346 case CHP_VARY_OFF:
1347 sch->opm &= ~mask;
1348 sch->lpm &= ~mask;
1349 io_subchannel_terminate_path(sch, mask);
1350 break;
1351 case CHP_VARY_ON:
1352 sch->opm |= mask;
1353 sch->lpm |= mask;
1354 io_subchannel_verify(sch);
1355 break;
1356 case CHP_OFFLINE:
1357 if (stsch(sch->schid, &sch->schib))
1358 return -ENXIO;
1359 if (!css_sch_is_valid(&sch->schib))
1360 return -ENODEV;
1361 io_subchannel_terminate_path(sch, mask);
1362 break;
1363 case CHP_ONLINE:
1364 if (stsch(sch->schid, &sch->schib))
1365 return -ENXIO;
1366 sch->lpm |= mask & sch->opm;
1367 io_subchannel_verify(sch);
1368 break;
1369 }
1370 return 0;
1371}
1372
1da177e4 1373static void
8bbace7e 1374io_subchannel_shutdown(struct subchannel *sch)
1da177e4 1375{
1da177e4
LT
1376 struct ccw_device *cdev;
1377 int ret;
1378
db6a6423 1379 cdev = sch_get_cdev(sch);
1da177e4 1380
a8237fc4 1381 if (cio_is_console(sch->schid))
1da177e4
LT
1382 return;
1383 if (!sch->schib.pmcw.ena)
1384 /* Nothing to do. */
1385 return;
1386 ret = cio_disable_subchannel(sch);
1387 if (ret != -EBUSY)
1388 /* Subchannel is disabled, we're done. */
1389 return;
1390 cdev->private->state = DEV_STATE_QUIESCE;
1391 if (cdev->handler)
1392 cdev->handler(cdev, cdev->private->intparm,
1393 ERR_PTR(-EIO));
1394 ret = ccw_device_cancel_halt_clear(cdev);
1395 if (ret == -EBUSY) {
1396 ccw_device_set_timeout(cdev, HZ/10);
1397 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1398 }
1399 cio_disable_subchannel(sch);
1400}
1401
c820de39
CH
1402static int io_subchannel_get_status(struct subchannel *sch)
1403{
1404 struct schib schib;
1405
1406 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1407 return CIO_GONE;
1408 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1409 return CIO_REVALIDATE;
1410 if (!sch->lpm)
1411 return CIO_NO_PATH;
1412 return CIO_OPER;
1413}
1414
1415static int device_is_disconnected(struct ccw_device *cdev)
1416{
1417 if (!cdev)
1418 return 0;
1419 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1420 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1421}
1422
1423static int recovery_check(struct device *dev, void *data)
1424{
1425 struct ccw_device *cdev = to_ccwdev(dev);
1426 int *redo = data;
1427
1428 spin_lock_irq(cdev->ccwlock);
1429 switch (cdev->private->state) {
1430 case DEV_STATE_DISCONNECTED:
1431 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1432 cdev->private->dev_id.ssid,
1433 cdev->private->dev_id.devno);
1434 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1435 *redo = 1;
1436 break;
1437 case DEV_STATE_DISCONNECTED_SENSE_ID:
1438 *redo = 1;
1439 break;
1440 }
1441 spin_unlock_irq(cdev->ccwlock);
1442
1443 return 0;
1444}
1445
1446static void recovery_work_func(struct work_struct *unused)
1447{
1448 int redo = 0;
1449
1450 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1451 if (redo) {
1452 spin_lock_irq(&recovery_lock);
1453 if (!timer_pending(&recovery_timer)) {
1454 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1455 recovery_phase++;
1456 mod_timer(&recovery_timer, jiffies +
1457 recovery_delay[recovery_phase] * HZ);
1458 }
1459 spin_unlock_irq(&recovery_lock);
1460 } else
1461 CIO_MSG_EVENT(4, "recovery: end\n");
1462}
1463
1464static DECLARE_WORK(recovery_work, recovery_work_func);
1465
1466static void recovery_func(unsigned long data)
1467{
1468 /*
1469 * We can't do our recovery in softirq context and it's not
1470 * performance critical, so we schedule it.
1471 */
1472 schedule_work(&recovery_work);
1473}
1474
1475static void ccw_device_schedule_recovery(void)
1476{
1477 unsigned long flags;
1478
1479 CIO_MSG_EVENT(4, "recovery: schedule\n");
1480 spin_lock_irqsave(&recovery_lock, flags);
1481 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1482 recovery_phase = 0;
1483 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1484 }
1485 spin_unlock_irqrestore(&recovery_lock, flags);
1486}
1487
1488static void device_set_disconnected(struct ccw_device *cdev)
1489{
1490 if (!cdev)
1491 return;
1492 ccw_device_set_timeout(cdev, 0);
1493 cdev->private->flags.fake_irb = 0;
1494 cdev->private->state = DEV_STATE_DISCONNECTED;
1495 if (cdev->online)
1496 ccw_device_schedule_recovery();
1497}
1498
1499static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1500{
1501 int event, ret, disc;
1502 unsigned long flags;
1503 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
1504 struct ccw_device *cdev;
1505
1506 spin_lock_irqsave(sch->lock, flags);
1507 cdev = sch_get_cdev(sch);
1508 disc = device_is_disconnected(cdev);
1509 if (disc && slow) {
1510 /* Disconnected devices are evaluated directly only.*/
1511 spin_unlock_irqrestore(sch->lock, flags);
1512 return 0;
1513 }
1514 /* No interrupt after machine check - kill pending timers. */
1515 if (cdev)
1516 ccw_device_set_timeout(cdev, 0);
1517 if (!disc && !slow) {
1518 /* Non-disconnected devices are evaluated on the slow path. */
1519 spin_unlock_irqrestore(sch->lock, flags);
1520 return -EAGAIN;
1521 }
1522 event = io_subchannel_get_status(sch);
1523 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1524 sch->schid.ssid, sch->schid.sch_no, event,
1525 disc ? "disconnected" : "normal",
1526 slow ? "slow" : "fast");
1527 /* Analyze subchannel status. */
1528 action = NONE;
1529 switch (event) {
1530 case CIO_NO_PATH:
1531 if (disc) {
1532 /* Check if paths have become available. */
1533 action = REPROBE;
1534 break;
1535 }
1536 /* fall through */
1537 case CIO_GONE:
1538 /* Prevent unwanted effects when opening lock. */
1539 cio_disable_subchannel(sch);
1540 device_set_disconnected(cdev);
1541 /* Ask driver what to do with device. */
1542 action = UNREGISTER;
1543 spin_unlock_irqrestore(sch->lock, flags);
1544 ret = io_subchannel_notify(sch, event);
1545 spin_lock_irqsave(sch->lock, flags);
1546 if (ret)
1547 action = NONE;
1548 break;
1549 case CIO_REVALIDATE:
1550 /* Device will be removed, so no notify necessary. */
1551 if (disc)
1552 /* Reprobe because immediate unregister might block. */
1553 action = REPROBE;
1554 else
1555 action = UNREGISTER_PROBE;
1556 break;
1557 case CIO_OPER:
1558 if (disc)
1559 /* Get device operational again. */
1560 action = REPROBE;
1561 break;
1562 }
1563 /* Perform action. */
1564 ret = 0;
1565 switch (action) {
1566 case UNREGISTER:
1567 case UNREGISTER_PROBE:
1568 /* Unregister device (will use subchannel lock). */
1569 spin_unlock_irqrestore(sch->lock, flags);
1570 css_sch_device_unregister(sch);
1571 spin_lock_irqsave(sch->lock, flags);
1572
1573 /* Reset intparm to zeroes. */
1574 sch->schib.pmcw.intparm = 0;
1575 cio_modify(sch);
1576 break;
1577 case REPROBE:
1578 ccw_device_trigger_reprobe(cdev);
1579 break;
1580 default:
1581 break;
1582 }
1583 spin_unlock_irqrestore(sch->lock, flags);
1584 /* Probe if necessary. */
1585 if (action == UNREGISTER_PROBE)
1586 ret = css_probe_device(sch->schid);
1587
1588 return ret;
1589}
1590
1da177e4
LT
1591#ifdef CONFIG_CCW_CONSOLE
1592static struct ccw_device console_cdev;
1593static struct ccw_device_private console_private;
1594static int console_cdev_in_use;
1595
2ec22984
CH
1596static DEFINE_SPINLOCK(ccw_console_lock);
1597
1598spinlock_t * cio_get_console_lock(void)
1599{
1600 return &ccw_console_lock;
1601}
1602
0ae7a7b2
CH
1603static int ccw_device_console_enable(struct ccw_device *cdev,
1604 struct subchannel *sch)
1da177e4
LT
1605{
1606 int rc;
1607
cd6b4f27
CH
1608 /* Attach subchannel private data. */
1609 sch->private = cio_get_console_priv();
1610 memset(sch->private, 0, sizeof(struct io_subchannel_private));
0ae7a7b2
CH
1611 io_subchannel_init_fields(sch);
1612 sch->driver = &io_subchannel_driver;
1da177e4 1613 /* Initialize the ccw_device structure. */
292888c8 1614 cdev->dev.parent= &sch->dev;
1da177e4
LT
1615 rc = io_subchannel_recog(cdev, sch);
1616 if (rc)
1617 return rc;
1618
1619 /* Now wait for the async. recognition to come to an end. */
1620 spin_lock_irq(cdev->ccwlock);
1621 while (!dev_fsm_final_state(cdev))
1622 wait_cons_dev();
1623 rc = -EIO;
1624 if (cdev->private->state != DEV_STATE_OFFLINE)
1625 goto out_unlock;
1626 ccw_device_online(cdev);
1627 while (!dev_fsm_final_state(cdev))
1628 wait_cons_dev();
1629 if (cdev->private->state != DEV_STATE_ONLINE)
1630 goto out_unlock;
1631 rc = 0;
1632out_unlock:
1633 spin_unlock_irq(cdev->ccwlock);
1634 return 0;
1635}
1636
1637struct ccw_device *
1638ccw_device_probe_console(void)
1639{
1640 struct subchannel *sch;
1641 int ret;
1642
1643 if (xchg(&console_cdev_in_use, 1) != 0)
600b5d16 1644 return ERR_PTR(-EBUSY);
1da177e4
LT
1645 sch = cio_probe_console();
1646 if (IS_ERR(sch)) {
1647 console_cdev_in_use = 0;
1648 return (void *) sch;
1649 }
1650 memset(&console_cdev, 0, sizeof(struct ccw_device));
1651 memset(&console_private, 0, sizeof(struct ccw_device_private));
1652 console_cdev.private = &console_private;
c1637532 1653 console_private.cdev = &console_cdev;
1da177e4
LT
1654 ret = ccw_device_console_enable(&console_cdev, sch);
1655 if (ret) {
1656 cio_release_console();
1657 console_cdev_in_use = 0;
1658 return ERR_PTR(ret);
1659 }
1660 console_cdev.online = 1;
1661 return &console_cdev;
1662}
1663#endif
1664
1665/*
1666 * get ccw_device matching the busid, but only if owned by cdrv
1667 */
b0744bd2
CH
1668static int
1669__ccwdev_check_busid(struct device *dev, void *id)
1670{
1671 char *bus_id;
1672
12975aef 1673 bus_id = id;
b0744bd2
CH
1674
1675 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1676}
1677
1678
b2ffd8e9
CH
1679/**
1680 * get_ccwdev_by_busid() - obtain device from a bus id
1681 * @cdrv: driver the device is owned by
1682 * @bus_id: bus id of the device to be searched
1683 *
1684 * This function searches all devices owned by @cdrv for a device with a bus
1685 * id matching @bus_id.
1686 * Returns:
1687 * If a match is found, its reference count of the found device is increased
1688 * and it is returned; else %NULL is returned.
1689 */
1690struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1691 const char *bus_id)
1da177e4 1692{
b0744bd2 1693 struct device *dev;
1da177e4
LT
1694 struct device_driver *drv;
1695
1696 drv = get_driver(&cdrv->driver);
1697 if (!drv)
b0744bd2 1698 return NULL;
1da177e4 1699
b0744bd2
CH
1700 dev = driver_find_device(drv, NULL, (void *)bus_id,
1701 __ccwdev_check_busid);
1da177e4
LT
1702 put_driver(drv);
1703
d2c993d8 1704 return dev ? to_ccwdev(dev) : NULL;
1da177e4
LT
1705}
1706
1707/************************** device driver handling ************************/
1708
1709/* This is the implementation of the ccw_driver class. The probe, remove
1710 * and release methods are initially very similar to the device_driver
1711 * implementations, with the difference that they have ccw_device
1712 * arguments.
1713 *
1714 * A ccw driver also contains the information that is needed for
1715 * device matching.
1716 */
1717static int
1718ccw_device_probe (struct device *dev)
1719{
1720 struct ccw_device *cdev = to_ccwdev(dev);
1721 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1722 int ret;
1723
1724 cdev->drv = cdrv; /* to let the driver call _set_online */
1725
1726 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1727
1728 if (ret) {
d2c993d8 1729 cdev->drv = NULL;
1da177e4
LT
1730 return ret;
1731 }
1732
1733 return 0;
1734}
1735
1736static int
1737ccw_device_remove (struct device *dev)
1738{
1739 struct ccw_device *cdev = to_ccwdev(dev);
1740 struct ccw_driver *cdrv = cdev->drv;
1741 int ret;
1742
1da177e4
LT
1743 if (cdrv->remove)
1744 cdrv->remove(cdev);
1745 if (cdev->online) {
1746 cdev->online = 0;
1747 spin_lock_irq(cdev->ccwlock);
1748 ret = ccw_device_offline(cdev);
1749 spin_unlock_irq(cdev->ccwlock);
1750 if (ret == 0)
1751 wait_event(cdev->private->wait_q,
1752 dev_fsm_final_state(cdev));
1753 else
139b83dd 1754 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
e556bbbd
CH
1755 "device 0.%x.%04x\n",
1756 ret, cdev->private->dev_id.ssid,
1757 cdev->private->dev_id.devno);
1da177e4
LT
1758 }
1759 ccw_device_set_timeout(cdev, 0);
d2c993d8 1760 cdev->drv = NULL;
1da177e4
LT
1761 return 0;
1762}
1763
958974fb
CH
1764static void ccw_device_shutdown(struct device *dev)
1765{
1766 struct ccw_device *cdev;
1767
1768 cdev = to_ccwdev(dev);
1769 if (cdev->drv && cdev->drv->shutdown)
1770 cdev->drv->shutdown(cdev);
1842f2b1 1771 disable_cmf(cdev);
958974fb
CH
1772}
1773
8bbace7e
CH
1774struct bus_type ccw_bus_type = {
1775 .name = "ccw",
1776 .match = ccw_bus_match,
1777 .uevent = ccw_uevent,
1778 .probe = ccw_device_probe,
1779 .remove = ccw_device_remove,
958974fb 1780 .shutdown = ccw_device_shutdown,
8bbace7e
CH
1781};
1782
b2ffd8e9
CH
1783/**
1784 * ccw_driver_register() - register a ccw driver
1785 * @cdriver: driver to be registered
1786 *
1787 * This function is mainly a wrapper around driver_register().
1788 * Returns:
1789 * %0 on success and a negative error value on failure.
1790 */
1791int ccw_driver_register(struct ccw_driver *cdriver)
1da177e4
LT
1792{
1793 struct device_driver *drv = &cdriver->driver;
1794
1795 drv->bus = &ccw_bus_type;
1796 drv->name = cdriver->name;
4beee646 1797 drv->owner = cdriver->owner;
1da177e4
LT
1798
1799 return driver_register(drv);
1800}
1801
b2ffd8e9
CH
1802/**
1803 * ccw_driver_unregister() - deregister a ccw driver
1804 * @cdriver: driver to be deregistered
1805 *
1806 * This function is mainly a wrapper around driver_unregister().
1807 */
1808void ccw_driver_unregister(struct ccw_driver *cdriver)
1da177e4
LT
1809{
1810 driver_unregister(&cdriver->driver);
1811}
1812
a8237fc4
CH
1813/* Helper func for qdio. */
1814struct subchannel_id
1815ccw_device_get_subchannel_id(struct ccw_device *cdev)
1816{
1817 struct subchannel *sch;
1818
1819 sch = to_subchannel(cdev->dev.parent);
1820 return sch->schid;
1821}
1822
1da177e4
LT
1823MODULE_LICENSE("GPL");
1824EXPORT_SYMBOL(ccw_device_set_online);
1825EXPORT_SYMBOL(ccw_device_set_offline);
1826EXPORT_SYMBOL(ccw_driver_register);
1827EXPORT_SYMBOL(ccw_driver_unregister);
1828EXPORT_SYMBOL(get_ccwdev_by_busid);
1829EXPORT_SYMBOL(ccw_bus_type);
1830EXPORT_SYMBOL(ccw_device_work);
1831EXPORT_SYMBOL(ccw_device_notify_work);
a8237fc4 1832EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);
This page took 0.478525 seconds and 5 git commands to generate.