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