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