blob: 67e7a31239549e4a16c205f5f6a473f096f7e470 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * drivers/s390/cio/device.c
3 * bus driver for ccw devices
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Cornelia Huckc820de32008-07-14 09:58:45 +02005 * Copyright IBM Corp. 2002,2008
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Author(s): Arnd Bergmann (arndb@de.ibm.com)
Cornelia Huck4ce3b302006-01-14 13:21:04 -08007 * Cornelia Huck (cornelia.huck@de.ibm.com)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Martin Schwidefsky (schwidefsky@de.ibm.com)
9 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#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>
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010019#include <linux/timer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
21#include <asm/ccwdev.h>
22#include <asm/cio.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080023#include <asm/param.h> /* HZ */
Cornelia Huck1842f2b2007-10-12 16:11:22 +020024#include <asm/cmb.h>
Cornelia Huck3a3fc292008-07-14 09:58:58 +020025#include <asm/isc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +020027#include "chp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070028#include "cio.h"
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +010029#include "cio_debug.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include "css.h"
31#include "device.h"
32#include "ioasm.h"
Cornelia Huckcd6b4f22008-01-26 14:10:43 +010033#include "io_sch.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010035static struct timer_list recovery_timer;
Cornelia Huck486d0a02008-02-19 15:29:23 +010036static DEFINE_SPINLOCK(recovery_lock);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +010037static int recovery_phase;
38static const unsigned long recovery_delay[] = { 3, 30, 300 };
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040/******************* 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
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020065/* 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). */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020068static int snprint_alias(char *buf, size_t size,
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020069 struct ccw_device_id *id, const char *suffix)
70{
71 int len;
72
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020073 len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020074 if (len > size)
75 return len;
76 buf += len;
77 size -= len;
78
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);
84
85 return len;
86}
87
88/* Set up environment variables for ccw device uevent. Return 0 on success,
89 * non-zero otherwise. */
Kay Sievers7eff2e72007-08-14 15:15:12 +020090static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
92 struct ccw_device *cdev = to_ccwdev(dev);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020093 struct ccw_device_id *id = &(cdev->id);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020094 int ret;
95 char modalias_buf[30];
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +020097 /* CU_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +020098 ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +020099 if (ret)
100 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200101
102 /* CU_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200103 ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200104 if (ret)
105 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107 /* The next two can be zero, that's ok for us */
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200108 /* DEV_TYPE= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200109 ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200110 if (ret)
111 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200113 /* DEV_MODEL= */
Kay Sievers7eff2e72007-08-14 15:15:12 +0200114 ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200115 if (ret)
116 return ret;
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200117
118 /* MODALIAS= */
Cornelia Huckcfbe9bb2007-04-27 16:01:32 +0200119 snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
Kay Sievers7eff2e72007-08-14 15:15:12 +0200120 ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
121 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122}
123
Cornelia Huck8bbace72006-01-11 10:56:22 +0100124struct bus_type ccw_bus_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Cornelia Huck602b20f2008-01-26 14:10:39 +0100126static void io_subchannel_irq(struct subchannel *);
127static int io_subchannel_probe(struct subchannel *);
128static int io_subchannel_remove(struct subchannel *);
Cornelia Huck8bbace72006-01-11 10:56:22 +0100129static void io_subchannel_shutdown(struct subchannel *);
Cornelia Huckc820de32008-07-14 09:58:45 +0200130static int io_subchannel_sch_event(struct subchannel *, int);
131static int io_subchannel_chp_event(struct subchannel *, void *, int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200133static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100134 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 .subchannel_type = SUBCHANNEL_TYPE_IO,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100136 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200138 .sch_event = io_subchannel_sch_event,
139 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100140 .probe = io_subchannel_probe,
141 .remove = io_subchannel_remove,
142 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143};
144
145struct workqueue_struct *ccw_device_work;
146struct workqueue_struct *ccw_device_notify_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200147wait_queue_head_t ccw_device_init_wq;
148atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100150static void recovery_func(unsigned long data);
151
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152static int __init
153init_ccw_bus_type (void)
154{
155 int ret;
156
157 init_waitqueue_head(&ccw_device_init_wq);
158 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100159 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 ccw_device_work = create_singlethread_workqueue("cio");
162 if (!ccw_device_work)
163 return -ENOMEM; /* FIXME: better errno ? */
164 ccw_device_notify_work = create_singlethread_workqueue("cio_notify");
165 if (!ccw_device_notify_work) {
166 ret = -ENOMEM; /* FIXME: better errno ? */
167 goto out_err;
168 }
169 slow_path_wq = create_singlethread_workqueue("kslowcrw");
170 if (!slow_path_wq) {
171 ret = -ENOMEM; /* FIXME: better errno ? */
172 goto out_err;
173 }
174 if ((ret = bus_register (&ccw_bus_type)))
175 goto out_err;
176
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100177 ret = css_driver_register(&io_subchannel_driver);
178 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 goto out_err;
180
181 wait_event(ccw_device_init_wq,
182 atomic_read(&ccw_device_init_count) == 0);
183 flush_workqueue(ccw_device_work);
184 return 0;
185out_err:
186 if (ccw_device_work)
187 destroy_workqueue(ccw_device_work);
188 if (ccw_device_notify_work)
189 destroy_workqueue(ccw_device_notify_work);
190 if (slow_path_wq)
191 destroy_workqueue(slow_path_wq);
192 return ret;
193}
194
195static void __exit
196cleanup_ccw_bus_type (void)
197{
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100198 css_driver_unregister(&io_subchannel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 bus_unregister(&ccw_bus_type);
200 destroy_workqueue(ccw_device_notify_work);
201 destroy_workqueue(ccw_device_work);
202}
203
204subsys_initcall(init_ccw_bus_type);
205module_exit(cleanup_ccw_bus_type);
206
207/************************ device handling **************************/
208
209/*
210 * A ccw_device has some interfaces in sysfs in addition to the
211 * standard ones.
212 * The following entries are designed to export the information which
213 * resided in 2.4 in /proc/subchannels. Subchannel and device number
214 * are obvious, so they don't have an entry :)
215 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
216 */
217static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400218chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219{
220 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200221 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 ssize_t ret = 0;
223 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200224 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200226 for (chp = 0; chp < 8; chp++) {
227 mask = 0x80 >> chp;
228 if (ssd->path_mask & mask)
229 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
230 else
231 ret += sprintf(buf + ret, "00 ");
232 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 ret += sprintf (buf+ret, "\n");
234 return min((ssize_t)PAGE_SIZE, ret);
235}
236
237static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400238pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct subchannel *sch = to_subchannel(dev);
241 struct pmcw *pmcw = &sch->schib.pmcw;
242
243 return sprintf (buf, "%02x %02x %02x\n",
244 pmcw->pim, pmcw->pam, pmcw->pom);
245}
246
247static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400248devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249{
250 struct ccw_device *cdev = to_ccwdev(dev);
251 struct ccw_device_id *id = &(cdev->id);
252
253 if (id->dev_type != 0)
254 return sprintf(buf, "%04x/%02x\n",
255 id->dev_type, id->dev_model);
256 else
257 return sprintf(buf, "n/a\n");
258}
259
260static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400261cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct ccw_device *cdev = to_ccwdev(dev);
264 struct ccw_device_id *id = &(cdev->id);
265
266 return sprintf(buf, "%04x/%02x\n",
267 id->cu_type, id->cu_model);
268}
269
270static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800271modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
272{
273 struct ccw_device *cdev = to_ccwdev(dev);
274 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200275 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800276
Cornelia Huck086a6c62007-07-17 13:36:08 +0200277 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200278
279 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800280}
281
282static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400283online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct ccw_device *cdev = to_ccwdev(dev);
286
287 return sprintf(buf, cdev->online ? "1\n" : "0\n");
288}
289
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100290int ccw_device_is_orphan(struct ccw_device *cdev)
291{
292 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
293}
294
Cornelia Huckef995162007-04-27 16:01:39 +0200295static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100296{
Cornelia Huck7674da72006-12-08 15:54:21 +0100297 if (test_and_clear_bit(1, &cdev->private->registered))
Cornelia Huckef995162007-04-27 16:01:39 +0200298 device_del(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100299}
300
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200301static void ccw_device_remove_orphan_cb(struct device *dev)
302{
303 struct ccw_device *cdev = to_ccwdev(dev);
304
305 ccw_device_unregister(cdev);
306 put_device(&cdev->dev);
307}
308
309static void ccw_device_remove_sch_cb(struct device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310{
311 struct subchannel *sch;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200312
313 sch = to_subchannel(dev);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200314 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /* Reset intparm to zeroes. */
316 sch->schib.pmcw.intparm = 0;
317 cio_modify(sch);
318 put_device(&sch->dev);
319}
320
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200321static void
322ccw_device_remove_disconnected(struct ccw_device *cdev)
323{
324 unsigned long flags;
325 int rc;
326
327 /*
328 * Forced offline in disconnected state means
329 * 'throw away device'.
330 */
331 if (ccw_device_is_orphan(cdev)) {
332 /*
333 * Deregister ccw device.
334 * Unfortunately, we cannot do this directly from the
335 * attribute method.
336 */
337 spin_lock_irqsave(cdev->ccwlock, flags);
338 cdev->private->state = DEV_STATE_NOT_OPER;
339 spin_unlock_irqrestore(cdev->ccwlock, flags);
340 rc = device_schedule_callback(&cdev->dev,
341 ccw_device_remove_orphan_cb);
342 if (rc)
Michael Ernst139b83d2008-05-07 09:22:54 +0200343 CIO_MSG_EVENT(0, "Couldn't unregister orphan "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200344 "0.%x.%04x\n",
345 cdev->private->dev_id.ssid,
346 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200347 return;
348 }
349 /* Deregister subchannel, which will kill the ccw device. */
350 rc = device_schedule_callback(cdev->dev.parent,
351 ccw_device_remove_sch_cb);
352 if (rc)
Michael Ernst139b83d2008-05-07 09:22:54 +0200353 CIO_MSG_EVENT(0, "Couldn't unregister disconnected device "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200354 "0.%x.%04x\n",
355 cdev->private->dev_id.ssid,
356 cdev->private->dev_id.devno);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200357}
358
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200359/**
360 * ccw_device_set_offline() - disable a ccw device for I/O
361 * @cdev: target ccw device
362 *
363 * This function calls the driver's set_offline() function for @cdev, if
364 * given, and then disables @cdev.
365 * Returns:
366 * %0 on success and a negative error value on failure.
367 * Context:
368 * enabled, ccw device lock not held
369 */
370int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371{
372 int ret;
373
374 if (!cdev)
375 return -ENODEV;
376 if (!cdev->online || !cdev->drv)
377 return -EINVAL;
378
379 if (cdev->drv->set_offline) {
380 ret = cdev->drv->set_offline(cdev);
381 if (ret != 0)
382 return ret;
383 }
384 cdev->online = 0;
385 spin_lock_irq(cdev->ccwlock);
386 ret = ccw_device_offline(cdev);
387 if (ret == -ENODEV) {
388 if (cdev->private->state != DEV_STATE_NOT_OPER) {
389 cdev->private->state = DEV_STATE_OFFLINE;
390 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
391 }
392 spin_unlock_irq(cdev->ccwlock);
393 return ret;
394 }
395 spin_unlock_irq(cdev->ccwlock);
396 if (ret == 0)
397 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
398 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200399 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200400 "device 0.%x.%04x\n",
401 ret, cdev->private->dev_id.ssid,
402 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 cdev->online = 1;
404 }
405 return ret;
406}
407
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200408/**
409 * ccw_device_set_online() - enable a ccw device for I/O
410 * @cdev: target ccw device
411 *
412 * This function first enables @cdev and then calls the driver's set_online()
413 * function for @cdev, if given. If set_online() returns an error, @cdev is
414 * disabled again.
415 * Returns:
416 * %0 on success and a negative error value on failure.
417 * Context:
418 * enabled, ccw device lock not held
419 */
420int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421{
422 int ret;
423
424 if (!cdev)
425 return -ENODEV;
426 if (cdev->online || !cdev->drv)
427 return -EINVAL;
428
429 spin_lock_irq(cdev->ccwlock);
430 ret = ccw_device_online(cdev);
431 spin_unlock_irq(cdev->ccwlock);
432 if (ret == 0)
433 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
434 else {
Michael Ernst139b83d2008-05-07 09:22:54 +0200435 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200436 "device 0.%x.%04x\n",
437 ret, cdev->private->dev_id.ssid,
438 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439 return ret;
440 }
441 if (cdev->private->state != DEV_STATE_ONLINE)
442 return -ENODEV;
443 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
444 cdev->online = 1;
445 return 0;
446 }
447 spin_lock_irq(cdev->ccwlock);
448 ret = ccw_device_offline(cdev);
449 spin_unlock_irq(cdev->ccwlock);
450 if (ret == 0)
451 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200452 else
Michael Ernst139b83d2008-05-07 09:22:54 +0200453 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200454 "device 0.%x.%04x\n",
455 ret, cdev->private->dev_id.ssid,
456 cdev->private->dev_id.devno);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800457 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458}
459
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200460static void online_store_handle_offline(struct ccw_device *cdev)
461{
462 if (cdev->private->state == DEV_STATE_DISCONNECTED)
463 ccw_device_remove_disconnected(cdev);
464 else if (cdev->drv && cdev->drv->set_offline)
465 ccw_device_set_offline(cdev);
466}
467
468static int online_store_recog_and_online(struct ccw_device *cdev)
469{
470 int ret;
471
472 /* Do device recognition, if needed. */
473 if (cdev->id.cu_type == 0) {
474 ret = ccw_device_recognition(cdev);
475 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200476 CIO_MSG_EVENT(0, "Couldn't start recognition "
477 "for device 0.%x.%04x (ret=%d)\n",
478 cdev->private->dev_id.ssid,
479 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200480 return ret;
481 }
482 wait_event(cdev->private->wait_q,
483 cdev->private->flags.recog_done);
484 }
485 if (cdev->drv && cdev->drv->set_online)
486 ccw_device_set_online(cdev);
487 return 0;
488}
489static void online_store_handle_online(struct ccw_device *cdev, int force)
490{
491 int ret;
492
493 ret = online_store_recog_and_online(cdev);
494 if (ret)
495 return;
496 if (force && cdev->private->state == DEV_STATE_BOXED) {
497 ret = ccw_device_stlck(cdev);
498 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200499 dev_warn(&cdev->dev,
500 "ccw_device_stlck returned %d!\n", ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200501 return;
502 }
503 if (cdev->id.cu_type == 0)
504 cdev->private->state = DEV_STATE_NOT_OPER;
505 online_store_recog_and_online(cdev);
506 }
507
508}
509
510static ssize_t online_store (struct device *dev, struct device_attribute *attr,
511 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512{
513 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200514 int force, ret;
515 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800517 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 return -EAGAIN;
519
520 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
521 atomic_set(&cdev->private->onoff, 0);
522 return -EINVAL;
523 }
524 if (!strncmp(buf, "force\n", count)) {
525 force = 1;
526 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200527 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 } else {
529 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200530 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200532 if (ret)
533 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200534 switch (i) {
535 case 0:
536 online_store_handle_offline(cdev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200537 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200538 break;
539 case 1:
540 online_store_handle_online(cdev, force);
Cornelia Huck2f972202008-04-30 13:38:33 +0200541 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200542 break;
543 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200544 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200546out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (cdev->drv)
548 module_put(cdev->drv->owner);
549 atomic_set(&cdev->private->onoff, 0);
Cornelia Huck2f972202008-04-30 13:38:33 +0200550 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551}
552
553static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400554available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
556 struct ccw_device *cdev = to_ccwdev(dev);
557 struct subchannel *sch;
558
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100559 if (ccw_device_is_orphan(cdev))
560 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 switch (cdev->private->state) {
562 case DEV_STATE_BOXED:
563 return sprintf(buf, "boxed\n");
564 case DEV_STATE_DISCONNECTED:
565 case DEV_STATE_DISCONNECTED_SENSE_ID:
566 case DEV_STATE_NOT_OPER:
567 sch = to_subchannel(dev->parent);
568 if (!sch->lpm)
569 return sprintf(buf, "no path\n");
570 else
571 return sprintf(buf, "no device\n");
572 default:
573 /* All other states considered fine. */
574 return sprintf(buf, "good\n");
575 }
576}
577
578static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
579static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
580static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
581static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800582static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584static DEVICE_ATTR(availability, 0444, available_show, NULL);
585
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200586static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 &dev_attr_chpids.attr,
588 &dev_attr_pimpampom.attr,
589 NULL,
590};
591
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200592static struct attribute_group io_subchannel_attr_group = {
593 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100594};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595
596static struct attribute * ccwdev_attrs[] = {
597 &dev_attr_devtype.attr,
598 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800599 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 &dev_attr_online.attr,
601 &dev_attr_cmb_enable.attr,
602 &dev_attr_availability.attr,
603 NULL,
604};
605
606static struct attribute_group ccwdev_attr_group = {
607 .attrs = ccwdev_attrs,
608};
609
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200610static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200611 &ccwdev_attr_group,
612 NULL,
613};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614
615/* this is a simple abstraction for device_register that sets the
616 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200617static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
619 struct device *dev = &cdev->dev;
620 int ret;
621
622 dev->bus = &ccw_bus_type;
623
624 if ((ret = device_add(dev)))
625 return ret;
626
627 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 return ret;
629}
630
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700631struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200632 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700633 struct ccw_device * sibling;
634};
635
636static int
637match_devno(struct device * dev, void * data)
638{
Cornelia Huck78964262006-10-11 15:31:38 +0200639 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700640 struct ccw_device * cdev;
641
642 cdev = to_ccwdev(dev);
643 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100644 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200645 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100646 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700647 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700648 return 0;
649}
650
Cornelia Huck78964262006-10-11 15:31:38 +0200651static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
652 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200655 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656
Cornelia Huck78964262006-10-11 15:31:38 +0200657 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200658 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700659 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700661 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100664static int match_orphan(struct device *dev, void *data)
665{
666 struct ccw_dev_id *dev_id;
667 struct ccw_device *cdev;
668
669 dev_id = data;
670 cdev = to_ccwdev(dev);
671 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
672}
673
674static struct ccw_device *
675get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
676 struct ccw_dev_id *dev_id)
677{
678 struct device *dev;
679
680 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
681 match_orphan);
682
683 return dev ? to_ccwdev(dev) : NULL;
684}
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100687ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100689 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 struct ccw_device *cdev;
691
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100692 priv = container_of(work, struct ccw_device_private, kick_work);
693 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 if (device_add(&cdev->dev)) {
695 put_device(&cdev->dev);
696 return;
697 }
698 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699}
700
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100701void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100703 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 struct ccw_device *cdev;
705 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100707 priv = container_of(work, struct ccw_device_private, kick_work);
708 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710
Cornelia Huckef995162007-04-27 16:01:39 +0200711 ccw_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100713 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 queue_work(ccw_device_work, &cdev->private->kick_work);
715}
716
717static void
718ccw_device_release(struct device *dev)
719{
720 struct ccw_device *cdev;
721
722 cdev = to_ccwdev(dev);
723 kfree(cdev->private);
724 kfree(cdev);
725}
726
Cornelia Huck7674da72006-12-08 15:54:21 +0100727static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
728{
729 struct ccw_device *cdev;
730
731 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
732 if (cdev) {
733 cdev->private = kzalloc(sizeof(struct ccw_device_private),
734 GFP_KERNEL | GFP_DMA);
735 if (cdev->private)
736 return cdev;
737 }
738 kfree(cdev);
739 return ERR_PTR(-ENOMEM);
740}
741
742static int io_subchannel_initialize_dev(struct subchannel *sch,
743 struct ccw_device *cdev)
744{
745 cdev->private->cdev = cdev;
746 atomic_set(&cdev->private->onoff, 0);
747 cdev->dev.parent = &sch->dev;
748 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100749 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200750 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100751 /* Do first half of device_register. */
752 device_initialize(&cdev->dev);
753 if (!get_device(&sch->dev)) {
754 if (cdev->dev.release)
755 cdev->dev.release(&cdev->dev);
756 return -ENODEV;
757 }
758 return 0;
759}
760
761static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
762{
763 struct ccw_device *cdev;
764 int ret;
765
766 cdev = io_subchannel_allocate_dev(sch);
767 if (!IS_ERR(cdev)) {
768 ret = io_subchannel_initialize_dev(sch, cdev);
769 if (ret) {
770 kfree(cdev);
771 cdev = ERR_PTR(ret);
772 }
773 }
774 return cdev;
775}
776
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100777static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
778
779static void sch_attach_device(struct subchannel *sch,
780 struct ccw_device *cdev)
781{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200782 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100783 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100784 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100785 cdev->private->schid = sch->schid;
786 cdev->ccwlock = sch->lock;
Cornelia Huckc820de32008-07-14 09:58:45 +0200787 ccw_device_trigger_reprobe(cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100788 spin_unlock_irq(sch->lock);
789}
790
791static void sch_attach_disconnected_device(struct subchannel *sch,
792 struct ccw_device *cdev)
793{
794 struct subchannel *other_sch;
795 int ret;
796
797 other_sch = to_subchannel(get_device(cdev->dev.parent));
798 ret = device_move(&cdev->dev, &sch->dev);
799 if (ret) {
Michael Ernst139b83d2008-05-07 09:22:54 +0200800 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100801 "(ret=%d)!\n", cdev->private->dev_id.ssid,
802 cdev->private->dev_id.devno, ret);
803 put_device(&other_sch->dev);
804 return;
805 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100806 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100807 /* No need to keep a subchannel without ccw device around. */
808 css_sch_device_unregister(other_sch);
809 put_device(&other_sch->dev);
810 sch_attach_device(sch, cdev);
811}
812
813static void sch_attach_orphaned_device(struct subchannel *sch,
814 struct ccw_device *cdev)
815{
816 int ret;
817
818 /* Try to move the ccw device to its new subchannel. */
819 ret = device_move(&cdev->dev, &sch->dev);
820 if (ret) {
821 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
822 "failed (ret=%d)!\n",
823 cdev->private->dev_id.ssid,
824 cdev->private->dev_id.devno, ret);
825 return;
826 }
827 sch_attach_device(sch, cdev);
828}
829
830static void sch_create_and_recog_new_device(struct subchannel *sch)
831{
832 struct ccw_device *cdev;
833
834 /* Need to allocate a new ccw device. */
835 cdev = io_subchannel_create_ccwdev(sch);
836 if (IS_ERR(cdev)) {
837 /* OK, we did everything we could... */
838 css_sch_device_unregister(sch);
839 return;
840 }
841 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100842 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100843 spin_unlock_irq(sch->lock);
844 /* Start recognition for the new ccw device. */
845 if (io_subchannel_recog(cdev, sch)) {
846 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100847 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100848 spin_unlock_irq(sch->lock);
849 if (cdev->dev.release)
850 cdev->dev.release(&cdev->dev);
851 css_sch_device_unregister(sch);
852 }
853}
854
855
856void ccw_device_move_to_orphanage(struct work_struct *work)
857{
858 struct ccw_device_private *priv;
859 struct ccw_device *cdev;
860 struct ccw_device *replacing_cdev;
861 struct subchannel *sch;
862 int ret;
863 struct channel_subsystem *css;
864 struct ccw_dev_id dev_id;
865
866 priv = container_of(work, struct ccw_device_private, kick_work);
867 cdev = priv->cdev;
868 sch = to_subchannel(cdev->dev.parent);
869 css = to_css(sch->dev.parent);
870 dev_id.devno = sch->schib.pmcw.dev;
871 dev_id.ssid = sch->schid.ssid;
872
873 /*
874 * Move the orphaned ccw device to the orphanage so the replacing
875 * ccw device can take its place on the subchannel.
876 */
877 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
878 if (ret) {
879 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
880 "(ret=%d)!\n", cdev->private->dev_id.ssid,
881 cdev->private->dev_id.devno, ret);
882 return;
883 }
884 cdev->ccwlock = css->pseudo_subchannel->lock;
885 /*
886 * Search for the replacing ccw device
887 * - among the disconnected devices
888 * - in the orphanage
889 */
890 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
891 if (replacing_cdev) {
892 sch_attach_disconnected_device(sch, replacing_cdev);
893 return;
894 }
895 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
896 if (replacing_cdev) {
897 sch_attach_orphaned_device(sch, replacing_cdev);
898 return;
899 }
900 sch_create_and_recog_new_device(sch);
901}
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903/*
904 * Register recognized device.
905 */
906static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100907io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100909 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 struct ccw_device *cdev;
911 struct subchannel *sch;
912 int ret;
913 unsigned long flags;
914
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100915 priv = container_of(work, struct ccw_device_private, kick_work);
916 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200918 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100919 /*
920 * io_subchannel_register() will also be called after device
921 * recognition has been done for a boxed device (which will already
922 * be registered). We need to reprobe since we may now have sense id
923 * information.
924 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700925 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100926 if (!cdev->drv) {
927 ret = device_reprobe(&cdev->dev);
928 if (ret)
929 /* We can't do much here. */
Michael Ernst139b83d2008-05-07 09:22:54 +0200930 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200931 " %d for 0.%x.%04x\n", ret,
932 cdev->private->dev_id.ssid,
933 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100934 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 goto out;
936 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700937 /*
938 * Now we know this subchannel will stay, we can throw
939 * our delayed uevent.
940 */
941 sch->dev.uevent_suppress = 0;
942 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 /* make it known to the system */
944 ret = ccw_device_register(cdev);
945 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200946 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
947 cdev->private->dev_id.ssid,
948 cdev->private->dev_id.devno, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100950 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100951 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100952 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 kfree (cdev->private);
954 kfree (cdev);
955 put_device(&sch->dev);
956 if (atomic_dec_and_test(&ccw_device_init_count))
957 wake_up(&ccw_device_init_wq);
958 return;
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 put_device(&cdev->dev);
961out:
962 cdev->private->flags.recog_done = 1;
963 put_device(&sch->dev);
964 wake_up(&cdev->private->wait_q);
965 if (atomic_dec_and_test(&ccw_device_init_count))
966 wake_up(&ccw_device_init_wq);
967}
968
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200969static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100971 struct ccw_device_private *priv;
972 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 struct subchannel *sch;
974
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100975 priv = container_of(work, struct ccw_device_private, kick_work);
976 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200978 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 /* Reset intparm to zeroes. */
980 sch->schib.pmcw.intparm = 0;
981 cio_modify(sch);
982 put_device(&cdev->dev);
983 put_device(&sch->dev);
984}
985
986/*
987 * subchannel recognition done. Called from the state machine.
988 */
989void
990io_subchannel_recog_done(struct ccw_device *cdev)
991{
992 struct subchannel *sch;
993
994 if (css_init_done == 0) {
995 cdev->private->flags.recog_done = 1;
996 return;
997 }
998 switch (cdev->private->state) {
999 case DEV_STATE_NOT_OPER:
1000 cdev->private->flags.recog_done = 1;
1001 /* Remove device found not operational. */
1002 if (!get_device(&cdev->dev))
1003 break;
1004 sch = to_subchannel(cdev->dev.parent);
1005 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001006 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 queue_work(slow_path_wq, &cdev->private->kick_work);
1008 if (atomic_dec_and_test(&ccw_device_init_count))
1009 wake_up(&ccw_device_init_wq);
1010 break;
1011 case DEV_STATE_BOXED:
1012 /* Device did not respond in time. */
1013 case DEV_STATE_OFFLINE:
1014 /*
1015 * We can't register the device in interrupt context so
1016 * we schedule a work item.
1017 */
1018 if (!get_device(&cdev->dev))
1019 break;
1020 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001021 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 queue_work(slow_path_wq, &cdev->private->kick_work);
1023 break;
1024 }
1025}
1026
1027static int
1028io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1029{
1030 int rc;
1031 struct ccw_device_private *priv;
1032
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001033 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001034 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 /* Init private data. */
1037 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001038 priv->dev_id.devno = sch->schib.pmcw.dev;
1039 priv->dev_id.ssid = sch->schid.ssid;
1040 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 priv->state = DEV_STATE_NOT_OPER;
1042 INIT_LIST_HEAD(&priv->cmb_list);
1043 init_waitqueue_head(&priv->wait_q);
1044 init_timer(&priv->timer);
1045
1046 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001047 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1048 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
1050 /* Increase counter of devices currently in recognition. */
1051 atomic_inc(&ccw_device_init_count);
1052
1053 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001054 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001056 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 if (rc) {
1058 if (atomic_dec_and_test(&ccw_device_init_count))
1059 wake_up(&ccw_device_init_wq);
1060 }
1061 return rc;
1062}
1063
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001064static void ccw_device_move_to_sch(struct work_struct *work)
1065{
1066 struct ccw_device_private *priv;
1067 int rc;
1068 struct subchannel *sch;
1069 struct ccw_device *cdev;
1070 struct subchannel *former_parent;
1071
1072 priv = container_of(work, struct ccw_device_private, kick_work);
1073 sch = priv->sch;
1074 cdev = priv->cdev;
1075 former_parent = ccw_device_is_orphan(cdev) ?
1076 NULL : to_subchannel(get_device(cdev->dev.parent));
1077 mutex_lock(&sch->reg_mutex);
1078 /* Try to move the ccw device to its new subchannel. */
1079 rc = device_move(&cdev->dev, &sch->dev);
1080 mutex_unlock(&sch->reg_mutex);
1081 if (rc) {
Michael Ernst139b83d2008-05-07 09:22:54 +02001082 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001083 "0.%x.%04x failed (ret=%d)!\n",
1084 cdev->private->dev_id.ssid,
1085 cdev->private->dev_id.devno, sch->schid.ssid,
1086 sch->schid.sch_no, rc);
1087 css_sch_device_unregister(sch);
1088 goto out;
1089 }
1090 if (former_parent) {
1091 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001092 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001093 spin_unlock_irq(former_parent->lock);
1094 css_sch_device_unregister(former_parent);
1095 /* Reset intparm to zeroes. */
1096 former_parent->schib.pmcw.intparm = 0;
1097 cio_modify(former_parent);
1098 }
1099 sch_attach_device(sch, cdev);
1100out:
1101 if (former_parent)
1102 put_device(&former_parent->dev);
1103 put_device(&cdev->dev);
1104}
1105
Cornelia Huck602b20f2008-01-26 14:10:39 +01001106static void io_subchannel_irq(struct subchannel *sch)
1107{
1108 struct ccw_device *cdev;
1109
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001110 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001111
1112 CIO_TRACE_EVENT(3, "IRQ");
1113 CIO_TRACE_EVENT(3, sch->dev.bus_id);
1114 if (cdev)
1115 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1116}
1117
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001118static void io_subchannel_init_fields(struct subchannel *sch)
1119{
1120 if (cio_is_console(sch->schid))
1121 sch->opm = 0xff;
1122 else
1123 sch->opm = chp_get_sch_opm(sch);
1124 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001125 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001126
1127 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1128 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1129 sch->schib.pmcw.dev, sch->schid.ssid,
1130 sch->schid.sch_no, sch->schib.pmcw.pim,
1131 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1132 /* Initially set up some fields in the pmcw. */
1133 sch->schib.pmcw.ena = 0;
1134 sch->schib.pmcw.csense = 1; /* concurrent sense */
1135 if ((sch->lpm & (sch->lpm - 1)) != 0)
1136 sch->schib.pmcw.mp = 1; /* multipath mode */
1137 /* clean up possible residual cmf stuff */
1138 sch->schib.pmcw.mme = 0;
1139 sch->schib.pmcw.mbfc = 0;
1140 sch->schib.pmcw.mbi = 0;
1141 sch->schib.mba = 0;
1142}
1143
1144static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 struct ccw_device *cdev;
1147 int rc;
1148 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001149 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001151 cdev = sch_get_cdev(sch);
1152 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001153 rc = sysfs_create_group(&sch->dev.kobj,
1154 &io_subchannel_attr_group);
1155 if (rc)
1156 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1157 "attributes for subchannel "
1158 "0.%x.%04x (rc=%d)\n",
1159 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160 /*
1161 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001162 * Throw the delayed uevent for the subchannel, register
1163 * the ccw_device and exit. This happens for all early
1164 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 */
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001166 sch->dev.uevent_suppress = 0;
1167 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001168 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 device_initialize(&cdev->dev);
1170 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171 /*
1172 * Check if the device is already online. If it is
1173 * the reference count needs to be corrected
1174 * (see ccw_device_online and css_init_done for the
1175 * ugly details).
1176 */
1177 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1178 cdev->private->state != DEV_STATE_OFFLINE &&
1179 cdev->private->state != DEV_STATE_BOXED)
1180 get_device(&cdev->dev);
1181 return 0;
1182 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001183 io_subchannel_init_fields(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001184 /*
1185 * First check if a fitting device may be found amongst the
1186 * disconnected devices or in the orphanage.
1187 */
1188 dev_id.devno = sch->schib.pmcw.dev;
1189 dev_id.ssid = sch->schid.ssid;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001190 rc = sysfs_create_group(&sch->dev.kobj,
1191 &io_subchannel_attr_group);
1192 if (rc)
1193 return rc;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001194 /* Allocate I/O subchannel private data. */
1195 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1196 GFP_KERNEL | GFP_DMA);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001197 if (!sch->private) {
1198 rc = -ENOMEM;
1199 goto out_err;
1200 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001201 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1202 if (!cdev)
1203 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1204 &dev_id);
1205 if (cdev) {
1206 /*
1207 * Schedule moving the device until when we have a registered
1208 * subchannel to move to and succeed the probe. We can
1209 * unregister later again, when the probe is through.
1210 */
1211 cdev->private->sch = sch;
1212 PREPARE_WORK(&cdev->private->kick_work,
1213 ccw_device_move_to_sch);
1214 queue_work(slow_path_wq, &cdev->private->kick_work);
1215 return 0;
1216 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001217 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001218 if (IS_ERR(cdev)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001219 rc = PTR_ERR(cdev);
1220 goto out_err;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001221 }
Cornelia Huck8bbace72006-01-11 10:56:22 +01001222 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001223 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001224 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001225 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001226 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 if (cdev->dev.release)
1228 cdev->dev.release(&cdev->dev);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001229 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001231 return 0;
1232out_err:
1233 kfree(sch->private);
1234 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235 return rc;
1236}
1237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001239io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct ccw_device *cdev;
1242 unsigned long flags;
1243
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001244 cdev = sch_get_cdev(sch);
1245 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247 /* Set ccw device to not operational and drop reference. */
1248 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001249 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250 cdev->private->state = DEV_STATE_NOT_OPER;
1251 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001252 ccw_device_unregister(cdev);
1253 put_device(&cdev->dev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001254 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001255 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256 return 0;
1257}
1258
Cornelia Huck602b20f2008-01-26 14:10:39 +01001259static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260{
1261 struct ccw_device *cdev;
1262
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001263 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 if (!cdev)
1265 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001266 return ccw_device_notify(cdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267}
1268
Cornelia Huck602b20f2008-01-26 14:10:39 +01001269static void io_subchannel_verify(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270{
1271 struct ccw_device *cdev;
1272
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001273 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001274 if (cdev)
1275 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1276}
1277
Cornelia Huckc820de32008-07-14 09:58:45 +02001278static int check_for_io_on_path(struct subchannel *sch, int mask)
1279{
1280 int cc;
1281
1282 cc = stsch(sch->schid, &sch->schib);
1283 if (cc)
1284 return 0;
Peter Oberparleiter23d805b2008-07-14 09:58:50 +02001285 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001286 return 1;
1287 return 0;
1288}
1289
1290static void terminate_internal_io(struct subchannel *sch,
1291 struct ccw_device *cdev)
1292{
1293 if (cio_clear(sch)) {
1294 /* Recheck device in case clear failed. */
1295 sch->lpm = 0;
1296 if (cdev->online)
1297 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1298 else
1299 css_schedule_eval(sch->schid);
1300 return;
1301 }
1302 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1303 /* Request retry of internal operation. */
1304 cdev->private->flags.intretry = 1;
1305 /* Call handler. */
1306 if (cdev->handler)
1307 cdev->handler(cdev, cdev->private->intparm,
1308 ERR_PTR(-EIO));
1309}
1310
1311static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312{
1313 struct ccw_device *cdev;
1314
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001315 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 if (!cdev)
1317 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001318 if (check_for_io_on_path(sch, mask)) {
1319 if (cdev->private->state == DEV_STATE_ONLINE)
1320 ccw_device_kill_io(cdev);
1321 else {
1322 terminate_internal_io(sch, cdev);
1323 /* Re-start path verification. */
1324 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1325 }
1326 } else
1327 /* trigger path verification. */
1328 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1329
1330}
1331
1332static int io_subchannel_chp_event(struct subchannel *sch, void *data,
1333 int event)
1334{
1335 int mask;
1336 struct res_acc_data *res_data;
1337
1338 res_data = data;
1339 mask = chp_ssd_get_mask(&sch->ssd_info, res_data);
1340 if (!mask)
1341 return 0;
1342 switch (event) {
1343 case CHP_VARY_OFF:
1344 sch->opm &= ~mask;
1345 sch->lpm &= ~mask;
1346 io_subchannel_terminate_path(sch, mask);
1347 break;
1348 case CHP_VARY_ON:
1349 sch->opm |= mask;
1350 sch->lpm |= mask;
1351 io_subchannel_verify(sch);
1352 break;
1353 case CHP_OFFLINE:
1354 if (stsch(sch->schid, &sch->schib))
1355 return -ENXIO;
1356 if (!css_sch_is_valid(&sch->schib))
1357 return -ENODEV;
1358 io_subchannel_terminate_path(sch, mask);
1359 break;
1360 case CHP_ONLINE:
1361 if (stsch(sch->schid, &sch->schib))
1362 return -ENXIO;
1363 sch->lpm |= mask & sch->opm;
1364 io_subchannel_verify(sch);
1365 break;
1366 }
1367 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001368}
1369
1370static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001371io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001373 struct ccw_device *cdev;
1374 int ret;
1375
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001376 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001378 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379 return;
1380 if (!sch->schib.pmcw.ena)
1381 /* Nothing to do. */
1382 return;
1383 ret = cio_disable_subchannel(sch);
1384 if (ret != -EBUSY)
1385 /* Subchannel is disabled, we're done. */
1386 return;
1387 cdev->private->state = DEV_STATE_QUIESCE;
1388 if (cdev->handler)
1389 cdev->handler(cdev, cdev->private->intparm,
1390 ERR_PTR(-EIO));
1391 ret = ccw_device_cancel_halt_clear(cdev);
1392 if (ret == -EBUSY) {
1393 ccw_device_set_timeout(cdev, HZ/10);
1394 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1395 }
1396 cio_disable_subchannel(sch);
1397}
1398
Cornelia Huckc820de32008-07-14 09:58:45 +02001399static int io_subchannel_get_status(struct subchannel *sch)
1400{
1401 struct schib schib;
1402
1403 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1404 return CIO_GONE;
1405 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1406 return CIO_REVALIDATE;
1407 if (!sch->lpm)
1408 return CIO_NO_PATH;
1409 return CIO_OPER;
1410}
1411
1412static int device_is_disconnected(struct ccw_device *cdev)
1413{
1414 if (!cdev)
1415 return 0;
1416 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1417 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1418}
1419
1420static int recovery_check(struct device *dev, void *data)
1421{
1422 struct ccw_device *cdev = to_ccwdev(dev);
1423 int *redo = data;
1424
1425 spin_lock_irq(cdev->ccwlock);
1426 switch (cdev->private->state) {
1427 case DEV_STATE_DISCONNECTED:
1428 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1429 cdev->private->dev_id.ssid,
1430 cdev->private->dev_id.devno);
1431 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1432 *redo = 1;
1433 break;
1434 case DEV_STATE_DISCONNECTED_SENSE_ID:
1435 *redo = 1;
1436 break;
1437 }
1438 spin_unlock_irq(cdev->ccwlock);
1439
1440 return 0;
1441}
1442
1443static void recovery_work_func(struct work_struct *unused)
1444{
1445 int redo = 0;
1446
1447 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1448 if (redo) {
1449 spin_lock_irq(&recovery_lock);
1450 if (!timer_pending(&recovery_timer)) {
1451 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1452 recovery_phase++;
1453 mod_timer(&recovery_timer, jiffies +
1454 recovery_delay[recovery_phase] * HZ);
1455 }
1456 spin_unlock_irq(&recovery_lock);
1457 } else
1458 CIO_MSG_EVENT(4, "recovery: end\n");
1459}
1460
1461static DECLARE_WORK(recovery_work, recovery_work_func);
1462
1463static void recovery_func(unsigned long data)
1464{
1465 /*
1466 * We can't do our recovery in softirq context and it's not
1467 * performance critical, so we schedule it.
1468 */
1469 schedule_work(&recovery_work);
1470}
1471
1472static void ccw_device_schedule_recovery(void)
1473{
1474 unsigned long flags;
1475
1476 CIO_MSG_EVENT(4, "recovery: schedule\n");
1477 spin_lock_irqsave(&recovery_lock, flags);
1478 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1479 recovery_phase = 0;
1480 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1481 }
1482 spin_unlock_irqrestore(&recovery_lock, flags);
1483}
1484
1485static void device_set_disconnected(struct ccw_device *cdev)
1486{
1487 if (!cdev)
1488 return;
1489 ccw_device_set_timeout(cdev, 0);
1490 cdev->private->flags.fake_irb = 0;
1491 cdev->private->state = DEV_STATE_DISCONNECTED;
1492 if (cdev->online)
1493 ccw_device_schedule_recovery();
1494}
1495
1496static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1497{
1498 int event, ret, disc;
1499 unsigned long flags;
1500 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
1501 struct ccw_device *cdev;
1502
1503 spin_lock_irqsave(sch->lock, flags);
1504 cdev = sch_get_cdev(sch);
1505 disc = device_is_disconnected(cdev);
1506 if (disc && slow) {
1507 /* Disconnected devices are evaluated directly only.*/
1508 spin_unlock_irqrestore(sch->lock, flags);
1509 return 0;
1510 }
1511 /* No interrupt after machine check - kill pending timers. */
1512 if (cdev)
1513 ccw_device_set_timeout(cdev, 0);
1514 if (!disc && !slow) {
1515 /* Non-disconnected devices are evaluated on the slow path. */
1516 spin_unlock_irqrestore(sch->lock, flags);
1517 return -EAGAIN;
1518 }
1519 event = io_subchannel_get_status(sch);
1520 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1521 sch->schid.ssid, sch->schid.sch_no, event,
1522 disc ? "disconnected" : "normal",
1523 slow ? "slow" : "fast");
1524 /* Analyze subchannel status. */
1525 action = NONE;
1526 switch (event) {
1527 case CIO_NO_PATH:
1528 if (disc) {
1529 /* Check if paths have become available. */
1530 action = REPROBE;
1531 break;
1532 }
1533 /* fall through */
1534 case CIO_GONE:
1535 /* Prevent unwanted effects when opening lock. */
1536 cio_disable_subchannel(sch);
1537 device_set_disconnected(cdev);
1538 /* Ask driver what to do with device. */
1539 action = UNREGISTER;
1540 spin_unlock_irqrestore(sch->lock, flags);
1541 ret = io_subchannel_notify(sch, event);
1542 spin_lock_irqsave(sch->lock, flags);
1543 if (ret)
1544 action = NONE;
1545 break;
1546 case CIO_REVALIDATE:
1547 /* Device will be removed, so no notify necessary. */
1548 if (disc)
1549 /* Reprobe because immediate unregister might block. */
1550 action = REPROBE;
1551 else
1552 action = UNREGISTER_PROBE;
1553 break;
1554 case CIO_OPER:
1555 if (disc)
1556 /* Get device operational again. */
1557 action = REPROBE;
1558 break;
1559 }
1560 /* Perform action. */
1561 ret = 0;
1562 switch (action) {
1563 case UNREGISTER:
1564 case UNREGISTER_PROBE:
1565 /* Unregister device (will use subchannel lock). */
1566 spin_unlock_irqrestore(sch->lock, flags);
1567 css_sch_device_unregister(sch);
1568 spin_lock_irqsave(sch->lock, flags);
1569
1570 /* Reset intparm to zeroes. */
1571 sch->schib.pmcw.intparm = 0;
1572 cio_modify(sch);
1573 break;
1574 case REPROBE:
1575 ccw_device_trigger_reprobe(cdev);
1576 break;
1577 default:
1578 break;
1579 }
1580 spin_unlock_irqrestore(sch->lock, flags);
1581 /* Probe if necessary. */
1582 if (action == UNREGISTER_PROBE)
1583 ret = css_probe_device(sch->schid);
1584
1585 return ret;
1586}
1587
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588#ifdef CONFIG_CCW_CONSOLE
1589static struct ccw_device console_cdev;
1590static struct ccw_device_private console_private;
1591static int console_cdev_in_use;
1592
Cornelia Huck2ec22982006-12-08 15:54:26 +01001593static DEFINE_SPINLOCK(ccw_console_lock);
1594
1595spinlock_t * cio_get_console_lock(void)
1596{
1597 return &ccw_console_lock;
1598}
1599
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001600static int ccw_device_console_enable(struct ccw_device *cdev,
1601 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602{
1603 int rc;
1604
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001605 /* Attach subchannel private data. */
1606 sch->private = cio_get_console_priv();
1607 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001608 io_subchannel_init_fields(sch);
1609 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001611 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612 rc = io_subchannel_recog(cdev, sch);
1613 if (rc)
1614 return rc;
1615
1616 /* Now wait for the async. recognition to come to an end. */
1617 spin_lock_irq(cdev->ccwlock);
1618 while (!dev_fsm_final_state(cdev))
1619 wait_cons_dev();
1620 rc = -EIO;
1621 if (cdev->private->state != DEV_STATE_OFFLINE)
1622 goto out_unlock;
1623 ccw_device_online(cdev);
1624 while (!dev_fsm_final_state(cdev))
1625 wait_cons_dev();
1626 if (cdev->private->state != DEV_STATE_ONLINE)
1627 goto out_unlock;
1628 rc = 0;
1629out_unlock:
1630 spin_unlock_irq(cdev->ccwlock);
1631 return 0;
1632}
1633
1634struct ccw_device *
1635ccw_device_probe_console(void)
1636{
1637 struct subchannel *sch;
1638 int ret;
1639
1640 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001641 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642 sch = cio_probe_console();
1643 if (IS_ERR(sch)) {
1644 console_cdev_in_use = 0;
1645 return (void *) sch;
1646 }
1647 memset(&console_cdev, 0, sizeof(struct ccw_device));
1648 memset(&console_private, 0, sizeof(struct ccw_device_private));
1649 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001650 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 ret = ccw_device_console_enable(&console_cdev, sch);
1652 if (ret) {
1653 cio_release_console();
1654 console_cdev_in_use = 0;
1655 return ERR_PTR(ret);
1656 }
1657 console_cdev.online = 1;
1658 return &console_cdev;
1659}
1660#endif
1661
1662/*
1663 * get ccw_device matching the busid, but only if owned by cdrv
1664 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001665static int
1666__ccwdev_check_busid(struct device *dev, void *id)
1667{
1668 char *bus_id;
1669
Cornelia Huck12975ae2006-10-11 15:31:47 +02001670 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001671
1672 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1673}
1674
1675
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001676/**
1677 * get_ccwdev_by_busid() - obtain device from a bus id
1678 * @cdrv: driver the device is owned by
1679 * @bus_id: bus id of the device to be searched
1680 *
1681 * This function searches all devices owned by @cdrv for a device with a bus
1682 * id matching @bus_id.
1683 * Returns:
1684 * If a match is found, its reference count of the found device is increased
1685 * and it is returned; else %NULL is returned.
1686 */
1687struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1688 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001690 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 struct device_driver *drv;
1692
1693 drv = get_driver(&cdrv->driver);
1694 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001695 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001697 dev = driver_find_device(drv, NULL, (void *)bus_id,
1698 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 put_driver(drv);
1700
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001701 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702}
1703
1704/************************** device driver handling ************************/
1705
1706/* This is the implementation of the ccw_driver class. The probe, remove
1707 * and release methods are initially very similar to the device_driver
1708 * implementations, with the difference that they have ccw_device
1709 * arguments.
1710 *
1711 * A ccw driver also contains the information that is needed for
1712 * device matching.
1713 */
1714static int
1715ccw_device_probe (struct device *dev)
1716{
1717 struct ccw_device *cdev = to_ccwdev(dev);
1718 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1719 int ret;
1720
1721 cdev->drv = cdrv; /* to let the driver call _set_online */
1722
1723 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1724
1725 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001726 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 return ret;
1728 }
1729
1730 return 0;
1731}
1732
1733static int
1734ccw_device_remove (struct device *dev)
1735{
1736 struct ccw_device *cdev = to_ccwdev(dev);
1737 struct ccw_driver *cdrv = cdev->drv;
1738 int ret;
1739
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 if (cdrv->remove)
1741 cdrv->remove(cdev);
1742 if (cdev->online) {
1743 cdev->online = 0;
1744 spin_lock_irq(cdev->ccwlock);
1745 ret = ccw_device_offline(cdev);
1746 spin_unlock_irq(cdev->ccwlock);
1747 if (ret == 0)
1748 wait_event(cdev->private->wait_q,
1749 dev_fsm_final_state(cdev));
1750 else
Michael Ernst139b83d2008-05-07 09:22:54 +02001751 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001752 "device 0.%x.%04x\n",
1753 ret, cdev->private->dev_id.ssid,
1754 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 }
1756 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001757 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 return 0;
1759}
1760
Cornelia Huck958974fb2007-10-12 16:11:21 +02001761static void ccw_device_shutdown(struct device *dev)
1762{
1763 struct ccw_device *cdev;
1764
1765 cdev = to_ccwdev(dev);
1766 if (cdev->drv && cdev->drv->shutdown)
1767 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001768 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001769}
1770
Cornelia Huck8bbace72006-01-11 10:56:22 +01001771struct bus_type ccw_bus_type = {
1772 .name = "ccw",
1773 .match = ccw_bus_match,
1774 .uevent = ccw_uevent,
1775 .probe = ccw_device_probe,
1776 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001777 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001778};
1779
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001780/**
1781 * ccw_driver_register() - register a ccw driver
1782 * @cdriver: driver to be registered
1783 *
1784 * This function is mainly a wrapper around driver_register().
1785 * Returns:
1786 * %0 on success and a negative error value on failure.
1787 */
1788int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789{
1790 struct device_driver *drv = &cdriver->driver;
1791
1792 drv->bus = &ccw_bus_type;
1793 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001794 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795
1796 return driver_register(drv);
1797}
1798
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001799/**
1800 * ccw_driver_unregister() - deregister a ccw driver
1801 * @cdriver: driver to be deregistered
1802 *
1803 * This function is mainly a wrapper around driver_unregister().
1804 */
1805void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806{
1807 driver_unregister(&cdriver->driver);
1808}
1809
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001810/* Helper func for qdio. */
1811struct subchannel_id
1812ccw_device_get_subchannel_id(struct ccw_device *cdev)
1813{
1814 struct subchannel *sch;
1815
1816 sch = to_subchannel(cdev->dev.parent);
1817 return sch->schid;
1818}
1819
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820MODULE_LICENSE("GPL");
1821EXPORT_SYMBOL(ccw_device_set_online);
1822EXPORT_SYMBOL(ccw_device_set_offline);
1823EXPORT_SYMBOL(ccw_driver_register);
1824EXPORT_SYMBOL(ccw_driver_unregister);
1825EXPORT_SYMBOL(get_ccwdev_by_busid);
1826EXPORT_SYMBOL(ccw_bus_type);
1827EXPORT_SYMBOL(ccw_device_work);
1828EXPORT_SYMBOL(ccw_device_notify_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001829EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);