blob: 38a79ecfc743edff2eaee6b746c4a16c2ec18796 [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);
Cornelia Huck99611f82008-07-14 09:59:02 +0200131static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
132 int);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133
Cornelia Huckf08adc02008-07-14 09:59:03 +0200134static 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
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200140static struct css_driver io_subchannel_driver = {
Cornelia Huck4beee642008-01-26 14:10:47 +0100141 .owner = THIS_MODULE,
Cornelia Huckf08adc02008-07-14 09:59:03 +0200142 .subchannel_type = io_subchannel_ids,
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100143 .name = "io_subchannel",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 .irq = io_subchannel_irq,
Cornelia Huckc820de32008-07-14 09:58:45 +0200145 .sch_event = io_subchannel_sch_event,
146 .chp_event = io_subchannel_chp_event,
Cornelia Huck8bbace72006-01-11 10:56:22 +0100147 .probe = io_subchannel_probe,
148 .remove = io_subchannel_remove,
149 .shutdown = io_subchannel_shutdown,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150};
151
152struct workqueue_struct *ccw_device_work;
Peter Oberparleiter40154b82006-06-29 14:57:03 +0200153wait_queue_head_t ccw_device_init_wq;
154atomic_t ccw_device_init_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100156static void recovery_func(unsigned long data);
157
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158static int __init
159init_ccw_bus_type (void)
160{
161 int ret;
162
163 init_waitqueue_head(&ccw_device_init_wq);
164 atomic_set(&ccw_device_init_count, 0);
Peter Oberparleiter90ab1332008-01-26 14:10:52 +0100165 setup_timer(&recovery_timer, recovery_func, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 ccw_device_work = create_singlethread_workqueue("cio");
168 if (!ccw_device_work)
169 return -ENOMEM; /* FIXME: better errno ? */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 slow_path_wq = create_singlethread_workqueue("kslowcrw");
171 if (!slow_path_wq) {
172 ret = -ENOMEM; /* FIXME: better errno ? */
173 goto out_err;
174 }
175 if ((ret = bus_register (&ccw_bus_type)))
176 goto out_err;
177
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100178 ret = css_driver_register(&io_subchannel_driver);
179 if (ret)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 goto out_err;
181
182 wait_event(ccw_device_init_wq,
183 atomic_read(&ccw_device_init_count) == 0);
184 flush_workqueue(ccw_device_work);
185 return 0;
186out_err:
187 if (ccw_device_work)
188 destroy_workqueue(ccw_device_work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (slow_path_wq)
190 destroy_workqueue(slow_path_wq);
191 return ret;
192}
193
194static void __exit
195cleanup_ccw_bus_type (void)
196{
Cornelia Huck25b7bb52008-01-26 14:10:41 +0100197 css_driver_unregister(&io_subchannel_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 bus_unregister(&ccw_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 destroy_workqueue(ccw_device_work);
200}
201
202subsys_initcall(init_ccw_bus_type);
203module_exit(cleanup_ccw_bus_type);
204
205/************************ device handling **************************/
206
207/*
208 * A ccw_device has some interfaces in sysfs in addition to the
209 * standard ones.
210 * The following entries are designed to export the information which
211 * resided in 2.4 in /proc/subchannels. Subchannel and device number
212 * are obvious, so they don't have an entry :)
213 * TODO: Split chpids and pimpampom up? Where is "in use" in the tree?
214 */
215static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400216chpids_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
218 struct subchannel *sch = to_subchannel(dev);
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200219 struct chsc_ssd_info *ssd = &sch->ssd_info;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 ssize_t ret = 0;
221 int chp;
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200222 int mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223
Peter Oberparleiter7ad6a242007-04-27 16:01:35 +0200224 for (chp = 0; chp < 8; chp++) {
225 mask = 0x80 >> chp;
226 if (ssd->path_mask & mask)
227 ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id);
228 else
229 ret += sprintf(buf + ret, "00 ");
230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 ret += sprintf (buf+ret, "\n");
232 return min((ssize_t)PAGE_SIZE, ret);
233}
234
235static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400236pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237{
238 struct subchannel *sch = to_subchannel(dev);
239 struct pmcw *pmcw = &sch->schib.pmcw;
240
241 return sprintf (buf, "%02x %02x %02x\n",
242 pmcw->pim, pmcw->pam, pmcw->pom);
243}
244
245static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400246devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247{
248 struct ccw_device *cdev = to_ccwdev(dev);
249 struct ccw_device_id *id = &(cdev->id);
250
251 if (id->dev_type != 0)
252 return sprintf(buf, "%04x/%02x\n",
253 id->dev_type, id->dev_model);
254 else
255 return sprintf(buf, "n/a\n");
256}
257
258static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400259cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct ccw_device *cdev = to_ccwdev(dev);
262 struct ccw_device_id *id = &(cdev->id);
263
264 return sprintf(buf, "%04x/%02x\n",
265 id->cu_type, id->cu_model);
266}
267
268static ssize_t
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800269modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
270{
271 struct ccw_device *cdev = to_ccwdev(dev);
272 struct ccw_device_id *id = &(cdev->id);
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200273 int len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800274
Cornelia Huck086a6c62007-07-17 13:36:08 +0200275 len = snprint_alias(buf, PAGE_SIZE, id, "\n");
Peter Oberparleiterdb0c2d52006-09-20 15:59:49 +0200276
277 return len > PAGE_SIZE ? PAGE_SIZE : len;
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800278}
279
280static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400281online_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282{
283 struct ccw_device *cdev = to_ccwdev(dev);
284
285 return sprintf(buf, cdev->online ? "1\n" : "0\n");
286}
287
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100288int ccw_device_is_orphan(struct ccw_device *cdev)
289{
290 return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
291}
292
Cornelia Huckef995162007-04-27 16:01:39 +0200293static void ccw_device_unregister(struct ccw_device *cdev)
Cornelia Huck7674da72006-12-08 15:54:21 +0100294{
Cornelia Huck7674da72006-12-08 15:54:21 +0100295 if (test_and_clear_bit(1, &cdev->private->registered))
Cornelia Huckef995162007-04-27 16:01:39 +0200296 device_del(&cdev->dev);
Cornelia Huck7674da72006-12-08 15:54:21 +0100297}
298
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200299static void ccw_device_remove_orphan_cb(struct work_struct *work)
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200300{
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200301 struct ccw_device_private *priv;
302 struct ccw_device *cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200303
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200304 priv = container_of(work, struct ccw_device_private, kick_work);
305 cdev = priv->cdev;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200306 ccw_device_unregister(cdev);
307 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200308 /* Release cdev reference for workqueue processing. */
309 put_device(&cdev->dev);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200310}
311
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200312static void ccw_device_call_sch_unregister(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200314static void
315ccw_device_remove_disconnected(struct ccw_device *cdev)
316{
317 unsigned long flags;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200318
319 /*
320 * Forced offline in disconnected state means
321 * 'throw away device'.
322 */
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200323 /* Get cdev reference for workqueue processing. */
324 if (!get_device(&cdev->dev))
325 return;
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200326 if (ccw_device_is_orphan(cdev)) {
327 /*
328 * Deregister ccw device.
329 * Unfortunately, we cannot do this directly from the
330 * attribute method.
331 */
332 spin_lock_irqsave(cdev->ccwlock, flags);
333 cdev->private->state = DEV_STATE_NOT_OPER;
334 spin_unlock_irqrestore(cdev->ccwlock, flags);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200335 PREPARE_WORK(&cdev->private->kick_work,
336 ccw_device_remove_orphan_cb);
337 } else
338 /* Deregister subchannel, which will kill the ccw device. */
339 PREPARE_WORK(&cdev->private->kick_work,
340 ccw_device_call_sch_unregister);
341 queue_work(slow_path_wq, &cdev->private->kick_work);
Cornelia Huck59a8a6e2007-05-31 17:38:06 +0200342}
343
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200344/**
345 * ccw_device_set_offline() - disable a ccw device for I/O
346 * @cdev: target ccw device
347 *
348 * This function calls the driver's set_offline() function for @cdev, if
349 * given, and then disables @cdev.
350 * Returns:
351 * %0 on success and a negative error value on failure.
352 * Context:
353 * enabled, ccw device lock not held
354 */
355int ccw_device_set_offline(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356{
357 int ret;
358
359 if (!cdev)
360 return -ENODEV;
361 if (!cdev->online || !cdev->drv)
362 return -EINVAL;
363
364 if (cdev->drv->set_offline) {
365 ret = cdev->drv->set_offline(cdev);
366 if (ret != 0)
367 return ret;
368 }
369 cdev->online = 0;
370 spin_lock_irq(cdev->ccwlock);
371 ret = ccw_device_offline(cdev);
372 if (ret == -ENODEV) {
373 if (cdev->private->state != DEV_STATE_NOT_OPER) {
374 cdev->private->state = DEV_STATE_OFFLINE;
375 dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
376 }
377 spin_unlock_irq(cdev->ccwlock);
378 return ret;
379 }
380 spin_unlock_irq(cdev->ccwlock);
381 if (ret == 0)
382 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
383 else {
Michael Ernst139b83dd2008-05-07 09:22:54 +0200384 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200385 "device 0.%x.%04x\n",
386 ret, cdev->private->dev_id.ssid,
387 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 cdev->online = 1;
389 }
390 return ret;
391}
392
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +0200393/**
394 * ccw_device_set_online() - enable a ccw device for I/O
395 * @cdev: target ccw device
396 *
397 * This function first enables @cdev and then calls the driver's set_online()
398 * function for @cdev, if given. If set_online() returns an error, @cdev is
399 * disabled again.
400 * Returns:
401 * %0 on success and a negative error value on failure.
402 * Context:
403 * enabled, ccw device lock not held
404 */
405int ccw_device_set_online(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406{
407 int ret;
408
409 if (!cdev)
410 return -ENODEV;
411 if (cdev->online || !cdev->drv)
412 return -EINVAL;
413
414 spin_lock_irq(cdev->ccwlock);
415 ret = ccw_device_online(cdev);
416 spin_unlock_irq(cdev->ccwlock);
417 if (ret == 0)
418 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
419 else {
Michael Ernst139b83dd2008-05-07 09:22:54 +0200420 CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200421 "device 0.%x.%04x\n",
422 ret, cdev->private->dev_id.ssid,
423 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 return ret;
425 }
426 if (cdev->private->state != DEV_STATE_ONLINE)
427 return -ENODEV;
428 if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) {
429 cdev->online = 1;
430 return 0;
431 }
432 spin_lock_irq(cdev->ccwlock);
433 ret = ccw_device_offline(cdev);
434 spin_unlock_irq(cdev->ccwlock);
435 if (ret == 0)
436 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200437 else
Michael Ernst139b83dd2008-05-07 09:22:54 +0200438 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200439 "device 0.%x.%04x\n",
440 ret, cdev->private->dev_id.ssid,
441 cdev->private->dev_id.devno);
Cornelia Huck6cadb782006-02-17 13:52:49 -0800442 return (ret == 0) ? -ENODEV : ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200445static void online_store_handle_offline(struct ccw_device *cdev)
446{
447 if (cdev->private->state == DEV_STATE_DISCONNECTED)
448 ccw_device_remove_disconnected(cdev);
449 else if (cdev->drv && cdev->drv->set_offline)
450 ccw_device_set_offline(cdev);
451}
452
453static int online_store_recog_and_online(struct ccw_device *cdev)
454{
455 int ret;
456
457 /* Do device recognition, if needed. */
458 if (cdev->id.cu_type == 0) {
459 ret = ccw_device_recognition(cdev);
460 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200461 CIO_MSG_EVENT(0, "Couldn't start recognition "
462 "for device 0.%x.%04x (ret=%d)\n",
463 cdev->private->dev_id.ssid,
464 cdev->private->dev_id.devno, ret);
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200465 return ret;
466 }
467 wait_event(cdev->private->wait_q,
468 cdev->private->flags.recog_done);
469 }
470 if (cdev->drv && cdev->drv->set_online)
471 ccw_device_set_online(cdev);
472 return 0;
473}
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200474static int online_store_handle_online(struct ccw_device *cdev, int force)
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200475{
476 int ret;
477
478 ret = online_store_recog_and_online(cdev);
479 if (ret)
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200480 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200481 if (force && cdev->private->state == DEV_STATE_BOXED) {
482 ret = ccw_device_stlck(cdev);
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200483 if (ret)
484 return ret;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200485 if (cdev->id.cu_type == 0)
486 cdev->private->state = DEV_STATE_NOT_OPER;
487 online_store_recog_and_online(cdev);
488 }
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200489 return 0;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200490}
491
492static ssize_t online_store (struct device *dev, struct device_attribute *attr,
493 const char *buf, size_t count)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494{
495 struct ccw_device *cdev = to_ccwdev(dev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200496 int force, ret;
497 unsigned long i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Martin Schwidefsky973bd992006-01-06 00:19:07 -0800499 if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 return -EAGAIN;
501
502 if (cdev->drv && !try_module_get(cdev->drv->owner)) {
503 atomic_set(&cdev->private->onoff, 0);
504 return -EINVAL;
505 }
506 if (!strncmp(buf, "force\n", count)) {
507 force = 1;
508 i = 1;
Cornelia Huck2f972202008-04-30 13:38:33 +0200509 ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 } else {
511 force = 0;
Cornelia Huck2f972202008-04-30 13:38:33 +0200512 ret = strict_strtoul(buf, 16, &i);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200514 if (ret)
515 goto out;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200516 switch (i) {
517 case 0:
518 online_store_handle_offline(cdev);
Cornelia Huck2f972202008-04-30 13:38:33 +0200519 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200520 break;
521 case 1:
Michael Ernstc78aa6c2008-07-14 09:59:22 +0200522 ret = online_store_handle_online(cdev, force);
523 if (!ret)
524 ret = count;
Cornelia Huckf5ba6c82007-04-27 16:01:30 +0200525 break;
526 default:
Cornelia Huck2f972202008-04-30 13:38:33 +0200527 ret = -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 }
Cornelia Huck2f972202008-04-30 13:38:33 +0200529out:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 if (cdev->drv)
531 module_put(cdev->drv->owner);
532 atomic_set(&cdev->private->onoff, 0);
Cornelia Huck2f972202008-04-30 13:38:33 +0200533 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534}
535
536static ssize_t
Yani Ioannou3fd3c0a2005-05-17 06:43:27 -0400537available_show (struct device *dev, struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538{
539 struct ccw_device *cdev = to_ccwdev(dev);
540 struct subchannel *sch;
541
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100542 if (ccw_device_is_orphan(cdev))
543 return sprintf(buf, "no device\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 switch (cdev->private->state) {
545 case DEV_STATE_BOXED:
546 return sprintf(buf, "boxed\n");
547 case DEV_STATE_DISCONNECTED:
548 case DEV_STATE_DISCONNECTED_SENSE_ID:
549 case DEV_STATE_NOT_OPER:
550 sch = to_subchannel(dev->parent);
551 if (!sch->lpm)
552 return sprintf(buf, "no path\n");
553 else
554 return sprintf(buf, "no device\n");
555 default:
556 /* All other states considered fine. */
557 return sprintf(buf, "good\n");
558 }
559}
560
561static DEVICE_ATTR(chpids, 0444, chpids_show, NULL);
562static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL);
563static DEVICE_ATTR(devtype, 0444, devtype_show, NULL);
564static DEVICE_ATTR(cutype, 0444, cutype_show, NULL);
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800565static DEVICE_ATTR(modalias, 0444, modalias_show, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566static DEVICE_ATTR(online, 0644, online_show, online_store);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567static DEVICE_ATTR(availability, 0444, available_show, NULL);
568
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200569static struct attribute *io_subchannel_attrs[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 &dev_attr_chpids.attr,
571 &dev_attr_pimpampom.attr,
572 NULL,
573};
574
Cornelia Huck7e9db9e2008-07-14 09:58:44 +0200575static struct attribute_group io_subchannel_attr_group = {
576 .attrs = io_subchannel_attrs,
Cornelia Huck529192f2006-12-08 15:55:57 +0100577};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579static struct attribute * ccwdev_attrs[] = {
580 &dev_attr_devtype.attr,
581 &dev_attr_cutype.attr,
Bastian Blankf1fc78a2005-10-30 15:00:12 -0800582 &dev_attr_modalias.attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 &dev_attr_online.attr,
584 &dev_attr_cmb_enable.attr,
585 &dev_attr_availability.attr,
586 NULL,
587};
588
589static struct attribute_group ccwdev_attr_group = {
590 .attrs = ccwdev_attrs,
591};
592
Cornelia Huckf7e5d672007-05-10 15:45:43 +0200593static struct attribute_group *ccwdev_attr_groups[] = {
Cornelia Huckef995162007-04-27 16:01:39 +0200594 &ccwdev_attr_group,
595 NULL,
596};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597
598/* this is a simple abstraction for device_register that sets the
599 * correct bus type and adds the bus specific files */
Cornelia Huck3c9da7b2006-10-27 12:39:33 +0200600static int ccw_device_register(struct ccw_device *cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601{
602 struct device *dev = &cdev->dev;
603 int ret;
604
605 dev->bus = &ccw_bus_type;
606
607 if ((ret = device_add(dev)))
608 return ret;
609
610 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 return ret;
612}
613
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700614struct match_data {
Cornelia Huck78964262006-10-11 15:31:38 +0200615 struct ccw_dev_id dev_id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700616 struct ccw_device * sibling;
617};
618
619static int
620match_devno(struct device * dev, void * data)
621{
Cornelia Huck78964262006-10-11 15:31:38 +0200622 struct match_data * d = data;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700623 struct ccw_device * cdev;
624
625 cdev = to_ccwdev(dev);
626 if ((cdev->private->state == DEV_STATE_DISCONNECTED) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100627 !ccw_device_is_orphan(cdev) &&
Cornelia Huck78964262006-10-11 15:31:38 +0200628 ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) &&
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100629 (cdev != d->sibling))
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700630 return 1;
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700631 return 0;
632}
633
Cornelia Huck78964262006-10-11 15:31:38 +0200634static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id,
635 struct ccw_device *sibling)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 struct device *dev;
Heiko Carstens292888c2006-08-30 14:33:35 +0200638 struct match_data data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
Cornelia Huck78964262006-10-11 15:31:38 +0200640 data.dev_id = *dev_id;
Heiko Carstens292888c2006-08-30 14:33:35 +0200641 data.sibling = sibling;
Cornelia Hucke5945b42005-10-11 08:28:59 -0700642 dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700644 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645}
646
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100647static int match_orphan(struct device *dev, void *data)
648{
649 struct ccw_dev_id *dev_id;
650 struct ccw_device *cdev;
651
652 dev_id = data;
653 cdev = to_ccwdev(dev);
654 return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
655}
656
657static struct ccw_device *
658get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css,
659 struct ccw_dev_id *dev_id)
660{
661 struct device *dev;
662
663 dev = device_find_child(&css->pseudo_subchannel->dev, dev_id,
664 match_orphan);
665
666 return dev ? to_ccwdev(dev) : NULL;
667}
668
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100670ccw_device_add_changed(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100672 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 struct ccw_device *cdev;
674
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100675 priv = container_of(work, struct ccw_device_private, kick_work);
676 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (device_add(&cdev->dev)) {
678 put_device(&cdev->dev);
679 return;
680 }
681 set_bit(1, &cdev->private->registered);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682}
683
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100684void ccw_device_do_unreg_rereg(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100686 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 struct ccw_device *cdev;
688 struct subchannel *sch;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100690 priv = container_of(work, struct ccw_device_private, kick_work);
691 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 sch = to_subchannel(cdev->dev.parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
Cornelia Huckef995162007-04-27 16:01:39 +0200694 ccw_device_unregister(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100696 ccw_device_add_changed);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 queue_work(ccw_device_work, &cdev->private->kick_work);
698}
699
700static void
701ccw_device_release(struct device *dev)
702{
703 struct ccw_device *cdev;
704
705 cdev = to_ccwdev(dev);
706 kfree(cdev->private);
707 kfree(cdev);
708}
709
Cornelia Huck7674da72006-12-08 15:54:21 +0100710static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
711{
712 struct ccw_device *cdev;
713
714 cdev = kzalloc(sizeof(*cdev), GFP_KERNEL);
715 if (cdev) {
716 cdev->private = kzalloc(sizeof(struct ccw_device_private),
717 GFP_KERNEL | GFP_DMA);
718 if (cdev->private)
719 return cdev;
720 }
721 kfree(cdev);
722 return ERR_PTR(-ENOMEM);
723}
724
725static int io_subchannel_initialize_dev(struct subchannel *sch,
726 struct ccw_device *cdev)
727{
728 cdev->private->cdev = cdev;
729 atomic_set(&cdev->private->onoff, 0);
730 cdev->dev.parent = &sch->dev;
731 cdev->dev.release = ccw_device_release;
Heiko Carstens33583c32007-11-05 11:10:07 +0100732 INIT_WORK(&cdev->private->kick_work, NULL);
Cornelia Huckef995162007-04-27 16:01:39 +0200733 cdev->dev.groups = ccwdev_attr_groups;
Cornelia Huck7674da72006-12-08 15:54:21 +0100734 /* Do first half of device_register. */
735 device_initialize(&cdev->dev);
736 if (!get_device(&sch->dev)) {
737 if (cdev->dev.release)
738 cdev->dev.release(&cdev->dev);
739 return -ENODEV;
740 }
741 return 0;
742}
743
744static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
745{
746 struct ccw_device *cdev;
747 int ret;
748
749 cdev = io_subchannel_allocate_dev(sch);
750 if (!IS_ERR(cdev)) {
751 ret = io_subchannel_initialize_dev(sch, cdev);
752 if (ret) {
753 kfree(cdev);
754 cdev = ERR_PTR(ret);
755 }
756 }
757 return cdev;
758}
759
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100760static int io_subchannel_recog(struct ccw_device *, struct subchannel *);
761
762static void sch_attach_device(struct subchannel *sch,
763 struct ccw_device *cdev)
764{
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200765 css_update_ssd_info(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100766 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100767 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100768 cdev->private->schid = sch->schid;
769 cdev->ccwlock = sch->lock;
Cornelia Huckc820de32008-07-14 09:58:45 +0200770 ccw_device_trigger_reprobe(cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100771 spin_unlock_irq(sch->lock);
772}
773
774static void sch_attach_disconnected_device(struct subchannel *sch,
775 struct ccw_device *cdev)
776{
777 struct subchannel *other_sch;
778 int ret;
779
780 other_sch = to_subchannel(get_device(cdev->dev.parent));
781 ret = device_move(&cdev->dev, &sch->dev);
782 if (ret) {
Michael Ernst139b83dd2008-05-07 09:22:54 +0200783 CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100784 "(ret=%d)!\n", cdev->private->dev_id.ssid,
785 cdev->private->dev_id.devno, ret);
786 put_device(&other_sch->dev);
787 return;
788 }
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100789 sch_set_cdev(other_sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100790 /* No need to keep a subchannel without ccw device around. */
791 css_sch_device_unregister(other_sch);
792 put_device(&other_sch->dev);
793 sch_attach_device(sch, cdev);
794}
795
796static void sch_attach_orphaned_device(struct subchannel *sch,
797 struct ccw_device *cdev)
798{
799 int ret;
800
801 /* Try to move the ccw device to its new subchannel. */
802 ret = device_move(&cdev->dev, &sch->dev);
803 if (ret) {
804 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage "
805 "failed (ret=%d)!\n",
806 cdev->private->dev_id.ssid,
807 cdev->private->dev_id.devno, ret);
808 return;
809 }
810 sch_attach_device(sch, cdev);
811}
812
813static void sch_create_and_recog_new_device(struct subchannel *sch)
814{
815 struct ccw_device *cdev;
816
817 /* Need to allocate a new ccw device. */
818 cdev = io_subchannel_create_ccwdev(sch);
819 if (IS_ERR(cdev)) {
820 /* OK, we did everything we could... */
821 css_sch_device_unregister(sch);
822 return;
823 }
824 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100825 sch_set_cdev(sch, cdev);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100826 spin_unlock_irq(sch->lock);
827 /* Start recognition for the new ccw device. */
828 if (io_subchannel_recog(cdev, sch)) {
829 spin_lock_irq(sch->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100830 sch_set_cdev(sch, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +0100831 spin_unlock_irq(sch->lock);
832 if (cdev->dev.release)
833 cdev->dev.release(&cdev->dev);
834 css_sch_device_unregister(sch);
835 }
836}
837
838
839void ccw_device_move_to_orphanage(struct work_struct *work)
840{
841 struct ccw_device_private *priv;
842 struct ccw_device *cdev;
843 struct ccw_device *replacing_cdev;
844 struct subchannel *sch;
845 int ret;
846 struct channel_subsystem *css;
847 struct ccw_dev_id dev_id;
848
849 priv = container_of(work, struct ccw_device_private, kick_work);
850 cdev = priv->cdev;
851 sch = to_subchannel(cdev->dev.parent);
852 css = to_css(sch->dev.parent);
853 dev_id.devno = sch->schib.pmcw.dev;
854 dev_id.ssid = sch->schid.ssid;
855
856 /*
857 * Move the orphaned ccw device to the orphanage so the replacing
858 * ccw device can take its place on the subchannel.
859 */
860 ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev);
861 if (ret) {
862 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed "
863 "(ret=%d)!\n", cdev->private->dev_id.ssid,
864 cdev->private->dev_id.devno, ret);
865 return;
866 }
867 cdev->ccwlock = css->pseudo_subchannel->lock;
868 /*
869 * Search for the replacing ccw device
870 * - among the disconnected devices
871 * - in the orphanage
872 */
873 replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev);
874 if (replacing_cdev) {
875 sch_attach_disconnected_device(sch, replacing_cdev);
876 return;
877 }
878 replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id);
879 if (replacing_cdev) {
880 sch_attach_orphaned_device(sch, replacing_cdev);
881 return;
882 }
883 sch_create_and_recog_new_device(sch);
884}
885
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886/*
887 * Register recognized device.
888 */
889static void
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100890io_subchannel_register(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100892 struct ccw_device_private *priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893 struct ccw_device *cdev;
894 struct subchannel *sch;
895 int ret;
896 unsigned long flags;
897
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100898 priv = container_of(work, struct ccw_device_private, kick_work);
899 cdev = priv->cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck82b7ac02007-04-27 16:01:36 +0200901 css_update_ssd_info(sch);
Cornelia Huck47af5512006-12-04 15:41:07 +0100902 /*
903 * io_subchannel_register() will also be called after device
904 * recognition has been done for a boxed device (which will already
905 * be registered). We need to reprobe since we may now have sense id
906 * information.
907 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -0700908 if (klist_node_attached(&cdev->dev.knode_parent)) {
Cornelia Huck47af5512006-12-04 15:41:07 +0100909 if (!cdev->drv) {
910 ret = device_reprobe(&cdev->dev);
911 if (ret)
912 /* We can't do much here. */
Michael Ernst139b83dd2008-05-07 09:22:54 +0200913 CIO_MSG_EVENT(0, "device_reprobe() returned"
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200914 " %d for 0.%x.%04x\n", ret,
915 cdev->private->dev_id.ssid,
916 cdev->private->dev_id.devno);
Cornelia Huck47af5512006-12-04 15:41:07 +0100917 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 goto out;
919 }
Cornelia Huckfa1a8c22007-04-26 00:12:03 -0700920 /*
921 * Now we know this subchannel will stay, we can throw
922 * our delayed uevent.
923 */
924 sch->dev.uevent_suppress = 0;
925 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700926 /* make it known to the system */
927 ret = ccw_device_register(cdev);
928 if (ret) {
Cornelia Hucke556bbb2007-07-27 12:29:19 +0200929 CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
930 cdev->private->dev_id.ssid,
931 cdev->private->dev_id.devno, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 put_device(&cdev->dev);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100933 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +0100934 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +0100935 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700936 kfree (cdev->private);
937 kfree (cdev);
938 put_device(&sch->dev);
939 if (atomic_dec_and_test(&ccw_device_init_count))
940 wake_up(&ccw_device_init_wq);
941 return;
942 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 put_device(&cdev->dev);
944out:
945 cdev->private->flags.recog_done = 1;
946 put_device(&sch->dev);
947 wake_up(&cdev->private->wait_q);
948 if (atomic_dec_and_test(&ccw_device_init_count))
949 wake_up(&ccw_device_init_wq);
950}
951
Cornelia Huck3f4cf6e2007-10-12 16:11:26 +0200952static void ccw_device_call_sch_unregister(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953{
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100954 struct ccw_device_private *priv;
955 struct ccw_device *cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 struct subchannel *sch;
957
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100958 priv = container_of(work, struct ccw_device_private, kick_work);
959 cdev = priv->cdev;
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200960 /* Get subchannel reference for local processing. */
961 if (!get_device(cdev->dev.parent))
962 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 sch = to_subchannel(cdev->dev.parent);
Cornelia Huck6ab48792006-07-12 16:39:50 +0200964 css_sch_device_unregister(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965 /* Reset intparm to zeroes. */
966 sch->schib.pmcw.intparm = 0;
967 cio_modify(sch);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200968 /* Release cdev reference for workqueue processing.*/
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 put_device(&cdev->dev);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200970 /* Release subchannel reference for local processing. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 put_device(&sch->dev);
972}
973
974/*
975 * subchannel recognition done. Called from the state machine.
976 */
977void
978io_subchannel_recog_done(struct ccw_device *cdev)
979{
980 struct subchannel *sch;
981
982 if (css_init_done == 0) {
983 cdev->private->flags.recog_done = 1;
984 return;
985 }
986 switch (cdev->private->state) {
987 case DEV_STATE_NOT_OPER:
988 cdev->private->flags.recog_done = 1;
989 /* Remove device found not operational. */
990 if (!get_device(&cdev->dev))
991 break;
992 sch = to_subchannel(cdev->dev.parent);
993 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +0100994 ccw_device_call_sch_unregister);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 queue_work(slow_path_wq, &cdev->private->kick_work);
Peter Oberparleiter46fbe4e2008-10-10 21:33:05 +0200996 /* Release subchannel reference for asynchronous recognition. */
997 put_device(&sch->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 if (atomic_dec_and_test(&ccw_device_init_count))
999 wake_up(&ccw_device_init_wq);
1000 break;
1001 case DEV_STATE_BOXED:
1002 /* Device did not respond in time. */
1003 case DEV_STATE_OFFLINE:
1004 /*
1005 * We can't register the device in interrupt context so
1006 * we schedule a work item.
1007 */
1008 if (!get_device(&cdev->dev))
1009 break;
1010 PREPARE_WORK(&cdev->private->kick_work,
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001011 io_subchannel_register);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 queue_work(slow_path_wq, &cdev->private->kick_work);
1013 break;
1014 }
1015}
1016
1017static int
1018io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
1019{
1020 int rc;
1021 struct ccw_device_private *priv;
1022
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001023 sch_set_cdev(sch, cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001024 cdev->ccwlock = sch->lock;
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001025
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026 /* Init private data. */
1027 priv = cdev->private;
Cornelia Huck78964262006-10-11 15:31:38 +02001028 priv->dev_id.devno = sch->schib.pmcw.dev;
1029 priv->dev_id.ssid = sch->schid.ssid;
1030 priv->schid = sch->schid;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 priv->state = DEV_STATE_NOT_OPER;
1032 INIT_LIST_HEAD(&priv->cmb_list);
1033 init_waitqueue_head(&priv->wait_q);
1034 init_timer(&priv->timer);
1035
1036 /* Set an initial name for the device. */
Cornelia Huckfb6958a2006-01-06 00:19:25 -08001037 snprintf (cdev->dev.bus_id, BUS_ID_SIZE, "0.%x.%04x",
1038 sch->schid.ssid, sch->schib.pmcw.dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039
1040 /* Increase counter of devices currently in recognition. */
1041 atomic_inc(&ccw_device_init_count);
1042
1043 /* Start async. device sensing. */
Cornelia Huck2ec22982006-12-08 15:54:26 +01001044 spin_lock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 rc = ccw_device_recognition(cdev);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001046 spin_unlock_irq(sch->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 if (rc) {
1048 if (atomic_dec_and_test(&ccw_device_init_count))
1049 wake_up(&ccw_device_init_wq);
1050 }
1051 return rc;
1052}
1053
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001054static void ccw_device_move_to_sch(struct work_struct *work)
1055{
1056 struct ccw_device_private *priv;
1057 int rc;
1058 struct subchannel *sch;
1059 struct ccw_device *cdev;
1060 struct subchannel *former_parent;
1061
1062 priv = container_of(work, struct ccw_device_private, kick_work);
1063 sch = priv->sch;
1064 cdev = priv->cdev;
1065 former_parent = ccw_device_is_orphan(cdev) ?
1066 NULL : to_subchannel(get_device(cdev->dev.parent));
1067 mutex_lock(&sch->reg_mutex);
1068 /* Try to move the ccw device to its new subchannel. */
1069 rc = device_move(&cdev->dev, &sch->dev);
1070 mutex_unlock(&sch->reg_mutex);
1071 if (rc) {
Michael Ernst139b83dd2008-05-07 09:22:54 +02001072 CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel "
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001073 "0.%x.%04x failed (ret=%d)!\n",
1074 cdev->private->dev_id.ssid,
1075 cdev->private->dev_id.devno, sch->schid.ssid,
1076 sch->schid.sch_no, rc);
1077 css_sch_device_unregister(sch);
1078 goto out;
1079 }
1080 if (former_parent) {
1081 spin_lock_irq(former_parent->lock);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001082 sch_set_cdev(former_parent, NULL);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001083 spin_unlock_irq(former_parent->lock);
1084 css_sch_device_unregister(former_parent);
1085 /* Reset intparm to zeroes. */
1086 former_parent->schib.pmcw.intparm = 0;
1087 cio_modify(former_parent);
1088 }
1089 sch_attach_device(sch, cdev);
1090out:
1091 if (former_parent)
1092 put_device(&former_parent->dev);
1093 put_device(&cdev->dev);
1094}
1095
Cornelia Huck602b20f2008-01-26 14:10:39 +01001096static void io_subchannel_irq(struct subchannel *sch)
1097{
1098 struct ccw_device *cdev;
1099
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001100 cdev = sch_get_cdev(sch);
Cornelia Huck602b20f2008-01-26 14:10:39 +01001101
1102 CIO_TRACE_EVENT(3, "IRQ");
1103 CIO_TRACE_EVENT(3, sch->dev.bus_id);
1104 if (cdev)
1105 dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
1106}
1107
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001108static void io_subchannel_init_fields(struct subchannel *sch)
1109{
1110 if (cio_is_console(sch->schid))
1111 sch->opm = 0xff;
1112 else
1113 sch->opm = chp_get_sch_opm(sch);
1114 sch->lpm = sch->schib.pmcw.pam & sch->opm;
Cornelia Huck3a3fc292008-07-14 09:58:58 +02001115 sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001116
1117 CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1118 " - PIM = %02X, PAM = %02X, POM = %02X\n",
1119 sch->schib.pmcw.dev, sch->schid.ssid,
1120 sch->schid.sch_no, sch->schib.pmcw.pim,
1121 sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1122 /* Initially set up some fields in the pmcw. */
1123 sch->schib.pmcw.ena = 0;
1124 sch->schib.pmcw.csense = 1; /* concurrent sense */
1125 if ((sch->lpm & (sch->lpm - 1)) != 0)
1126 sch->schib.pmcw.mp = 1; /* multipath mode */
1127 /* clean up possible residual cmf stuff */
1128 sch->schib.pmcw.mme = 0;
1129 sch->schib.pmcw.mbfc = 0;
1130 sch->schib.pmcw.mbi = 0;
1131 sch->schib.mba = 0;
1132}
1133
1134static int io_subchannel_probe(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 struct ccw_device *cdev;
1137 int rc;
1138 unsigned long flags;
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001139 struct ccw_dev_id dev_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001141 cdev = sch_get_cdev(sch);
1142 if (cdev) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001143 rc = sysfs_create_group(&sch->dev.kobj,
1144 &io_subchannel_attr_group);
1145 if (rc)
1146 CIO_MSG_EVENT(0, "Failed to create io subchannel "
1147 "attributes for subchannel "
1148 "0.%x.%04x (rc=%d)\n",
1149 sch->schid.ssid, sch->schid.sch_no, rc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /*
1151 * This subchannel already has an associated ccw_device.
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001152 * Throw the delayed uevent for the subchannel, register
1153 * the ccw_device and exit. This happens for all early
1154 * devices, e.g. the console.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 */
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001156 sch->dev.uevent_suppress = 0;
1157 kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
Cornelia Hucke1031782007-10-12 16:11:23 +02001158 cdev->dev.groups = ccwdev_attr_groups;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 device_initialize(&cdev->dev);
1160 ccw_device_register(cdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 /*
1162 * Check if the device is already online. If it is
1163 * the reference count needs to be corrected
1164 * (see ccw_device_online and css_init_done for the
1165 * ugly details).
1166 */
1167 if (cdev->private->state != DEV_STATE_NOT_OPER &&
1168 cdev->private->state != DEV_STATE_OFFLINE &&
1169 cdev->private->state != DEV_STATE_BOXED)
1170 get_device(&cdev->dev);
1171 return 0;
1172 }
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001173 io_subchannel_init_fields(sch);
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001174 /*
1175 * First check if a fitting device may be found amongst the
1176 * disconnected devices or in the orphanage.
1177 */
1178 dev_id.devno = sch->schib.pmcw.dev;
1179 dev_id.ssid = sch->schid.ssid;
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001180 rc = sysfs_create_group(&sch->dev.kobj,
1181 &io_subchannel_attr_group);
1182 if (rc)
1183 return rc;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001184 /* Allocate I/O subchannel private data. */
1185 sch->private = kzalloc(sizeof(struct io_subchannel_private),
1186 GFP_KERNEL | GFP_DMA);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001187 if (!sch->private) {
1188 rc = -ENOMEM;
1189 goto out_err;
1190 }
Cornelia Huckd7b5a4c92006-12-08 15:54:28 +01001191 cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL);
1192 if (!cdev)
1193 cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent),
1194 &dev_id);
1195 if (cdev) {
1196 /*
1197 * Schedule moving the device until when we have a registered
1198 * subchannel to move to and succeed the probe. We can
1199 * unregister later again, when the probe is through.
1200 */
1201 cdev->private->sch = sch;
1202 PREPARE_WORK(&cdev->private->kick_work,
1203 ccw_device_move_to_sch);
1204 queue_work(slow_path_wq, &cdev->private->kick_work);
1205 return 0;
1206 }
Cornelia Huck7674da72006-12-08 15:54:21 +01001207 cdev = io_subchannel_create_ccwdev(sch);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001208 if (IS_ERR(cdev)) {
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001209 rc = PTR_ERR(cdev);
1210 goto out_err;
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001211 }
Cornelia Huck8bbace72006-01-11 10:56:22 +01001212 rc = io_subchannel_recog(cdev, sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001213 if (rc) {
Cornelia Huck2ec22982006-12-08 15:54:26 +01001214 spin_lock_irqsave(sch->lock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001215 sch_set_cdev(sch, NULL);
Cornelia Huck2ec22982006-12-08 15:54:26 +01001216 spin_unlock_irqrestore(sch->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 if (cdev->dev.release)
1218 cdev->dev.release(&cdev->dev);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001219 goto out_err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 }
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001221 return 0;
1222out_err:
1223 kfree(sch->private);
1224 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 return rc;
1226}
1227
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228static int
Cornelia Huck8bbace72006-01-11 10:56:22 +01001229io_subchannel_remove (struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230{
1231 struct ccw_device *cdev;
1232 unsigned long flags;
1233
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001234 cdev = sch_get_cdev(sch);
1235 if (!cdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 /* Set ccw device to not operational and drop reference. */
1238 spin_lock_irqsave(cdev->ccwlock, flags);
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001239 sch_set_cdev(sch, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 cdev->private->state = DEV_STATE_NOT_OPER;
1241 spin_unlock_irqrestore(cdev->ccwlock, flags);
Cornelia Huckef995162007-04-27 16:01:39 +02001242 ccw_device_unregister(cdev);
1243 put_device(&cdev->dev);
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001244 kfree(sch->private);
Cornelia Huck7e9db9e2008-07-14 09:58:44 +02001245 sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246 return 0;
1247}
1248
Cornelia Huck602b20f2008-01-26 14:10:39 +01001249static int io_subchannel_notify(struct subchannel *sch, int event)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250{
1251 struct ccw_device *cdev;
1252
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001253 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (!cdev)
1255 return 0;
Cornelia Huckc820de32008-07-14 09:58:45 +02001256 return ccw_device_notify(cdev, event);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257}
1258
Cornelia Huck602b20f2008-01-26 14:10:39 +01001259static void io_subchannel_verify(struct subchannel *sch)
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 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1266}
1267
Cornelia Huckc820de32008-07-14 09:58:45 +02001268static int check_for_io_on_path(struct subchannel *sch, int mask)
1269{
1270 int cc;
1271
1272 cc = stsch(sch->schid, &sch->schib);
1273 if (cc)
1274 return 0;
Peter Oberparleiter23d805b62008-07-14 09:58:50 +02001275 if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask)
Cornelia Huckc820de32008-07-14 09:58:45 +02001276 return 1;
1277 return 0;
1278}
1279
1280static void terminate_internal_io(struct subchannel *sch,
1281 struct ccw_device *cdev)
1282{
1283 if (cio_clear(sch)) {
1284 /* Recheck device in case clear failed. */
1285 sch->lpm = 0;
1286 if (cdev->online)
1287 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1288 else
1289 css_schedule_eval(sch->schid);
1290 return;
1291 }
1292 cdev->private->state = DEV_STATE_CLEAR_VERIFY;
1293 /* Request retry of internal operation. */
1294 cdev->private->flags.intretry = 1;
1295 /* Call handler. */
1296 if (cdev->handler)
1297 cdev->handler(cdev, cdev->private->intparm,
1298 ERR_PTR(-EIO));
1299}
1300
1301static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302{
1303 struct ccw_device *cdev;
1304
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001305 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306 if (!cdev)
1307 return;
Cornelia Huckc820de32008-07-14 09:58:45 +02001308 if (check_for_io_on_path(sch, mask)) {
1309 if (cdev->private->state == DEV_STATE_ONLINE)
1310 ccw_device_kill_io(cdev);
1311 else {
1312 terminate_internal_io(sch, cdev);
1313 /* Re-start path verification. */
1314 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1315 }
1316 } else
1317 /* trigger path verification. */
1318 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1319
1320}
1321
Cornelia Huck99611f82008-07-14 09:59:02 +02001322static int io_subchannel_chp_event(struct subchannel *sch,
1323 struct chp_link *link, int event)
Cornelia Huckc820de32008-07-14 09:58:45 +02001324{
1325 int mask;
Cornelia Huckc820de32008-07-14 09:58:45 +02001326
Cornelia Huck99611f82008-07-14 09:59:02 +02001327 mask = chp_ssd_get_mask(&sch->ssd_info, link);
Cornelia Huckc820de32008-07-14 09:58:45 +02001328 if (!mask)
1329 return 0;
1330 switch (event) {
1331 case CHP_VARY_OFF:
1332 sch->opm &= ~mask;
1333 sch->lpm &= ~mask;
1334 io_subchannel_terminate_path(sch, mask);
1335 break;
1336 case CHP_VARY_ON:
1337 sch->opm |= mask;
1338 sch->lpm |= mask;
1339 io_subchannel_verify(sch);
1340 break;
1341 case CHP_OFFLINE:
1342 if (stsch(sch->schid, &sch->schib))
1343 return -ENXIO;
1344 if (!css_sch_is_valid(&sch->schib))
1345 return -ENODEV;
1346 io_subchannel_terminate_path(sch, mask);
1347 break;
1348 case CHP_ONLINE:
1349 if (stsch(sch->schid, &sch->schib))
1350 return -ENXIO;
1351 sch->lpm |= mask & sch->opm;
1352 io_subchannel_verify(sch);
1353 break;
1354 }
1355 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356}
1357
1358static void
Cornelia Huck8bbace72006-01-11 10:56:22 +01001359io_subchannel_shutdown(struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 struct ccw_device *cdev;
1362 int ret;
1363
Cornelia Huckdb6a6422008-01-26 14:10:46 +01001364 cdev = sch_get_cdev(sch);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001366 if (cio_is_console(sch->schid))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return;
1368 if (!sch->schib.pmcw.ena)
1369 /* Nothing to do. */
1370 return;
1371 ret = cio_disable_subchannel(sch);
1372 if (ret != -EBUSY)
1373 /* Subchannel is disabled, we're done. */
1374 return;
1375 cdev->private->state = DEV_STATE_QUIESCE;
1376 if (cdev->handler)
1377 cdev->handler(cdev, cdev->private->intparm,
1378 ERR_PTR(-EIO));
1379 ret = ccw_device_cancel_halt_clear(cdev);
1380 if (ret == -EBUSY) {
1381 ccw_device_set_timeout(cdev, HZ/10);
1382 wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1383 }
1384 cio_disable_subchannel(sch);
1385}
1386
Cornelia Huckc820de32008-07-14 09:58:45 +02001387static int io_subchannel_get_status(struct subchannel *sch)
1388{
1389 struct schib schib;
1390
1391 if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
1392 return CIO_GONE;
1393 if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
1394 return CIO_REVALIDATE;
1395 if (!sch->lpm)
1396 return CIO_NO_PATH;
1397 return CIO_OPER;
1398}
1399
1400static int device_is_disconnected(struct ccw_device *cdev)
1401{
1402 if (!cdev)
1403 return 0;
1404 return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1405 cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1406}
1407
1408static int recovery_check(struct device *dev, void *data)
1409{
1410 struct ccw_device *cdev = to_ccwdev(dev);
1411 int *redo = data;
1412
1413 spin_lock_irq(cdev->ccwlock);
1414 switch (cdev->private->state) {
1415 case DEV_STATE_DISCONNECTED:
1416 CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1417 cdev->private->dev_id.ssid,
1418 cdev->private->dev_id.devno);
1419 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1420 *redo = 1;
1421 break;
1422 case DEV_STATE_DISCONNECTED_SENSE_ID:
1423 *redo = 1;
1424 break;
1425 }
1426 spin_unlock_irq(cdev->ccwlock);
1427
1428 return 0;
1429}
1430
1431static void recovery_work_func(struct work_struct *unused)
1432{
1433 int redo = 0;
1434
1435 bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1436 if (redo) {
1437 spin_lock_irq(&recovery_lock);
1438 if (!timer_pending(&recovery_timer)) {
1439 if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1440 recovery_phase++;
1441 mod_timer(&recovery_timer, jiffies +
1442 recovery_delay[recovery_phase] * HZ);
1443 }
1444 spin_unlock_irq(&recovery_lock);
1445 } else
1446 CIO_MSG_EVENT(4, "recovery: end\n");
1447}
1448
1449static DECLARE_WORK(recovery_work, recovery_work_func);
1450
1451static void recovery_func(unsigned long data)
1452{
1453 /*
1454 * We can't do our recovery in softirq context and it's not
1455 * performance critical, so we schedule it.
1456 */
1457 schedule_work(&recovery_work);
1458}
1459
1460static void ccw_device_schedule_recovery(void)
1461{
1462 unsigned long flags;
1463
1464 CIO_MSG_EVENT(4, "recovery: schedule\n");
1465 spin_lock_irqsave(&recovery_lock, flags);
1466 if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1467 recovery_phase = 0;
1468 mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1469 }
1470 spin_unlock_irqrestore(&recovery_lock, flags);
1471}
1472
1473static void device_set_disconnected(struct ccw_device *cdev)
1474{
1475 if (!cdev)
1476 return;
1477 ccw_device_set_timeout(cdev, 0);
1478 cdev->private->flags.fake_irb = 0;
1479 cdev->private->state = DEV_STATE_DISCONNECTED;
1480 if (cdev->online)
1481 ccw_device_schedule_recovery();
1482}
1483
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001484void ccw_device_set_notoper(struct ccw_device *cdev)
1485{
1486 struct subchannel *sch = to_subchannel(cdev->dev.parent);
1487
1488 CIO_TRACE_EVENT(2, "notoper");
1489 CIO_TRACE_EVENT(2, sch->dev.bus_id);
1490 ccw_device_set_timeout(cdev, 0);
1491 cio_disable_subchannel(sch);
1492 cdev->private->state = DEV_STATE_NOT_OPER;
1493}
1494
Cornelia Huckc820de32008-07-14 09:58:45 +02001495static int io_subchannel_sch_event(struct subchannel *sch, int slow)
1496{
1497 int event, ret, disc;
1498 unsigned long flags;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001499 enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action;
Cornelia Huckc820de32008-07-14 09:58:45 +02001500 struct ccw_device *cdev;
1501
1502 spin_lock_irqsave(sch->lock, flags);
1503 cdev = sch_get_cdev(sch);
1504 disc = device_is_disconnected(cdev);
1505 if (disc && slow) {
1506 /* Disconnected devices are evaluated directly only.*/
1507 spin_unlock_irqrestore(sch->lock, flags);
1508 return 0;
1509 }
1510 /* No interrupt after machine check - kill pending timers. */
1511 if (cdev)
1512 ccw_device_set_timeout(cdev, 0);
1513 if (!disc && !slow) {
1514 /* Non-disconnected devices are evaluated on the slow path. */
1515 spin_unlock_irqrestore(sch->lock, flags);
1516 return -EAGAIN;
1517 }
1518 event = io_subchannel_get_status(sch);
1519 CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
1520 sch->schid.ssid, sch->schid.sch_no, event,
1521 disc ? "disconnected" : "normal",
1522 slow ? "slow" : "fast");
1523 /* Analyze subchannel status. */
1524 action = NONE;
1525 switch (event) {
1526 case CIO_NO_PATH:
1527 if (disc) {
1528 /* Check if paths have become available. */
1529 action = REPROBE;
1530 break;
1531 }
1532 /* fall through */
1533 case CIO_GONE:
Cornelia Huckc820de32008-07-14 09:58:45 +02001534 /* Ask driver what to do with device. */
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001535 if (io_subchannel_notify(sch, event))
1536 action = DISC;
1537 else
1538 action = UNREGISTER;
Cornelia Huckc820de32008-07-14 09:58:45 +02001539 break;
1540 case CIO_REVALIDATE:
1541 /* Device will be removed, so no notify necessary. */
1542 if (disc)
1543 /* Reprobe because immediate unregister might block. */
1544 action = REPROBE;
1545 else
1546 action = UNREGISTER_PROBE;
1547 break;
1548 case CIO_OPER:
1549 if (disc)
1550 /* Get device operational again. */
1551 action = REPROBE;
1552 break;
1553 }
1554 /* Perform action. */
1555 ret = 0;
1556 switch (action) {
1557 case UNREGISTER:
1558 case UNREGISTER_PROBE:
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001559 ccw_device_set_notoper(cdev);
Cornelia Huckc820de32008-07-14 09:58:45 +02001560 /* Unregister device (will use subchannel lock). */
1561 spin_unlock_irqrestore(sch->lock, flags);
1562 css_sch_device_unregister(sch);
1563 spin_lock_irqsave(sch->lock, flags);
1564
1565 /* Reset intparm to zeroes. */
1566 sch->schib.pmcw.intparm = 0;
1567 cio_modify(sch);
1568 break;
1569 case REPROBE:
1570 ccw_device_trigger_reprobe(cdev);
1571 break;
Peter Oberparleiter91c36912008-08-21 19:46:39 +02001572 case DISC:
1573 device_set_disconnected(cdev);
1574 break;
Cornelia Huckc820de32008-07-14 09:58:45 +02001575 default:
1576 break;
1577 }
1578 spin_unlock_irqrestore(sch->lock, flags);
1579 /* Probe if necessary. */
1580 if (action == UNREGISTER_PROBE)
1581 ret = css_probe_device(sch->schid);
1582
1583 return ret;
1584}
1585
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586#ifdef CONFIG_CCW_CONSOLE
1587static struct ccw_device console_cdev;
1588static struct ccw_device_private console_private;
1589static int console_cdev_in_use;
1590
Cornelia Huck2ec22982006-12-08 15:54:26 +01001591static DEFINE_SPINLOCK(ccw_console_lock);
1592
1593spinlock_t * cio_get_console_lock(void)
1594{
1595 return &ccw_console_lock;
1596}
1597
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001598static int ccw_device_console_enable(struct ccw_device *cdev,
1599 struct subchannel *sch)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600{
1601 int rc;
1602
Cornelia Huckcd6b4f22008-01-26 14:10:43 +01001603 /* Attach subchannel private data. */
1604 sch->private = cio_get_console_priv();
1605 memset(sch->private, 0, sizeof(struct io_subchannel_private));
Cornelia Huck0ae7a7b2008-07-14 09:58:43 +02001606 io_subchannel_init_fields(sch);
1607 sch->driver = &io_subchannel_driver;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 /* Initialize the ccw_device structure. */
Heiko Carstens292888c2006-08-30 14:33:35 +02001609 cdev->dev.parent= &sch->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610 rc = io_subchannel_recog(cdev, sch);
1611 if (rc)
1612 return rc;
1613
1614 /* Now wait for the async. recognition to come to an end. */
1615 spin_lock_irq(cdev->ccwlock);
1616 while (!dev_fsm_final_state(cdev))
1617 wait_cons_dev();
1618 rc = -EIO;
1619 if (cdev->private->state != DEV_STATE_OFFLINE)
1620 goto out_unlock;
1621 ccw_device_online(cdev);
1622 while (!dev_fsm_final_state(cdev))
1623 wait_cons_dev();
1624 if (cdev->private->state != DEV_STATE_ONLINE)
1625 goto out_unlock;
1626 rc = 0;
1627out_unlock:
1628 spin_unlock_irq(cdev->ccwlock);
1629 return 0;
1630}
1631
1632struct ccw_device *
1633ccw_device_probe_console(void)
1634{
1635 struct subchannel *sch;
1636 int ret;
1637
1638 if (xchg(&console_cdev_in_use, 1) != 0)
Peter Oberparleiter600b5d12006-02-01 03:06:35 -08001639 return ERR_PTR(-EBUSY);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640 sch = cio_probe_console();
1641 if (IS_ERR(sch)) {
1642 console_cdev_in_use = 0;
1643 return (void *) sch;
1644 }
1645 memset(&console_cdev, 0, sizeof(struct ccw_device));
1646 memset(&console_private, 0, sizeof(struct ccw_device_private));
1647 console_cdev.private = &console_private;
Martin Schwidefskyc1637532006-12-08 15:53:57 +01001648 console_private.cdev = &console_cdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 ret = ccw_device_console_enable(&console_cdev, sch);
1650 if (ret) {
1651 cio_release_console();
1652 console_cdev_in_use = 0;
1653 return ERR_PTR(ret);
1654 }
1655 console_cdev.online = 1;
1656 return &console_cdev;
1657}
1658#endif
1659
1660/*
1661 * get ccw_device matching the busid, but only if owned by cdrv
1662 */
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001663static int
1664__ccwdev_check_busid(struct device *dev, void *id)
1665{
1666 char *bus_id;
1667
Cornelia Huck12975ae2006-10-11 15:31:47 +02001668 bus_id = id;
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001669
1670 return (strncmp(bus_id, dev->bus_id, BUS_ID_SIZE) == 0);
1671}
1672
1673
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001674/**
1675 * get_ccwdev_by_busid() - obtain device from a bus id
1676 * @cdrv: driver the device is owned by
1677 * @bus_id: bus id of the device to be searched
1678 *
1679 * This function searches all devices owned by @cdrv for a device with a bus
1680 * id matching @bus_id.
1681 * Returns:
1682 * If a match is found, its reference count of the found device is increased
1683 * and it is returned; else %NULL is returned.
1684 */
1685struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1686 const char *bus_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687{
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001688 struct device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001689 struct device_driver *drv;
1690
1691 drv = get_driver(&cdrv->driver);
1692 if (!drv)
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001693 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694
Cornelia Huckb0744bd2005-06-25 14:55:27 -07001695 dev = driver_find_device(drv, NULL, (void *)bus_id,
1696 __ccwdev_check_busid);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697 put_driver(drv);
1698
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001699 return dev ? to_ccwdev(dev) : NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001700}
1701
1702/************************** device driver handling ************************/
1703
1704/* This is the implementation of the ccw_driver class. The probe, remove
1705 * and release methods are initially very similar to the device_driver
1706 * implementations, with the difference that they have ccw_device
1707 * arguments.
1708 *
1709 * A ccw driver also contains the information that is needed for
1710 * device matching.
1711 */
1712static int
1713ccw_device_probe (struct device *dev)
1714{
1715 struct ccw_device *cdev = to_ccwdev(dev);
1716 struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1717 int ret;
1718
1719 cdev->drv = cdrv; /* to let the driver call _set_online */
1720
1721 ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1722
1723 if (ret) {
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001724 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001725 return ret;
1726 }
1727
1728 return 0;
1729}
1730
1731static int
1732ccw_device_remove (struct device *dev)
1733{
1734 struct ccw_device *cdev = to_ccwdev(dev);
1735 struct ccw_driver *cdrv = cdev->drv;
1736 int ret;
1737
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 if (cdrv->remove)
1739 cdrv->remove(cdev);
1740 if (cdev->online) {
1741 cdev->online = 0;
1742 spin_lock_irq(cdev->ccwlock);
1743 ret = ccw_device_offline(cdev);
1744 spin_unlock_irq(cdev->ccwlock);
1745 if (ret == 0)
1746 wait_event(cdev->private->wait_q,
1747 dev_fsm_final_state(cdev));
1748 else
Michael Ernst139b83dd2008-05-07 09:22:54 +02001749 CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
Cornelia Hucke556bbb2007-07-27 12:29:19 +02001750 "device 0.%x.%04x\n",
1751 ret, cdev->private->dev_id.ssid,
1752 cdev->private->dev_id.devno);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
1754 ccw_device_set_timeout(cdev, 0);
Heiko Carstensd2c993d2006-07-12 16:41:55 +02001755 cdev->drv = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 return 0;
1757}
1758
Cornelia Huck958974fb2007-10-12 16:11:21 +02001759static void ccw_device_shutdown(struct device *dev)
1760{
1761 struct ccw_device *cdev;
1762
1763 cdev = to_ccwdev(dev);
1764 if (cdev->drv && cdev->drv->shutdown)
1765 cdev->drv->shutdown(cdev);
Cornelia Huck1842f2b2007-10-12 16:11:22 +02001766 disable_cmf(cdev);
Cornelia Huck958974fb2007-10-12 16:11:21 +02001767}
1768
Cornelia Huck8bbace72006-01-11 10:56:22 +01001769struct bus_type ccw_bus_type = {
1770 .name = "ccw",
1771 .match = ccw_bus_match,
1772 .uevent = ccw_uevent,
1773 .probe = ccw_device_probe,
1774 .remove = ccw_device_remove,
Cornelia Huck958974fb2007-10-12 16:11:21 +02001775 .shutdown = ccw_device_shutdown,
Cornelia Huck8bbace72006-01-11 10:56:22 +01001776};
1777
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001778/**
1779 * ccw_driver_register() - register a ccw driver
1780 * @cdriver: driver to be registered
1781 *
1782 * This function is mainly a wrapper around driver_register().
1783 * Returns:
1784 * %0 on success and a negative error value on failure.
1785 */
1786int ccw_driver_register(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787{
1788 struct device_driver *drv = &cdriver->driver;
1789
1790 drv->bus = &ccw_bus_type;
1791 drv->name = cdriver->name;
Cornelia Huck4beee642008-01-26 14:10:47 +01001792 drv->owner = cdriver->owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
1794 return driver_register(drv);
1795}
1796
Cornelia Huckb2ffd8e2007-10-12 16:11:17 +02001797/**
1798 * ccw_driver_unregister() - deregister a ccw driver
1799 * @cdriver: driver to be deregistered
1800 *
1801 * This function is mainly a wrapper around driver_unregister().
1802 */
1803void ccw_driver_unregister(struct ccw_driver *cdriver)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804{
1805 driver_unregister(&cdriver->driver);
1806}
1807
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001808/* Helper func for qdio. */
1809struct subchannel_id
1810ccw_device_get_subchannel_id(struct ccw_device *cdev)
1811{
1812 struct subchannel *sch;
1813
1814 sch = to_subchannel(cdev->dev.parent);
1815 return sch->schid;
1816}
1817
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818MODULE_LICENSE("GPL");
1819EXPORT_SYMBOL(ccw_device_set_online);
1820EXPORT_SYMBOL(ccw_device_set_offline);
1821EXPORT_SYMBOL(ccw_driver_register);
1822EXPORT_SYMBOL(ccw_driver_unregister);
1823EXPORT_SYMBOL(get_ccwdev_by_busid);
1824EXPORT_SYMBOL(ccw_bus_type);
1825EXPORT_SYMBOL(ccw_device_work);
Cornelia Hucka8237fc2006-01-06 00:19:21 -08001826EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id);