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