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