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