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