Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * drivers/s390/cio/device.c | |
3 | * bus driver for ccw devices | |
1da177e4 | 4 | * |
c820de39 | 5 | * Copyright IBM Corp. 2002,2008 |
1da177e4 | 6 | * Author(s): Arnd Bergmann (arndb@de.ibm.com) |
4ce3b30c | 7 | * Cornelia Huck (cornelia.huck@de.ibm.com) |
1da177e4 LT |
8 | * Martin Schwidefsky (schwidefsky@de.ibm.com) |
9 | */ | |
1da177e4 LT |
10 | #include <linux/module.h> |
11 | #include <linux/init.h> | |
12 | #include <linux/spinlock.h> | |
13 | #include <linux/errno.h> | |
14 | #include <linux/err.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/list.h> | |
17 | #include <linux/device.h> | |
18 | #include <linux/workqueue.h> | |
90ab1336 | 19 | #include <linux/timer.h> |
1da177e4 LT |
20 | |
21 | #include <asm/ccwdev.h> | |
22 | #include <asm/cio.h> | |
4e57b681 | 23 | #include <asm/param.h> /* HZ */ |
1842f2b1 | 24 | #include <asm/cmb.h> |
3a3fc29a | 25 | #include <asm/isc.h> |
1da177e4 | 26 | |
0ae7a7b2 | 27 | #include "chp.h" |
1da177e4 | 28 | #include "cio.h" |
d7b5a4c9 | 29 | #include "cio_debug.h" |
1da177e4 LT |
30 | #include "css.h" |
31 | #include "device.h" | |
32 | #include "ioasm.h" | |
cd6b4f27 | 33 | #include "io_sch.h" |
ecf5d9ef | 34 | #include "blacklist.h" |
1da177e4 | 35 | |
90ab1336 | 36 | static struct timer_list recovery_timer; |
486d0a00 | 37 | static DEFINE_SPINLOCK(recovery_lock); |
90ab1336 PO |
38 | static int recovery_phase; |
39 | static const unsigned long recovery_delay[] = { 3, 30, 300 }; | |
40 | ||
1da177e4 LT |
41 | /******************* bus type handling ***********************/ |
42 | ||
43 | /* The Linux driver model distinguishes between a bus type and | |
44 | * the bus itself. Of course we only have one channel | |
45 | * subsystem driver and one channel system per machine, but | |
46 | * we still use the abstraction. T.R. says it's a good idea. */ | |
47 | static int | |
48 | ccw_bus_match (struct device * dev, struct device_driver * drv) | |
49 | { | |
50 | struct ccw_device *cdev = to_ccwdev(dev); | |
51 | struct ccw_driver *cdrv = to_ccwdrv(drv); | |
52 | const struct ccw_device_id *ids = cdrv->ids, *found; | |
53 | ||
54 | if (!ids) | |
55 | return 0; | |
56 | ||
57 | found = ccw_device_id_match(ids, &cdev->id); | |
58 | if (!found) | |
59 | return 0; | |
60 | ||
61 | cdev->id.driver_info = found->driver_info; | |
62 | ||
63 | return 1; | |
64 | } | |
65 | ||
db0c2d59 PO |
66 | /* Store modalias string delimited by prefix/suffix string into buffer with |
67 | * specified size. Return length of resulting string (excluding trailing '\0') | |
68 | * even if string doesn't fit buffer (snprintf semantics). */ | |
cfbe9bb2 | 69 | static int snprint_alias(char *buf, size_t size, |
db0c2d59 | 70 | struct ccw_device_id *id, const char *suffix) |
1da177e4 | 71 | { |
db0c2d59 | 72 | int len; |
1da177e4 | 73 | |
cfbe9bb2 | 74 | len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model); |
db0c2d59 PO |
75 | if (len > size) |
76 | return len; | |
77 | buf += len; | |
78 | size -= len; | |
1da177e4 | 79 | |
db0c2d59 PO |
80 | if (id->dev_type != 0) |
81 | len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type, | |
82 | id->dev_model, suffix); | |
83 | else | |
84 | len += snprintf(buf, size, "dtdm%s", suffix); | |
1da177e4 | 85 | |
db0c2d59 PO |
86 | return len; |
87 | } | |
88 | ||
89 | /* Set up environment variables for ccw device uevent. Return 0 on success, | |
90 | * non-zero otherwise. */ | |
7eff2e7a | 91 | static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env) |
db0c2d59 PO |
92 | { |
93 | struct ccw_device *cdev = to_ccwdev(dev); | |
94 | struct ccw_device_id *id = &(cdev->id); | |
cfbe9bb2 CH |
95 | int ret; |
96 | char modalias_buf[30]; | |
1da177e4 | 97 | |
db0c2d59 | 98 | /* CU_TYPE= */ |
7eff2e7a | 99 | ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type); |
cfbe9bb2 CH |
100 | if (ret) |
101 | return ret; | |
db0c2d59 PO |
102 | |
103 | /* CU_MODEL= */ | |
7eff2e7a | 104 | ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model); |
cfbe9bb2 CH |
105 | if (ret) |
106 | return ret; | |
1da177e4 LT |
107 | |
108 | /* The next two can be zero, that's ok for us */ | |
db0c2d59 | 109 | /* DEV_TYPE= */ |
7eff2e7a | 110 | ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type); |
cfbe9bb2 CH |
111 | if (ret) |
112 | return ret; | |
1da177e4 | 113 | |
db0c2d59 | 114 | /* DEV_MODEL= */ |
7eff2e7a | 115 | ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model); |
cfbe9bb2 CH |
116 | if (ret) |
117 | return ret; | |
db0c2d59 PO |
118 | |
119 | /* MODALIAS= */ | |
cfbe9bb2 | 120 | snprint_alias(modalias_buf, sizeof(modalias_buf), id, ""); |
7eff2e7a KS |
121 | ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf); |
122 | return ret; | |
1da177e4 LT |
123 | } |
124 | ||
8bbace7e | 125 | struct bus_type ccw_bus_type; |
1da177e4 | 126 | |
602b20f2 CH |
127 | static void io_subchannel_irq(struct subchannel *); |
128 | static int io_subchannel_probe(struct subchannel *); | |
129 | static int io_subchannel_remove(struct subchannel *); | |
8bbace7e | 130 | static void io_subchannel_shutdown(struct subchannel *); |
c820de39 | 131 | static int io_subchannel_sch_event(struct subchannel *, int); |
99611f87 CH |
132 | static int io_subchannel_chp_event(struct subchannel *, struct chp_link *, |
133 | int); | |
1da177e4 | 134 | |
f08adc00 CH |
135 | static struct css_device_id io_subchannel_ids[] = { |
136 | { .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, }, | |
137 | { /* end of list */ }, | |
138 | }; | |
139 | MODULE_DEVICE_TABLE(css, io_subchannel_ids); | |
140 | ||
f7e5d67c | 141 | static struct css_driver io_subchannel_driver = { |
4beee646 | 142 | .owner = THIS_MODULE, |
f08adc00 | 143 | .subchannel_type = io_subchannel_ids, |
25b7bb58 | 144 | .name = "io_subchannel", |
1da177e4 | 145 | .irq = io_subchannel_irq, |
c820de39 CH |
146 | .sch_event = io_subchannel_sch_event, |
147 | .chp_event = io_subchannel_chp_event, | |
8bbace7e CH |
148 | .probe = io_subchannel_probe, |
149 | .remove = io_subchannel_remove, | |
150 | .shutdown = io_subchannel_shutdown, | |
1da177e4 LT |
151 | }; |
152 | ||
153 | struct workqueue_struct *ccw_device_work; | |
40154b82 PO |
154 | wait_queue_head_t ccw_device_init_wq; |
155 | atomic_t ccw_device_init_count; | |
1da177e4 | 156 | |
90ab1336 PO |
157 | static void recovery_func(unsigned long data); |
158 | ||
1da177e4 LT |
159 | static int __init |
160 | init_ccw_bus_type (void) | |
161 | { | |
162 | int ret; | |
163 | ||
164 | init_waitqueue_head(&ccw_device_init_wq); | |
165 | atomic_set(&ccw_device_init_count, 0); | |
90ab1336 | 166 | setup_timer(&recovery_timer, recovery_func, 0); |
1da177e4 LT |
167 | |
168 | ccw_device_work = create_singlethread_workqueue("cio"); | |
169 | if (!ccw_device_work) | |
170 | return -ENOMEM; /* FIXME: better errno ? */ | |
1da177e4 LT |
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 | ||
25b7bb58 CH |
179 | ret = css_driver_register(&io_subchannel_driver); |
180 | if (ret) | |
1da177e4 LT |
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); | |
1da177e4 LT |
190 | if (slow_path_wq) |
191 | destroy_workqueue(slow_path_wq); | |
192 | return ret; | |
193 | } | |
194 | ||
195 | static void __exit | |
196 | cleanup_ccw_bus_type (void) | |
197 | { | |
25b7bb58 | 198 | css_driver_unregister(&io_subchannel_driver); |
1da177e4 | 199 | bus_unregister(&ccw_bus_type); |
1da177e4 LT |
200 | destroy_workqueue(ccw_device_work); |
201 | } | |
202 | ||
203 | subsys_initcall(init_ccw_bus_type); | |
204 | module_exit(cleanup_ccw_bus_type); | |
205 | ||
206 | /************************ device handling **************************/ | |
207 | ||
208 | /* | |
209 | * A ccw_device has some interfaces in sysfs in addition to the | |
210 | * standard ones. | |
211 | * The following entries are designed to export the information which | |
212 | * resided in 2.4 in /proc/subchannels. Subchannel and device number | |
213 | * are obvious, so they don't have an entry :) | |
214 | * TODO: Split chpids and pimpampom up? Where is "in use" in the tree? | |
215 | */ | |
216 | static ssize_t | |
3fd3c0a5 | 217 | chpids_show (struct device * dev, struct device_attribute *attr, char * buf) |
1da177e4 LT |
218 | { |
219 | struct subchannel *sch = to_subchannel(dev); | |
7ad6a249 | 220 | struct chsc_ssd_info *ssd = &sch->ssd_info; |
1da177e4 LT |
221 | ssize_t ret = 0; |
222 | int chp; | |
7ad6a249 PO |
223 | int mask; |
224 | ||
225 | for (chp = 0; chp < 8; chp++) { | |
226 | mask = 0x80 >> chp; | |
227 | if (ssd->path_mask & mask) | |
228 | ret += sprintf(buf + ret, "%02x ", ssd->chpid[chp].id); | |
229 | else | |
230 | ret += sprintf(buf + ret, "00 "); | |
231 | } | |
1da177e4 LT |
232 | ret += sprintf (buf+ret, "\n"); |
233 | return min((ssize_t)PAGE_SIZE, ret); | |
234 | } | |
235 | ||
236 | static ssize_t | |
3fd3c0a5 | 237 | pimpampom_show (struct device * dev, struct device_attribute *attr, char * buf) |
1da177e4 LT |
238 | { |
239 | struct subchannel *sch = to_subchannel(dev); | |
240 | struct pmcw *pmcw = &sch->schib.pmcw; | |
241 | ||
242 | return sprintf (buf, "%02x %02x %02x\n", | |
243 | pmcw->pim, pmcw->pam, pmcw->pom); | |
244 | } | |
245 | ||
246 | static ssize_t | |
3fd3c0a5 | 247 | devtype_show (struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
248 | { |
249 | struct ccw_device *cdev = to_ccwdev(dev); | |
250 | struct ccw_device_id *id = &(cdev->id); | |
251 | ||
252 | if (id->dev_type != 0) | |
253 | return sprintf(buf, "%04x/%02x\n", | |
254 | id->dev_type, id->dev_model); | |
255 | else | |
256 | return sprintf(buf, "n/a\n"); | |
257 | } | |
258 | ||
259 | static ssize_t | |
3fd3c0a5 | 260 | cutype_show (struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
261 | { |
262 | struct ccw_device *cdev = to_ccwdev(dev); | |
263 | struct ccw_device_id *id = &(cdev->id); | |
264 | ||
265 | return sprintf(buf, "%04x/%02x\n", | |
266 | id->cu_type, id->cu_model); | |
267 | } | |
268 | ||
f1fc78a8 BB |
269 | static ssize_t |
270 | modalias_show (struct device *dev, struct device_attribute *attr, char *buf) | |
271 | { | |
272 | struct ccw_device *cdev = to_ccwdev(dev); | |
273 | struct ccw_device_id *id = &(cdev->id); | |
db0c2d59 | 274 | int len; |
f1fc78a8 | 275 | |
086a6c62 | 276 | len = snprint_alias(buf, PAGE_SIZE, id, "\n"); |
db0c2d59 PO |
277 | |
278 | return len > PAGE_SIZE ? PAGE_SIZE : len; | |
f1fc78a8 BB |
279 | } |
280 | ||
1da177e4 | 281 | static ssize_t |
3fd3c0a5 | 282 | online_show (struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
283 | { |
284 | struct ccw_device *cdev = to_ccwdev(dev); | |
285 | ||
286 | return sprintf(buf, cdev->online ? "1\n" : "0\n"); | |
287 | } | |
288 | ||
d7b5a4c9 CH |
289 | int ccw_device_is_orphan(struct ccw_device *cdev) |
290 | { | |
291 | return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent)); | |
292 | } | |
293 | ||
ef99516c | 294 | static void ccw_device_unregister(struct ccw_device *cdev) |
7674da77 | 295 | { |
7674da77 | 296 | if (test_and_clear_bit(1, &cdev->private->registered)) |
ef99516c | 297 | device_del(&cdev->dev); |
7674da77 CH |
298 | } |
299 | ||
46fbe4e4 | 300 | static void ccw_device_remove_orphan_cb(struct work_struct *work) |
59a8a6e2 | 301 | { |
46fbe4e4 PO |
302 | struct ccw_device_private *priv; |
303 | struct ccw_device *cdev; | |
59a8a6e2 | 304 | |
46fbe4e4 PO |
305 | priv = container_of(work, struct ccw_device_private, kick_work); |
306 | cdev = priv->cdev; | |
59a8a6e2 CH |
307 | ccw_device_unregister(cdev); |
308 | put_device(&cdev->dev); | |
46fbe4e4 PO |
309 | /* Release cdev reference for workqueue processing. */ |
310 | put_device(&cdev->dev); | |
59a8a6e2 CH |
311 | } |
312 | ||
46fbe4e4 | 313 | static void ccw_device_call_sch_unregister(struct work_struct *work); |
59a8a6e2 | 314 | |
1da177e4 LT |
315 | static void |
316 | ccw_device_remove_disconnected(struct ccw_device *cdev) | |
317 | { | |
d7b5a4c9 | 318 | unsigned long flags; |
59a8a6e2 | 319 | |
1da177e4 LT |
320 | /* |
321 | * Forced offline in disconnected state means | |
322 | * 'throw away device'. | |
323 | */ | |
46fbe4e4 PO |
324 | /* Get cdev reference for workqueue processing. */ |
325 | if (!get_device(&cdev->dev)) | |
326 | return; | |
d7b5a4c9 | 327 | if (ccw_device_is_orphan(cdev)) { |
59a8a6e2 CH |
328 | /* |
329 | * Deregister ccw device. | |
330 | * Unfortunately, we cannot do this directly from the | |
331 | * attribute method. | |
332 | */ | |
d7b5a4c9 CH |
333 | spin_lock_irqsave(cdev->ccwlock, flags); |
334 | cdev->private->state = DEV_STATE_NOT_OPER; | |
335 | spin_unlock_irqrestore(cdev->ccwlock, flags); | |
46fbe4e4 PO |
336 | PREPARE_WORK(&cdev->private->kick_work, |
337 | ccw_device_remove_orphan_cb); | |
338 | } else | |
339 | /* Deregister subchannel, which will kill the ccw device. */ | |
340 | PREPARE_WORK(&cdev->private->kick_work, | |
341 | ccw_device_call_sch_unregister); | |
342 | queue_work(slow_path_wq, &cdev->private->kick_work); | |
1da177e4 LT |
343 | } |
344 | ||
b2ffd8e9 CH |
345 | /** |
346 | * ccw_device_set_offline() - disable a ccw device for I/O | |
347 | * @cdev: target ccw device | |
348 | * | |
349 | * This function calls the driver's set_offline() function for @cdev, if | |
350 | * given, and then disables @cdev. | |
351 | * Returns: | |
352 | * %0 on success and a negative error value on failure. | |
353 | * Context: | |
354 | * enabled, ccw device lock not held | |
355 | */ | |
356 | int ccw_device_set_offline(struct ccw_device *cdev) | |
1da177e4 LT |
357 | { |
358 | int ret; | |
359 | ||
360 | if (!cdev) | |
361 | return -ENODEV; | |
362 | if (!cdev->online || !cdev->drv) | |
363 | return -EINVAL; | |
364 | ||
365 | if (cdev->drv->set_offline) { | |
366 | ret = cdev->drv->set_offline(cdev); | |
367 | if (ret != 0) | |
368 | return ret; | |
369 | } | |
370 | cdev->online = 0; | |
371 | spin_lock_irq(cdev->ccwlock); | |
372 | ret = ccw_device_offline(cdev); | |
373 | if (ret == -ENODEV) { | |
374 | if (cdev->private->state != DEV_STATE_NOT_OPER) { | |
375 | cdev->private->state = DEV_STATE_OFFLINE; | |
376 | dev_fsm_event(cdev, DEV_EVENT_NOTOPER); | |
377 | } | |
378 | spin_unlock_irq(cdev->ccwlock); | |
9cd67421 CH |
379 | /* Give up reference from ccw_device_set_online(). */ |
380 | put_device(&cdev->dev); | |
1da177e4 LT |
381 | return ret; |
382 | } | |
383 | spin_unlock_irq(cdev->ccwlock); | |
9cd67421 | 384 | if (ret == 0) { |
1da177e4 | 385 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); |
9cd67421 CH |
386 | /* Give up reference from ccw_device_set_online(). */ |
387 | put_device(&cdev->dev); | |
388 | } else { | |
139b83dd | 389 | CIO_MSG_EVENT(0, "ccw_device_offline returned %d, " |
e556bbbd CH |
390 | "device 0.%x.%04x\n", |
391 | ret, cdev->private->dev_id.ssid, | |
392 | cdev->private->dev_id.devno); | |
1da177e4 LT |
393 | cdev->online = 1; |
394 | } | |
9cd67421 | 395 | return ret; |
1da177e4 LT |
396 | } |
397 | ||
b2ffd8e9 CH |
398 | /** |
399 | * ccw_device_set_online() - enable a ccw device for I/O | |
400 | * @cdev: target ccw device | |
401 | * | |
402 | * This function first enables @cdev and then calls the driver's set_online() | |
403 | * function for @cdev, if given. If set_online() returns an error, @cdev is | |
404 | * disabled again. | |
405 | * Returns: | |
406 | * %0 on success and a negative error value on failure. | |
407 | * Context: | |
408 | * enabled, ccw device lock not held | |
409 | */ | |
410 | int ccw_device_set_online(struct ccw_device *cdev) | |
1da177e4 LT |
411 | { |
412 | int ret; | |
413 | ||
414 | if (!cdev) | |
415 | return -ENODEV; | |
416 | if (cdev->online || !cdev->drv) | |
417 | return -EINVAL; | |
9cd67421 CH |
418 | /* Hold on to an extra reference while device is online. */ |
419 | if (!get_device(&cdev->dev)) | |
420 | return -ENODEV; | |
1da177e4 LT |
421 | |
422 | spin_lock_irq(cdev->ccwlock); | |
423 | ret = ccw_device_online(cdev); | |
424 | spin_unlock_irq(cdev->ccwlock); | |
425 | if (ret == 0) | |
426 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); | |
427 | else { | |
139b83dd | 428 | CIO_MSG_EVENT(0, "ccw_device_online returned %d, " |
e556bbbd CH |
429 | "device 0.%x.%04x\n", |
430 | ret, cdev->private->dev_id.ssid, | |
431 | cdev->private->dev_id.devno); | |
9cd67421 CH |
432 | /* Give up online reference since onlining failed. */ |
433 | put_device(&cdev->dev); | |
1da177e4 LT |
434 | return ret; |
435 | } | |
9cd67421 CH |
436 | if (cdev->private->state != DEV_STATE_ONLINE) { |
437 | /* Give up online reference since onlining failed. */ | |
438 | put_device(&cdev->dev); | |
1da177e4 | 439 | return -ENODEV; |
9cd67421 | 440 | } |
1da177e4 LT |
441 | if (!cdev->drv->set_online || cdev->drv->set_online(cdev) == 0) { |
442 | cdev->online = 1; | |
443 | return 0; | |
444 | } | |
445 | spin_lock_irq(cdev->ccwlock); | |
446 | ret = ccw_device_offline(cdev); | |
447 | spin_unlock_irq(cdev->ccwlock); | |
448 | if (ret == 0) | |
449 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); | |
e556bbbd | 450 | else |
139b83dd | 451 | CIO_MSG_EVENT(0, "ccw_device_offline returned %d, " |
e556bbbd CH |
452 | "device 0.%x.%04x\n", |
453 | ret, cdev->private->dev_id.ssid, | |
454 | cdev->private->dev_id.devno); | |
9cd67421 CH |
455 | /* Give up online reference since onlining failed. */ |
456 | put_device(&cdev->dev); | |
6cadb78b | 457 | return (ret == 0) ? -ENODEV : ret; |
1da177e4 LT |
458 | } |
459 | ||
f5ba6c86 CH |
460 | static void online_store_handle_offline(struct ccw_device *cdev) |
461 | { | |
462 | if (cdev->private->state == DEV_STATE_DISCONNECTED) | |
463 | ccw_device_remove_disconnected(cdev); | |
464 | else if (cdev->drv && cdev->drv->set_offline) | |
465 | ccw_device_set_offline(cdev); | |
466 | } | |
467 | ||
468 | static int online_store_recog_and_online(struct ccw_device *cdev) | |
469 | { | |
470 | int ret; | |
471 | ||
472 | /* Do device recognition, if needed. */ | |
473 | if (cdev->id.cu_type == 0) { | |
474 | ret = ccw_device_recognition(cdev); | |
475 | if (ret) { | |
e556bbbd CH |
476 | CIO_MSG_EVENT(0, "Couldn't start recognition " |
477 | "for device 0.%x.%04x (ret=%d)\n", | |
478 | cdev->private->dev_id.ssid, | |
479 | cdev->private->dev_id.devno, ret); | |
f5ba6c86 CH |
480 | return ret; |
481 | } | |
482 | wait_event(cdev->private->wait_q, | |
483 | cdev->private->flags.recog_done); | |
484 | } | |
485 | if (cdev->drv && cdev->drv->set_online) | |
486 | ccw_device_set_online(cdev); | |
487 | return 0; | |
488 | } | |
c78aa6cb | 489 | static int online_store_handle_online(struct ccw_device *cdev, int force) |
f5ba6c86 CH |
490 | { |
491 | int ret; | |
492 | ||
493 | ret = online_store_recog_and_online(cdev); | |
494 | if (ret) | |
c78aa6cb | 495 | return ret; |
f5ba6c86 CH |
496 | if (force && cdev->private->state == DEV_STATE_BOXED) { |
497 | ret = ccw_device_stlck(cdev); | |
c78aa6cb ME |
498 | if (ret) |
499 | return ret; | |
f5ba6c86 CH |
500 | if (cdev->id.cu_type == 0) |
501 | cdev->private->state = DEV_STATE_NOT_OPER; | |
502 | online_store_recog_and_online(cdev); | |
503 | } | |
c78aa6cb | 504 | return 0; |
f5ba6c86 CH |
505 | } |
506 | ||
507 | static ssize_t online_store (struct device *dev, struct device_attribute *attr, | |
508 | const char *buf, size_t count) | |
1da177e4 LT |
509 | { |
510 | struct ccw_device *cdev = to_ccwdev(dev); | |
2f972202 CH |
511 | int force, ret; |
512 | unsigned long i; | |
1da177e4 | 513 | |
973bd993 | 514 | if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0) |
1da177e4 LT |
515 | return -EAGAIN; |
516 | ||
517 | if (cdev->drv && !try_module_get(cdev->drv->owner)) { | |
518 | atomic_set(&cdev->private->onoff, 0); | |
519 | return -EINVAL; | |
520 | } | |
521 | if (!strncmp(buf, "force\n", count)) { | |
522 | force = 1; | |
523 | i = 1; | |
2f972202 | 524 | ret = 0; |
1da177e4 LT |
525 | } else { |
526 | force = 0; | |
2f972202 | 527 | ret = strict_strtoul(buf, 16, &i); |
1da177e4 | 528 | } |
2f972202 CH |
529 | if (ret) |
530 | goto out; | |
f5ba6c86 CH |
531 | switch (i) { |
532 | case 0: | |
533 | online_store_handle_offline(cdev); | |
2f972202 | 534 | ret = count; |
f5ba6c86 CH |
535 | break; |
536 | case 1: | |
c78aa6cb ME |
537 | ret = online_store_handle_online(cdev, force); |
538 | if (!ret) | |
539 | ret = count; | |
f5ba6c86 CH |
540 | break; |
541 | default: | |
2f972202 | 542 | ret = -EINVAL; |
1da177e4 | 543 | } |
2f972202 | 544 | out: |
1da177e4 LT |
545 | if (cdev->drv) |
546 | module_put(cdev->drv->owner); | |
547 | atomic_set(&cdev->private->onoff, 0); | |
2f972202 | 548 | return ret; |
1da177e4 LT |
549 | } |
550 | ||
551 | static ssize_t | |
3fd3c0a5 | 552 | available_show (struct device *dev, struct device_attribute *attr, char *buf) |
1da177e4 LT |
553 | { |
554 | struct ccw_device *cdev = to_ccwdev(dev); | |
555 | struct subchannel *sch; | |
556 | ||
d7b5a4c9 CH |
557 | if (ccw_device_is_orphan(cdev)) |
558 | return sprintf(buf, "no device\n"); | |
1da177e4 LT |
559 | switch (cdev->private->state) { |
560 | case DEV_STATE_BOXED: | |
561 | return sprintf(buf, "boxed\n"); | |
562 | case DEV_STATE_DISCONNECTED: | |
563 | case DEV_STATE_DISCONNECTED_SENSE_ID: | |
564 | case DEV_STATE_NOT_OPER: | |
565 | sch = to_subchannel(dev->parent); | |
566 | if (!sch->lpm) | |
567 | return sprintf(buf, "no path\n"); | |
568 | else | |
569 | return sprintf(buf, "no device\n"); | |
570 | default: | |
571 | /* All other states considered fine. */ | |
572 | return sprintf(buf, "good\n"); | |
573 | } | |
574 | } | |
575 | ||
576 | static DEVICE_ATTR(chpids, 0444, chpids_show, NULL); | |
577 | static DEVICE_ATTR(pimpampom, 0444, pimpampom_show, NULL); | |
578 | static DEVICE_ATTR(devtype, 0444, devtype_show, NULL); | |
579 | static DEVICE_ATTR(cutype, 0444, cutype_show, NULL); | |
f1fc78a8 | 580 | static DEVICE_ATTR(modalias, 0444, modalias_show, NULL); |
1da177e4 | 581 | static DEVICE_ATTR(online, 0644, online_show, online_store); |
1da177e4 LT |
582 | static DEVICE_ATTR(availability, 0444, available_show, NULL); |
583 | ||
7e9db9ea | 584 | static struct attribute *io_subchannel_attrs[] = { |
1da177e4 LT |
585 | &dev_attr_chpids.attr, |
586 | &dev_attr_pimpampom.attr, | |
587 | NULL, | |
588 | }; | |
589 | ||
7e9db9ea CH |
590 | static struct attribute_group io_subchannel_attr_group = { |
591 | .attrs = io_subchannel_attrs, | |
529192f3 | 592 | }; |
1da177e4 LT |
593 | |
594 | static struct attribute * ccwdev_attrs[] = { | |
595 | &dev_attr_devtype.attr, | |
596 | &dev_attr_cutype.attr, | |
f1fc78a8 | 597 | &dev_attr_modalias.attr, |
1da177e4 LT |
598 | &dev_attr_online.attr, |
599 | &dev_attr_cmb_enable.attr, | |
600 | &dev_attr_availability.attr, | |
601 | NULL, | |
602 | }; | |
603 | ||
604 | static struct attribute_group ccwdev_attr_group = { | |
605 | .attrs = ccwdev_attrs, | |
606 | }; | |
607 | ||
f7e5d67c | 608 | static struct attribute_group *ccwdev_attr_groups[] = { |
ef99516c CH |
609 | &ccwdev_attr_group, |
610 | NULL, | |
611 | }; | |
1da177e4 LT |
612 | |
613 | /* this is a simple abstraction for device_register that sets the | |
614 | * correct bus type and adds the bus specific files */ | |
3c9da7ba | 615 | static int ccw_device_register(struct ccw_device *cdev) |
1da177e4 LT |
616 | { |
617 | struct device *dev = &cdev->dev; | |
618 | int ret; | |
619 | ||
620 | dev->bus = &ccw_bus_type; | |
621 | ||
622 | if ((ret = device_add(dev))) | |
623 | return ret; | |
624 | ||
625 | set_bit(1, &cdev->private->registered); | |
1da177e4 LT |
626 | return ret; |
627 | } | |
628 | ||
b0744bd2 | 629 | struct match_data { |
78964268 | 630 | struct ccw_dev_id dev_id; |
b0744bd2 CH |
631 | struct ccw_device * sibling; |
632 | }; | |
633 | ||
634 | static int | |
635 | match_devno(struct device * dev, void * data) | |
636 | { | |
78964268 | 637 | struct match_data * d = data; |
b0744bd2 CH |
638 | struct ccw_device * cdev; |
639 | ||
640 | cdev = to_ccwdev(dev); | |
641 | if ((cdev->private->state == DEV_STATE_DISCONNECTED) && | |
d7b5a4c9 | 642 | !ccw_device_is_orphan(cdev) && |
78964268 | 643 | ccw_dev_id_is_equal(&cdev->private->dev_id, &d->dev_id) && |
d7b5a4c9 | 644 | (cdev != d->sibling)) |
b0744bd2 | 645 | return 1; |
b0744bd2 CH |
646 | return 0; |
647 | } | |
648 | ||
78964268 CH |
649 | static struct ccw_device * get_disc_ccwdev_by_dev_id(struct ccw_dev_id *dev_id, |
650 | struct ccw_device *sibling) | |
1da177e4 | 651 | { |
1da177e4 | 652 | struct device *dev; |
292888c8 | 653 | struct match_data data; |
1da177e4 | 654 | |
78964268 | 655 | data.dev_id = *dev_id; |
292888c8 | 656 | data.sibling = sibling; |
e5945b4f | 657 | dev = bus_find_device(&ccw_bus_type, NULL, &data, match_devno); |
1da177e4 | 658 | |
b0744bd2 | 659 | return dev ? to_ccwdev(dev) : NULL; |
1da177e4 LT |
660 | } |
661 | ||
d7b5a4c9 CH |
662 | static int match_orphan(struct device *dev, void *data) |
663 | { | |
664 | struct ccw_dev_id *dev_id; | |
665 | struct ccw_device *cdev; | |
666 | ||
667 | dev_id = data; | |
668 | cdev = to_ccwdev(dev); | |
669 | return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id); | |
670 | } | |
671 | ||
672 | static struct ccw_device * | |
673 | get_orphaned_ccwdev_by_dev_id(struct channel_subsystem *css, | |
674 | struct ccw_dev_id *dev_id) | |
675 | { | |
676 | struct device *dev; | |
677 | ||
678 | dev = device_find_child(&css->pseudo_subchannel->dev, dev_id, | |
679 | match_orphan); | |
680 | ||
681 | return dev ? to_ccwdev(dev) : NULL; | |
682 | } | |
683 | ||
1da177e4 | 684 | static void |
c1637532 | 685 | ccw_device_add_changed(struct work_struct *work) |
1da177e4 | 686 | { |
c1637532 | 687 | struct ccw_device_private *priv; |
1da177e4 LT |
688 | struct ccw_device *cdev; |
689 | ||
c1637532 MS |
690 | priv = container_of(work, struct ccw_device_private, kick_work); |
691 | cdev = priv->cdev; | |
1da177e4 LT |
692 | if (device_add(&cdev->dev)) { |
693 | put_device(&cdev->dev); | |
694 | return; | |
695 | } | |
696 | set_bit(1, &cdev->private->registered); | |
1da177e4 LT |
697 | } |
698 | ||
d7b5a4c9 | 699 | void ccw_device_do_unreg_rereg(struct work_struct *work) |
1da177e4 | 700 | { |
c1637532 | 701 | struct ccw_device_private *priv; |
1da177e4 LT |
702 | struct ccw_device *cdev; |
703 | struct subchannel *sch; | |
1da177e4 | 704 | |
c1637532 MS |
705 | priv = container_of(work, struct ccw_device_private, kick_work); |
706 | cdev = priv->cdev; | |
1da177e4 | 707 | sch = to_subchannel(cdev->dev.parent); |
d7b5a4c9 | 708 | |
ef99516c | 709 | ccw_device_unregister(cdev); |
1da177e4 | 710 | PREPARE_WORK(&cdev->private->kick_work, |
c1637532 | 711 | ccw_device_add_changed); |
1da177e4 LT |
712 | queue_work(ccw_device_work, &cdev->private->kick_work); |
713 | } | |
714 | ||
715 | static void | |
716 | ccw_device_release(struct device *dev) | |
717 | { | |
718 | struct ccw_device *cdev; | |
719 | ||
720 | cdev = to_ccwdev(dev); | |
6eff208f CH |
721 | /* Release reference of parent subchannel. */ |
722 | put_device(cdev->dev.parent); | |
1da177e4 LT |
723 | kfree(cdev->private); |
724 | kfree(cdev); | |
725 | } | |
726 | ||
7674da77 CH |
727 | static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch) |
728 | { | |
729 | struct ccw_device *cdev; | |
730 | ||
731 | cdev = kzalloc(sizeof(*cdev), GFP_KERNEL); | |
732 | if (cdev) { | |
733 | cdev->private = kzalloc(sizeof(struct ccw_device_private), | |
734 | GFP_KERNEL | GFP_DMA); | |
735 | if (cdev->private) | |
736 | return cdev; | |
737 | } | |
738 | kfree(cdev); | |
739 | return ERR_PTR(-ENOMEM); | |
740 | } | |
741 | ||
742 | static int io_subchannel_initialize_dev(struct subchannel *sch, | |
743 | struct ccw_device *cdev) | |
744 | { | |
745 | cdev->private->cdev = cdev; | |
746 | atomic_set(&cdev->private->onoff, 0); | |
747 | cdev->dev.parent = &sch->dev; | |
748 | cdev->dev.release = ccw_device_release; | |
33583c36 | 749 | INIT_WORK(&cdev->private->kick_work, NULL); |
ef99516c | 750 | cdev->dev.groups = ccwdev_attr_groups; |
7674da77 CH |
751 | /* Do first half of device_register. */ |
752 | device_initialize(&cdev->dev); | |
753 | if (!get_device(&sch->dev)) { | |
283fdd0b CH |
754 | /* Release reference from device_initialize(). */ |
755 | put_device(&cdev->dev); | |
7674da77 CH |
756 | return -ENODEV; |
757 | } | |
758 | return 0; | |
759 | } | |
760 | ||
761 | static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch) | |
762 | { | |
763 | struct ccw_device *cdev; | |
764 | int ret; | |
765 | ||
766 | cdev = io_subchannel_allocate_dev(sch); | |
767 | if (!IS_ERR(cdev)) { | |
768 | ret = io_subchannel_initialize_dev(sch, cdev); | |
769 | if (ret) { | |
770 | kfree(cdev); | |
771 | cdev = ERR_PTR(ret); | |
772 | } | |
773 | } | |
774 | return cdev; | |
775 | } | |
776 | ||
d7b5a4c9 CH |
777 | static int io_subchannel_recog(struct ccw_device *, struct subchannel *); |
778 | ||
779 | static void sch_attach_device(struct subchannel *sch, | |
780 | struct ccw_device *cdev) | |
781 | { | |
82b7ac05 | 782 | css_update_ssd_info(sch); |
d7b5a4c9 | 783 | spin_lock_irq(sch->lock); |
db6a6423 | 784 | sch_set_cdev(sch, cdev); |
d7b5a4c9 CH |
785 | cdev->private->schid = sch->schid; |
786 | cdev->ccwlock = sch->lock; | |
c820de39 | 787 | ccw_device_trigger_reprobe(cdev); |
d7b5a4c9 CH |
788 | spin_unlock_irq(sch->lock); |
789 | } | |
790 | ||
791 | static void sch_attach_disconnected_device(struct subchannel *sch, | |
792 | struct ccw_device *cdev) | |
793 | { | |
794 | struct subchannel *other_sch; | |
795 | int ret; | |
796 | ||
6eff208f CH |
797 | /* Get reference for new parent. */ |
798 | if (!get_device(&sch->dev)) | |
799 | return; | |
800 | other_sch = to_subchannel(cdev->dev.parent); | |
801 | /* Note: device_move() changes cdev->dev.parent */ | |
d7b5a4c9 CH |
802 | ret = device_move(&cdev->dev, &sch->dev); |
803 | if (ret) { | |
139b83dd | 804 | CIO_MSG_EVENT(0, "Moving disconnected device 0.%x.%04x failed " |
d7b5a4c9 CH |
805 | "(ret=%d)!\n", cdev->private->dev_id.ssid, |
806 | cdev->private->dev_id.devno, ret); | |
6eff208f CH |
807 | /* Put reference for new parent. */ |
808 | put_device(&sch->dev); | |
d7b5a4c9 CH |
809 | return; |
810 | } | |
db6a6423 | 811 | sch_set_cdev(other_sch, NULL); |
d7b5a4c9 CH |
812 | /* No need to keep a subchannel without ccw device around. */ |
813 | css_sch_device_unregister(other_sch); | |
d7b5a4c9 | 814 | sch_attach_device(sch, cdev); |
6eff208f CH |
815 | /* Put reference for old parent. */ |
816 | put_device(&other_sch->dev); | |
d7b5a4c9 CH |
817 | } |
818 | ||
819 | static void sch_attach_orphaned_device(struct subchannel *sch, | |
820 | struct ccw_device *cdev) | |
821 | { | |
822 | int ret; | |
6eff208f | 823 | struct subchannel *pseudo_sch; |
d7b5a4c9 | 824 | |
6eff208f CH |
825 | /* Get reference for new parent. */ |
826 | if (!get_device(&sch->dev)) | |
827 | return; | |
828 | pseudo_sch = to_subchannel(cdev->dev.parent); | |
829 | /* | |
830 | * Try to move the ccw device to its new subchannel. | |
831 | * Note: device_move() changes cdev->dev.parent | |
832 | */ | |
d7b5a4c9 CH |
833 | ret = device_move(&cdev->dev, &sch->dev); |
834 | if (ret) { | |
835 | CIO_MSG_EVENT(0, "Moving device 0.%x.%04x from orphanage " | |
836 | "failed (ret=%d)!\n", | |
837 | cdev->private->dev_id.ssid, | |
838 | cdev->private->dev_id.devno, ret); | |
6eff208f CH |
839 | /* Put reference for new parent. */ |
840 | put_device(&sch->dev); | |
d7b5a4c9 CH |
841 | return; |
842 | } | |
843 | sch_attach_device(sch, cdev); | |
6eff208f CH |
844 | /* Put reference on pseudo subchannel. */ |
845 | put_device(&pseudo_sch->dev); | |
d7b5a4c9 CH |
846 | } |
847 | ||
848 | static void sch_create_and_recog_new_device(struct subchannel *sch) | |
849 | { | |
850 | struct ccw_device *cdev; | |
851 | ||
852 | /* Need to allocate a new ccw device. */ | |
853 | cdev = io_subchannel_create_ccwdev(sch); | |
854 | if (IS_ERR(cdev)) { | |
855 | /* OK, we did everything we could... */ | |
856 | css_sch_device_unregister(sch); | |
857 | return; | |
858 | } | |
859 | spin_lock_irq(sch->lock); | |
db6a6423 | 860 | sch_set_cdev(sch, cdev); |
d7b5a4c9 CH |
861 | spin_unlock_irq(sch->lock); |
862 | /* Start recognition for the new ccw device. */ | |
863 | if (io_subchannel_recog(cdev, sch)) { | |
864 | spin_lock_irq(sch->lock); | |
db6a6423 | 865 | sch_set_cdev(sch, NULL); |
d7b5a4c9 | 866 | spin_unlock_irq(sch->lock); |
d7b5a4c9 | 867 | css_sch_device_unregister(sch); |
6eff208f CH |
868 | /* Put reference from io_subchannel_create_ccwdev(). */ |
869 | put_device(&sch->dev); | |
870 | /* Give up initial reference. */ | |
871 | put_device(&cdev->dev); | |
d7b5a4c9 CH |
872 | } |
873 | } | |
874 | ||
875 | ||
876 | void ccw_device_move_to_orphanage(struct work_struct *work) | |
877 | { | |
878 | struct ccw_device_private *priv; | |
879 | struct ccw_device *cdev; | |
880 | struct ccw_device *replacing_cdev; | |
881 | struct subchannel *sch; | |
882 | int ret; | |
883 | struct channel_subsystem *css; | |
884 | struct ccw_dev_id dev_id; | |
885 | ||
886 | priv = container_of(work, struct ccw_device_private, kick_work); | |
887 | cdev = priv->cdev; | |
888 | sch = to_subchannel(cdev->dev.parent); | |
889 | css = to_css(sch->dev.parent); | |
890 | dev_id.devno = sch->schib.pmcw.dev; | |
891 | dev_id.ssid = sch->schid.ssid; | |
892 | ||
6eff208f CH |
893 | /* Increase refcount for pseudo subchannel. */ |
894 | get_device(&css->pseudo_subchannel->dev); | |
d7b5a4c9 CH |
895 | /* |
896 | * Move the orphaned ccw device to the orphanage so the replacing | |
897 | * ccw device can take its place on the subchannel. | |
6eff208f | 898 | * Note: device_move() changes cdev->dev.parent |
d7b5a4c9 CH |
899 | */ |
900 | ret = device_move(&cdev->dev, &css->pseudo_subchannel->dev); | |
901 | if (ret) { | |
902 | CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to orphanage failed " | |
903 | "(ret=%d)!\n", cdev->private->dev_id.ssid, | |
904 | cdev->private->dev_id.devno, ret); | |
6eff208f CH |
905 | /* Decrease refcount for pseudo subchannel again. */ |
906 | put_device(&css->pseudo_subchannel->dev); | |
d7b5a4c9 CH |
907 | return; |
908 | } | |
909 | cdev->ccwlock = css->pseudo_subchannel->lock; | |
910 | /* | |
911 | * Search for the replacing ccw device | |
912 | * - among the disconnected devices | |
913 | * - in the orphanage | |
914 | */ | |
915 | replacing_cdev = get_disc_ccwdev_by_dev_id(&dev_id, cdev); | |
916 | if (replacing_cdev) { | |
917 | sch_attach_disconnected_device(sch, replacing_cdev); | |
85acc407 | 918 | /* Release reference from get_disc_ccwdev_by_dev_id() */ |
97166f52 | 919 | put_device(&replacing_cdev->dev); |
6eff208f CH |
920 | /* Release reference of subchannel from old cdev. */ |
921 | put_device(&sch->dev); | |
d7b5a4c9 CH |
922 | return; |
923 | } | |
924 | replacing_cdev = get_orphaned_ccwdev_by_dev_id(css, &dev_id); | |
925 | if (replacing_cdev) { | |
926 | sch_attach_orphaned_device(sch, replacing_cdev); | |
85acc407 | 927 | /* Release reference from get_orphaned_ccwdev_by_dev_id() */ |
97166f52 | 928 | put_device(&replacing_cdev->dev); |
6eff208f CH |
929 | /* Release reference of subchannel from old cdev. */ |
930 | put_device(&sch->dev); | |
d7b5a4c9 CH |
931 | return; |
932 | } | |
933 | sch_create_and_recog_new_device(sch); | |
6eff208f CH |
934 | /* Release reference of subchannel from old cdev. */ |
935 | put_device(&sch->dev); | |
d7b5a4c9 CH |
936 | } |
937 | ||
1da177e4 LT |
938 | /* |
939 | * Register recognized device. | |
940 | */ | |
941 | static void | |
c1637532 | 942 | io_subchannel_register(struct work_struct *work) |
1da177e4 | 943 | { |
c1637532 | 944 | struct ccw_device_private *priv; |
1da177e4 LT |
945 | struct ccw_device *cdev; |
946 | struct subchannel *sch; | |
947 | int ret; | |
948 | unsigned long flags; | |
949 | ||
c1637532 MS |
950 | priv = container_of(work, struct ccw_device_private, kick_work); |
951 | cdev = priv->cdev; | |
1da177e4 | 952 | sch = to_subchannel(cdev->dev.parent); |
5fb6b854 CH |
953 | /* |
954 | * Check if subchannel is still registered. It may have become | |
955 | * unregistered if a machine check hit us after finishing | |
956 | * device recognition but before the register work could be | |
957 | * queued. | |
958 | */ | |
959 | if (!device_is_registered(&sch->dev)) | |
960 | goto out_err; | |
82b7ac05 | 961 | css_update_ssd_info(sch); |
47af5518 CH |
962 | /* |
963 | * io_subchannel_register() will also be called after device | |
964 | * recognition has been done for a boxed device (which will already | |
965 | * be registered). We need to reprobe since we may now have sense id | |
966 | * information. | |
967 | */ | |
d6a30761 | 968 | if (device_is_registered(&cdev->dev)) { |
47af5518 CH |
969 | if (!cdev->drv) { |
970 | ret = device_reprobe(&cdev->dev); | |
971 | if (ret) | |
972 | /* We can't do much here. */ | |
139b83dd | 973 | CIO_MSG_EVENT(0, "device_reprobe() returned" |
e556bbbd CH |
974 | " %d for 0.%x.%04x\n", ret, |
975 | cdev->private->dev_id.ssid, | |
976 | cdev->private->dev_id.devno); | |
47af5518 | 977 | } |
1da177e4 LT |
978 | goto out; |
979 | } | |
fa1a8c23 CH |
980 | /* |
981 | * Now we know this subchannel will stay, we can throw | |
982 | * our delayed uevent. | |
983 | */ | |
984 | sch->dev.uevent_suppress = 0; | |
985 | kobject_uevent(&sch->dev.kobj, KOBJ_ADD); | |
1da177e4 LT |
986 | /* make it known to the system */ |
987 | ret = ccw_device_register(cdev); | |
988 | if (ret) { | |
e556bbbd CH |
989 | CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n", |
990 | cdev->private->dev_id.ssid, | |
991 | cdev->private->dev_id.devno, ret); | |
2ec22984 | 992 | spin_lock_irqsave(sch->lock, flags); |
db6a6423 | 993 | sch_set_cdev(sch, NULL); |
2ec22984 | 994 | spin_unlock_irqrestore(sch->lock, flags); |
6eff208f CH |
995 | /* Release initial device reference. */ |
996 | put_device(&cdev->dev); | |
5fb6b854 | 997 | goto out_err; |
1da177e4 | 998 | } |
1da177e4 LT |
999 | out: |
1000 | cdev->private->flags.recog_done = 1; | |
1da177e4 | 1001 | wake_up(&cdev->private->wait_q); |
5fb6b854 CH |
1002 | out_err: |
1003 | /* Release reference for workqueue processing. */ | |
1004 | put_device(&cdev->dev); | |
1da177e4 LT |
1005 | if (atomic_dec_and_test(&ccw_device_init_count)) |
1006 | wake_up(&ccw_device_init_wq); | |
1007 | } | |
1008 | ||
3f4cf6e7 | 1009 | static void ccw_device_call_sch_unregister(struct work_struct *work) |
1da177e4 | 1010 | { |
c1637532 MS |
1011 | struct ccw_device_private *priv; |
1012 | struct ccw_device *cdev; | |
1da177e4 LT |
1013 | struct subchannel *sch; |
1014 | ||
c1637532 MS |
1015 | priv = container_of(work, struct ccw_device_private, kick_work); |
1016 | cdev = priv->cdev; | |
46fbe4e4 PO |
1017 | /* Get subchannel reference for local processing. */ |
1018 | if (!get_device(cdev->dev.parent)) | |
1019 | return; | |
1da177e4 | 1020 | sch = to_subchannel(cdev->dev.parent); |
6ab4879a | 1021 | css_sch_device_unregister(sch); |
1da177e4 | 1022 | /* Reset intparm to zeroes. */ |
13952ec1 SO |
1023 | sch->config.intparm = 0; |
1024 | cio_commit_config(sch); | |
46fbe4e4 | 1025 | /* Release cdev reference for workqueue processing.*/ |
1da177e4 | 1026 | put_device(&cdev->dev); |
46fbe4e4 | 1027 | /* Release subchannel reference for local processing. */ |
1da177e4 LT |
1028 | put_device(&sch->dev); |
1029 | } | |
1030 | ||
1031 | /* | |
1032 | * subchannel recognition done. Called from the state machine. | |
1033 | */ | |
1034 | void | |
1035 | io_subchannel_recog_done(struct ccw_device *cdev) | |
1036 | { | |
1037 | struct subchannel *sch; | |
1038 | ||
1039 | if (css_init_done == 0) { | |
1040 | cdev->private->flags.recog_done = 1; | |
1041 | return; | |
1042 | } | |
1043 | switch (cdev->private->state) { | |
1044 | case DEV_STATE_NOT_OPER: | |
1045 | cdev->private->flags.recog_done = 1; | |
1046 | /* Remove device found not operational. */ | |
1047 | if (!get_device(&cdev->dev)) | |
1048 | break; | |
1049 | sch = to_subchannel(cdev->dev.parent); | |
1050 | PREPARE_WORK(&cdev->private->kick_work, | |
c1637532 | 1051 | ccw_device_call_sch_unregister); |
1da177e4 LT |
1052 | queue_work(slow_path_wq, &cdev->private->kick_work); |
1053 | if (atomic_dec_and_test(&ccw_device_init_count)) | |
1054 | wake_up(&ccw_device_init_wq); | |
1055 | break; | |
1056 | case DEV_STATE_BOXED: | |
1057 | /* Device did not respond in time. */ | |
1058 | case DEV_STATE_OFFLINE: | |
1059 | /* | |
1060 | * We can't register the device in interrupt context so | |
1061 | * we schedule a work item. | |
1062 | */ | |
1063 | if (!get_device(&cdev->dev)) | |
1064 | break; | |
1065 | PREPARE_WORK(&cdev->private->kick_work, | |
c1637532 | 1066 | io_subchannel_register); |
1da177e4 LT |
1067 | queue_work(slow_path_wq, &cdev->private->kick_work); |
1068 | break; | |
1069 | } | |
1070 | } | |
1071 | ||
1072 | static int | |
1073 | io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch) | |
1074 | { | |
1075 | int rc; | |
1076 | struct ccw_device_private *priv; | |
1077 | ||
db6a6423 | 1078 | sch_set_cdev(sch, cdev); |
2ec22984 | 1079 | cdev->ccwlock = sch->lock; |
fb6958a5 | 1080 | |
1da177e4 LT |
1081 | /* Init private data. */ |
1082 | priv = cdev->private; | |
78964268 CH |
1083 | priv->dev_id.devno = sch->schib.pmcw.dev; |
1084 | priv->dev_id.ssid = sch->schid.ssid; | |
1085 | priv->schid = sch->schid; | |
1da177e4 LT |
1086 | priv->state = DEV_STATE_NOT_OPER; |
1087 | INIT_LIST_HEAD(&priv->cmb_list); | |
1088 | init_waitqueue_head(&priv->wait_q); | |
1089 | init_timer(&priv->timer); | |
1090 | ||
1091 | /* Set an initial name for the device. */ | |
1f4e7eda CH |
1092 | if (cio_is_console(sch->schid)) |
1093 | cdev->dev.init_name = cio_get_console_cdev_name(sch); | |
1094 | else | |
1095 | dev_set_name(&cdev->dev, "0.%x.%04x", | |
1096 | sch->schid.ssid, sch->schib.pmcw.dev); | |
1da177e4 LT |
1097 | |
1098 | /* Increase counter of devices currently in recognition. */ | |
1099 | atomic_inc(&ccw_device_init_count); | |
1100 | ||
1101 | /* Start async. device sensing. */ | |
2ec22984 | 1102 | spin_lock_irq(sch->lock); |
1da177e4 | 1103 | rc = ccw_device_recognition(cdev); |
2ec22984 | 1104 | spin_unlock_irq(sch->lock); |
1da177e4 LT |
1105 | if (rc) { |
1106 | if (atomic_dec_and_test(&ccw_device_init_count)) | |
1107 | wake_up(&ccw_device_init_wq); | |
1108 | } | |
1109 | return rc; | |
1110 | } | |
1111 | ||
d7b5a4c9 CH |
1112 | static void ccw_device_move_to_sch(struct work_struct *work) |
1113 | { | |
1114 | struct ccw_device_private *priv; | |
1115 | int rc; | |
1116 | struct subchannel *sch; | |
1117 | struct ccw_device *cdev; | |
1118 | struct subchannel *former_parent; | |
1119 | ||
1120 | priv = container_of(work, struct ccw_device_private, kick_work); | |
1121 | sch = priv->sch; | |
1122 | cdev = priv->cdev; | |
6eff208f CH |
1123 | former_parent = to_subchannel(cdev->dev.parent); |
1124 | /* Get reference for new parent. */ | |
1125 | if (!get_device(&sch->dev)) | |
1126 | return; | |
d7b5a4c9 | 1127 | mutex_lock(&sch->reg_mutex); |
6eff208f CH |
1128 | /* |
1129 | * Try to move the ccw device to its new subchannel. | |
1130 | * Note: device_move() changes cdev->dev.parent | |
1131 | */ | |
d7b5a4c9 CH |
1132 | rc = device_move(&cdev->dev, &sch->dev); |
1133 | mutex_unlock(&sch->reg_mutex); | |
1134 | if (rc) { | |
139b83dd | 1135 | CIO_MSG_EVENT(0, "Moving device 0.%x.%04x to subchannel " |
d7b5a4c9 CH |
1136 | "0.%x.%04x failed (ret=%d)!\n", |
1137 | cdev->private->dev_id.ssid, | |
1138 | cdev->private->dev_id.devno, sch->schid.ssid, | |
1139 | sch->schid.sch_no, rc); | |
1140 | css_sch_device_unregister(sch); | |
6eff208f CH |
1141 | /* Put reference for new parent again. */ |
1142 | put_device(&sch->dev); | |
d7b5a4c9 CH |
1143 | goto out; |
1144 | } | |
6eff208f | 1145 | if (!sch_is_pseudo_sch(former_parent)) { |
d7b5a4c9 | 1146 | spin_lock_irq(former_parent->lock); |
db6a6423 | 1147 | sch_set_cdev(former_parent, NULL); |
d7b5a4c9 CH |
1148 | spin_unlock_irq(former_parent->lock); |
1149 | css_sch_device_unregister(former_parent); | |
1150 | /* Reset intparm to zeroes. */ | |
13952ec1 SO |
1151 | former_parent->config.intparm = 0; |
1152 | cio_commit_config(former_parent); | |
d7b5a4c9 CH |
1153 | } |
1154 | sch_attach_device(sch, cdev); | |
1155 | out: | |
6eff208f CH |
1156 | /* Put reference for old parent. */ |
1157 | put_device(&former_parent->dev); | |
d7b5a4c9 CH |
1158 | put_device(&cdev->dev); |
1159 | } | |
1160 | ||
602b20f2 CH |
1161 | static void io_subchannel_irq(struct subchannel *sch) |
1162 | { | |
1163 | struct ccw_device *cdev; | |
1164 | ||
db6a6423 | 1165 | cdev = sch_get_cdev(sch); |
602b20f2 CH |
1166 | |
1167 | CIO_TRACE_EVENT(3, "IRQ"); | |
2a0217d5 | 1168 | CIO_TRACE_EVENT(3, dev_name(&sch->dev)); |
602b20f2 CH |
1169 | if (cdev) |
1170 | dev_fsm_event(cdev, DEV_EVENT_INTERRUPT); | |
1171 | } | |
1172 | ||
13952ec1 SO |
1173 | void io_subchannel_init_config(struct subchannel *sch) |
1174 | { | |
1175 | memset(&sch->config, 0, sizeof(sch->config)); | |
1176 | sch->config.csense = 1; | |
d36f0c66 SO |
1177 | /* Use subchannel mp mode when there is more than 1 installed CHPID. */ |
1178 | if ((sch->schib.pmcw.pim & (sch->schib.pmcw.pim - 1)) != 0) | |
13952ec1 SO |
1179 | sch->config.mp = 1; |
1180 | } | |
1181 | ||
0ae7a7b2 CH |
1182 | static void io_subchannel_init_fields(struct subchannel *sch) |
1183 | { | |
1184 | if (cio_is_console(sch->schid)) | |
1185 | sch->opm = 0xff; | |
1186 | else | |
1187 | sch->opm = chp_get_sch_opm(sch); | |
1188 | sch->lpm = sch->schib.pmcw.pam & sch->opm; | |
3a3fc29a | 1189 | sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC; |
0ae7a7b2 CH |
1190 | |
1191 | CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X" | |
1192 | " - PIM = %02X, PAM = %02X, POM = %02X\n", | |
1193 | sch->schib.pmcw.dev, sch->schid.ssid, | |
1194 | sch->schid.sch_no, sch->schib.pmcw.pim, | |
1195 | sch->schib.pmcw.pam, sch->schib.pmcw.pom); | |
13952ec1 SO |
1196 | |
1197 | io_subchannel_init_config(sch); | |
0ae7a7b2 CH |
1198 | } |
1199 | ||
90ed2b69 CH |
1200 | static void io_subchannel_do_unreg(struct work_struct *work) |
1201 | { | |
1202 | struct subchannel *sch; | |
1203 | ||
1204 | sch = container_of(work, struct subchannel, work); | |
1205 | css_sch_device_unregister(sch); | |
1206 | /* Reset intparm to zeroes. */ | |
13952ec1 SO |
1207 | sch->config.intparm = 0; |
1208 | cio_commit_config(sch); | |
90ed2b69 CH |
1209 | put_device(&sch->dev); |
1210 | } | |
1211 | ||
1212 | /* Schedule unregister if we have no cdev. */ | |
1213 | static void io_subchannel_schedule_removal(struct subchannel *sch) | |
1214 | { | |
1215 | get_device(&sch->dev); | |
1216 | INIT_WORK(&sch->work, io_subchannel_do_unreg); | |
1217 | queue_work(slow_path_wq, &sch->work); | |
1218 | } | |
1219 | ||
1220 | /* | |
1221 | * Note: We always return 0 so that we bind to the device even on error. | |
1222 | * This is needed so that our remove function is called on unregister. | |
1223 | */ | |
0ae7a7b2 | 1224 | static int io_subchannel_probe(struct subchannel *sch) |
1da177e4 | 1225 | { |
1da177e4 LT |
1226 | struct ccw_device *cdev; |
1227 | int rc; | |
1228 | unsigned long flags; | |
d7b5a4c9 | 1229 | struct ccw_dev_id dev_id; |
1da177e4 | 1230 | |
db6a6423 CH |
1231 | cdev = sch_get_cdev(sch); |
1232 | if (cdev) { | |
7e9db9ea CH |
1233 | rc = sysfs_create_group(&sch->dev.kobj, |
1234 | &io_subchannel_attr_group); | |
1235 | if (rc) | |
1236 | CIO_MSG_EVENT(0, "Failed to create io subchannel " | |
1237 | "attributes for subchannel " | |
1238 | "0.%x.%04x (rc=%d)\n", | |
1239 | sch->schid.ssid, sch->schid.sch_no, rc); | |
1da177e4 LT |
1240 | /* |
1241 | * This subchannel already has an associated ccw_device. | |
7e9db9ea CH |
1242 | * Throw the delayed uevent for the subchannel, register |
1243 | * the ccw_device and exit. This happens for all early | |
1244 | * devices, e.g. the console. | |
1da177e4 | 1245 | */ |
7e9db9ea CH |
1246 | sch->dev.uevent_suppress = 0; |
1247 | kobject_uevent(&sch->dev.kobj, KOBJ_ADD); | |
e1031786 | 1248 | cdev->dev.groups = ccwdev_attr_groups; |
1da177e4 LT |
1249 | device_initialize(&cdev->dev); |
1250 | ccw_device_register(cdev); | |
1da177e4 LT |
1251 | /* |
1252 | * Check if the device is already online. If it is | |
9cd67421 CH |
1253 | * the reference count needs to be corrected since we |
1254 | * didn't obtain a reference in ccw_device_set_online. | |
1da177e4 LT |
1255 | */ |
1256 | if (cdev->private->state != DEV_STATE_NOT_OPER && | |
1257 | cdev->private->state != DEV_STATE_OFFLINE && | |
1258 | cdev->private->state != DEV_STATE_BOXED) | |
1259 | get_device(&cdev->dev); | |
1260 | return 0; | |
1261 | } | |
0ae7a7b2 | 1262 | io_subchannel_init_fields(sch); |
f444cc0e SO |
1263 | rc = cio_commit_config(sch); |
1264 | if (rc) | |
1265 | goto out_schedule; | |
7e9db9ea CH |
1266 | rc = sysfs_create_group(&sch->dev.kobj, |
1267 | &io_subchannel_attr_group); | |
1268 | if (rc) | |
90ed2b69 | 1269 | goto out_schedule; |
cd6b4f27 CH |
1270 | /* Allocate I/O subchannel private data. */ |
1271 | sch->private = kzalloc(sizeof(struct io_subchannel_private), | |
1272 | GFP_KERNEL | GFP_DMA); | |
90ed2b69 | 1273 | if (!sch->private) |
7e9db9ea | 1274 | goto out_err; |
111e95a4 SO |
1275 | /* |
1276 | * First check if a fitting device may be found amongst the | |
1277 | * disconnected devices or in the orphanage. | |
1278 | */ | |
1279 | dev_id.devno = sch->schib.pmcw.dev; | |
1280 | dev_id.ssid = sch->schid.ssid; | |
d7b5a4c9 CH |
1281 | cdev = get_disc_ccwdev_by_dev_id(&dev_id, NULL); |
1282 | if (!cdev) | |
1283 | cdev = get_orphaned_ccwdev_by_dev_id(to_css(sch->dev.parent), | |
1284 | &dev_id); | |
1285 | if (cdev) { | |
1286 | /* | |
1287 | * Schedule moving the device until when we have a registered | |
1288 | * subchannel to move to and succeed the probe. We can | |
1289 | * unregister later again, when the probe is through. | |
1290 | */ | |
1291 | cdev->private->sch = sch; | |
1292 | PREPARE_WORK(&cdev->private->kick_work, | |
1293 | ccw_device_move_to_sch); | |
1294 | queue_work(slow_path_wq, &cdev->private->kick_work); | |
1295 | return 0; | |
1296 | } | |
7674da77 | 1297 | cdev = io_subchannel_create_ccwdev(sch); |
90ed2b69 | 1298 | if (IS_ERR(cdev)) |
7e9db9ea | 1299 | goto out_err; |
8bbace7e | 1300 | rc = io_subchannel_recog(cdev, sch); |
1da177e4 | 1301 | if (rc) { |
2ec22984 | 1302 | spin_lock_irqsave(sch->lock, flags); |
90ed2b69 | 1303 | io_subchannel_recog_done(cdev); |
2ec22984 | 1304 | spin_unlock_irqrestore(sch->lock, flags); |
1da177e4 | 1305 | } |
7e9db9ea CH |
1306 | return 0; |
1307 | out_err: | |
1308 | kfree(sch->private); | |
1309 | sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group); | |
90ed2b69 CH |
1310 | out_schedule: |
1311 | io_subchannel_schedule_removal(sch); | |
1312 | return 0; | |
1da177e4 LT |
1313 | } |
1314 | ||
1da177e4 | 1315 | static int |
8bbace7e | 1316 | io_subchannel_remove (struct subchannel *sch) |
1da177e4 LT |
1317 | { |
1318 | struct ccw_device *cdev; | |
1319 | unsigned long flags; | |
1320 | ||
db6a6423 CH |
1321 | cdev = sch_get_cdev(sch); |
1322 | if (!cdev) | |
1da177e4 | 1323 | return 0; |
1da177e4 LT |
1324 | /* Set ccw device to not operational and drop reference. */ |
1325 | spin_lock_irqsave(cdev->ccwlock, flags); | |
db6a6423 | 1326 | sch_set_cdev(sch, NULL); |
1da177e4 LT |
1327 | cdev->private->state = DEV_STATE_NOT_OPER; |
1328 | spin_unlock_irqrestore(cdev->ccwlock, flags); | |
ef99516c CH |
1329 | ccw_device_unregister(cdev); |
1330 | put_device(&cdev->dev); | |
cd6b4f27 | 1331 | kfree(sch->private); |
7e9db9ea | 1332 | sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group); |
1da177e4 LT |
1333 | return 0; |
1334 | } | |
1335 | ||
602b20f2 | 1336 | static int io_subchannel_notify(struct subchannel *sch, int event) |
1da177e4 LT |
1337 | { |
1338 | struct ccw_device *cdev; | |
1339 | ||
db6a6423 | 1340 | cdev = sch_get_cdev(sch); |
1da177e4 LT |
1341 | if (!cdev) |
1342 | return 0; | |
c820de39 | 1343 | return ccw_device_notify(cdev, event); |
1da177e4 LT |
1344 | } |
1345 | ||
602b20f2 | 1346 | static void io_subchannel_verify(struct subchannel *sch) |
1da177e4 LT |
1347 | { |
1348 | struct ccw_device *cdev; | |
1349 | ||
db6a6423 | 1350 | cdev = sch_get_cdev(sch); |
1da177e4 LT |
1351 | if (cdev) |
1352 | dev_fsm_event(cdev, DEV_EVENT_VERIFY); | |
1353 | } | |
1354 | ||
c820de39 | 1355 | static int check_for_io_on_path(struct subchannel *sch, int mask) |
1da177e4 | 1356 | { |
cdb912a4 | 1357 | if (cio_update_schib(sch)) |
c820de39 | 1358 | return 0; |
23d805b6 | 1359 | if (scsw_actl(&sch->schib.scsw) && sch->schib.pmcw.lpum == mask) |
c820de39 CH |
1360 | return 1; |
1361 | return 0; | |
1362 | } | |
1363 | ||
1364 | static void terminate_internal_io(struct subchannel *sch, | |
1365 | struct ccw_device *cdev) | |
1366 | { | |
1367 | if (cio_clear(sch)) { | |
1368 | /* Recheck device in case clear failed. */ | |
1369 | sch->lpm = 0; | |
1370 | if (cdev->online) | |
1371 | dev_fsm_event(cdev, DEV_EVENT_VERIFY); | |
1372 | else | |
1373 | css_schedule_eval(sch->schid); | |
d23861ff | 1374 | return; |
c820de39 | 1375 | } |
1da177e4 | 1376 | cdev->private->state = DEV_STATE_CLEAR_VERIFY; |
c820de39 CH |
1377 | /* Request retry of internal operation. */ |
1378 | cdev->private->flags.intretry = 1; | |
1379 | /* Call handler. */ | |
1da177e4 LT |
1380 | if (cdev->handler) |
1381 | cdev->handler(cdev, cdev->private->intparm, | |
1382 | ERR_PTR(-EIO)); | |
1383 | } | |
1384 | ||
c820de39 CH |
1385 | static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask) |
1386 | { | |
1387 | struct ccw_device *cdev; | |
1388 | ||
1389 | cdev = sch_get_cdev(sch); | |
1390 | if (!cdev) | |
1391 | return; | |
1392 | if (check_for_io_on_path(sch, mask)) { | |
1393 | if (cdev->private->state == DEV_STATE_ONLINE) | |
1394 | ccw_device_kill_io(cdev); | |
1395 | else { | |
1396 | terminate_internal_io(sch, cdev); | |
1397 | /* Re-start path verification. */ | |
1398 | dev_fsm_event(cdev, DEV_EVENT_VERIFY); | |
1399 | } | |
1400 | } else | |
1401 | /* trigger path verification. */ | |
1402 | dev_fsm_event(cdev, DEV_EVENT_VERIFY); | |
1403 | ||
1404 | } | |
1405 | ||
99611f87 CH |
1406 | static int io_subchannel_chp_event(struct subchannel *sch, |
1407 | struct chp_link *link, int event) | |
c820de39 CH |
1408 | { |
1409 | int mask; | |
c820de39 | 1410 | |
99611f87 | 1411 | mask = chp_ssd_get_mask(&sch->ssd_info, link); |
c820de39 CH |
1412 | if (!mask) |
1413 | return 0; | |
1414 | switch (event) { | |
1415 | case CHP_VARY_OFF: | |
1416 | sch->opm &= ~mask; | |
1417 | sch->lpm &= ~mask; | |
1418 | io_subchannel_terminate_path(sch, mask); | |
1419 | break; | |
1420 | case CHP_VARY_ON: | |
1421 | sch->opm |= mask; | |
1422 | sch->lpm |= mask; | |
1423 | io_subchannel_verify(sch); | |
1424 | break; | |
1425 | case CHP_OFFLINE: | |
cdb912a4 | 1426 | if (cio_update_schib(sch)) |
c820de39 CH |
1427 | return -ENODEV; |
1428 | io_subchannel_terminate_path(sch, mask); | |
1429 | break; | |
1430 | case CHP_ONLINE: | |
cdb912a4 SO |
1431 | if (cio_update_schib(sch)) |
1432 | return -ENODEV; | |
c820de39 CH |
1433 | sch->lpm |= mask & sch->opm; |
1434 | io_subchannel_verify(sch); | |
1435 | break; | |
1436 | } | |
1437 | return 0; | |
1438 | } | |
1439 | ||
1da177e4 | 1440 | static void |
8bbace7e | 1441 | io_subchannel_shutdown(struct subchannel *sch) |
1da177e4 | 1442 | { |
1da177e4 LT |
1443 | struct ccw_device *cdev; |
1444 | int ret; | |
1445 | ||
db6a6423 | 1446 | cdev = sch_get_cdev(sch); |
1da177e4 | 1447 | |
a8237fc4 | 1448 | if (cio_is_console(sch->schid)) |
1da177e4 LT |
1449 | return; |
1450 | if (!sch->schib.pmcw.ena) | |
1451 | /* Nothing to do. */ | |
1452 | return; | |
1453 | ret = cio_disable_subchannel(sch); | |
1454 | if (ret != -EBUSY) | |
1455 | /* Subchannel is disabled, we're done. */ | |
1456 | return; | |
1457 | cdev->private->state = DEV_STATE_QUIESCE; | |
1458 | if (cdev->handler) | |
1459 | cdev->handler(cdev, cdev->private->intparm, | |
1460 | ERR_PTR(-EIO)); | |
1461 | ret = ccw_device_cancel_halt_clear(cdev); | |
1462 | if (ret == -EBUSY) { | |
1463 | ccw_device_set_timeout(cdev, HZ/10); | |
1464 | wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev)); | |
1465 | } | |
1466 | cio_disable_subchannel(sch); | |
1467 | } | |
1468 | ||
c820de39 CH |
1469 | static int io_subchannel_get_status(struct subchannel *sch) |
1470 | { | |
1471 | struct schib schib; | |
1472 | ||
1473 | if (stsch(sch->schid, &schib) || !schib.pmcw.dnv) | |
1474 | return CIO_GONE; | |
1475 | if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev)) | |
1476 | return CIO_REVALIDATE; | |
1477 | if (!sch->lpm) | |
1478 | return CIO_NO_PATH; | |
1479 | return CIO_OPER; | |
1480 | } | |
1481 | ||
1482 | static int device_is_disconnected(struct ccw_device *cdev) | |
1483 | { | |
1484 | if (!cdev) | |
1485 | return 0; | |
1486 | return (cdev->private->state == DEV_STATE_DISCONNECTED || | |
1487 | cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID); | |
1488 | } | |
1489 | ||
1490 | static int recovery_check(struct device *dev, void *data) | |
1491 | { | |
1492 | struct ccw_device *cdev = to_ccwdev(dev); | |
1493 | int *redo = data; | |
1494 | ||
1495 | spin_lock_irq(cdev->ccwlock); | |
1496 | switch (cdev->private->state) { | |
1497 | case DEV_STATE_DISCONNECTED: | |
1498 | CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n", | |
1499 | cdev->private->dev_id.ssid, | |
1500 | cdev->private->dev_id.devno); | |
1501 | dev_fsm_event(cdev, DEV_EVENT_VERIFY); | |
1502 | *redo = 1; | |
1503 | break; | |
1504 | case DEV_STATE_DISCONNECTED_SENSE_ID: | |
1505 | *redo = 1; | |
1506 | break; | |
1507 | } | |
1508 | spin_unlock_irq(cdev->ccwlock); | |
1509 | ||
1510 | return 0; | |
1511 | } | |
1512 | ||
1513 | static void recovery_work_func(struct work_struct *unused) | |
1514 | { | |
1515 | int redo = 0; | |
1516 | ||
1517 | bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check); | |
1518 | if (redo) { | |
1519 | spin_lock_irq(&recovery_lock); | |
1520 | if (!timer_pending(&recovery_timer)) { | |
1521 | if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1) | |
1522 | recovery_phase++; | |
1523 | mod_timer(&recovery_timer, jiffies + | |
1524 | recovery_delay[recovery_phase] * HZ); | |
1525 | } | |
1526 | spin_unlock_irq(&recovery_lock); | |
1527 | } else | |
1528 | CIO_MSG_EVENT(4, "recovery: end\n"); | |
1529 | } | |
1530 | ||
1531 | static DECLARE_WORK(recovery_work, recovery_work_func); | |
1532 | ||
1533 | static void recovery_func(unsigned long data) | |
1534 | { | |
1535 | /* | |
1536 | * We can't do our recovery in softirq context and it's not | |
1537 | * performance critical, so we schedule it. | |
1538 | */ | |
1539 | schedule_work(&recovery_work); | |
1540 | } | |
1541 | ||
1542 | static void ccw_device_schedule_recovery(void) | |
1543 | { | |
1544 | unsigned long flags; | |
1545 | ||
1546 | CIO_MSG_EVENT(4, "recovery: schedule\n"); | |
1547 | spin_lock_irqsave(&recovery_lock, flags); | |
1548 | if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) { | |
1549 | recovery_phase = 0; | |
1550 | mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ); | |
1551 | } | |
1552 | spin_unlock_irqrestore(&recovery_lock, flags); | |
1553 | } | |
1554 | ||
ecf5d9ef PO |
1555 | static int purge_fn(struct device *dev, void *data) |
1556 | { | |
1557 | struct ccw_device *cdev = to_ccwdev(dev); | |
1558 | struct ccw_device_private *priv = cdev->private; | |
1559 | int unreg; | |
1560 | ||
1561 | spin_lock_irq(cdev->ccwlock); | |
1562 | unreg = is_blacklisted(priv->dev_id.ssid, priv->dev_id.devno) && | |
1563 | (priv->state == DEV_STATE_OFFLINE); | |
1564 | spin_unlock_irq(cdev->ccwlock); | |
1565 | if (!unreg) | |
1566 | goto out; | |
1567 | if (!get_device(&cdev->dev)) | |
1568 | goto out; | |
1569 | CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", priv->dev_id.ssid, | |
1570 | priv->dev_id.devno); | |
1571 | PREPARE_WORK(&cdev->private->kick_work, ccw_device_call_sch_unregister); | |
1572 | queue_work(slow_path_wq, &cdev->private->kick_work); | |
1573 | ||
1574 | out: | |
1575 | /* Abort loop in case of pending signal. */ | |
1576 | if (signal_pending(current)) | |
1577 | return -EINTR; | |
1578 | ||
1579 | return 0; | |
1580 | } | |
1581 | ||
1582 | /** | |
1583 | * ccw_purge_blacklisted - purge unused, blacklisted devices | |
1584 | * | |
1585 | * Unregister all ccw devices that are offline and on the blacklist. | |
1586 | */ | |
1587 | int ccw_purge_blacklisted(void) | |
1588 | { | |
1589 | CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n"); | |
1590 | bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn); | |
1591 | return 0; | |
1592 | } | |
1593 | ||
c820de39 CH |
1594 | static void device_set_disconnected(struct ccw_device *cdev) |
1595 | { | |
1596 | if (!cdev) | |
1597 | return; | |
1598 | ccw_device_set_timeout(cdev, 0); | |
1599 | cdev->private->flags.fake_irb = 0; | |
1600 | cdev->private->state = DEV_STATE_DISCONNECTED; | |
1601 | if (cdev->online) | |
1602 | ccw_device_schedule_recovery(); | |
1603 | } | |
1604 | ||
91c36919 PO |
1605 | void ccw_device_set_notoper(struct ccw_device *cdev) |
1606 | { | |
1607 | struct subchannel *sch = to_subchannel(cdev->dev.parent); | |
1608 | ||
1609 | CIO_TRACE_EVENT(2, "notoper"); | |
b9d3aed7 | 1610 | CIO_TRACE_EVENT(2, dev_name(&sch->dev)); |
91c36919 PO |
1611 | ccw_device_set_timeout(cdev, 0); |
1612 | cio_disable_subchannel(sch); | |
1613 | cdev->private->state = DEV_STATE_NOT_OPER; | |
1614 | } | |
1615 | ||
c820de39 CH |
1616 | static int io_subchannel_sch_event(struct subchannel *sch, int slow) |
1617 | { | |
1618 | int event, ret, disc; | |
1619 | unsigned long flags; | |
91c36919 | 1620 | enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE, DISC } action; |
c820de39 CH |
1621 | struct ccw_device *cdev; |
1622 | ||
1623 | spin_lock_irqsave(sch->lock, flags); | |
1624 | cdev = sch_get_cdev(sch); | |
1625 | disc = device_is_disconnected(cdev); | |
1626 | if (disc && slow) { | |
1627 | /* Disconnected devices are evaluated directly only.*/ | |
1628 | spin_unlock_irqrestore(sch->lock, flags); | |
1629 | return 0; | |
1630 | } | |
1631 | /* No interrupt after machine check - kill pending timers. */ | |
1632 | if (cdev) | |
1633 | ccw_device_set_timeout(cdev, 0); | |
1634 | if (!disc && !slow) { | |
1635 | /* Non-disconnected devices are evaluated on the slow path. */ | |
1636 | spin_unlock_irqrestore(sch->lock, flags); | |
1637 | return -EAGAIN; | |
1638 | } | |
1639 | event = io_subchannel_get_status(sch); | |
1640 | CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n", | |
1641 | sch->schid.ssid, sch->schid.sch_no, event, | |
1642 | disc ? "disconnected" : "normal", | |
1643 | slow ? "slow" : "fast"); | |
1644 | /* Analyze subchannel status. */ | |
1645 | action = NONE; | |
1646 | switch (event) { | |
1647 | case CIO_NO_PATH: | |
1648 | if (disc) { | |
1649 | /* Check if paths have become available. */ | |
1650 | action = REPROBE; | |
1651 | break; | |
1652 | } | |
1653 | /* fall through */ | |
1654 | case CIO_GONE: | |
c820de39 | 1655 | /* Ask driver what to do with device. */ |
91c36919 PO |
1656 | if (io_subchannel_notify(sch, event)) |
1657 | action = DISC; | |
1658 | else | |
1659 | action = UNREGISTER; | |
c820de39 CH |
1660 | break; |
1661 | case CIO_REVALIDATE: | |
1662 | /* Device will be removed, so no notify necessary. */ | |
1663 | if (disc) | |
1664 | /* Reprobe because immediate unregister might block. */ | |
1665 | action = REPROBE; | |
1666 | else | |
1667 | action = UNREGISTER_PROBE; | |
1668 | break; | |
1669 | case CIO_OPER: | |
1670 | if (disc) | |
1671 | /* Get device operational again. */ | |
1672 | action = REPROBE; | |
1673 | break; | |
1674 | } | |
1675 | /* Perform action. */ | |
1676 | ret = 0; | |
1677 | switch (action) { | |
1678 | case UNREGISTER: | |
1679 | case UNREGISTER_PROBE: | |
91c36919 | 1680 | ccw_device_set_notoper(cdev); |
c820de39 CH |
1681 | /* Unregister device (will use subchannel lock). */ |
1682 | spin_unlock_irqrestore(sch->lock, flags); | |
1683 | css_sch_device_unregister(sch); | |
1684 | spin_lock_irqsave(sch->lock, flags); | |
1685 | ||
1686 | /* Reset intparm to zeroes. */ | |
13952ec1 SO |
1687 | sch->config.intparm = 0; |
1688 | cio_commit_config(sch); | |
c820de39 CH |
1689 | break; |
1690 | case REPROBE: | |
1691 | ccw_device_trigger_reprobe(cdev); | |
1692 | break; | |
91c36919 PO |
1693 | case DISC: |
1694 | device_set_disconnected(cdev); | |
1695 | break; | |
c820de39 CH |
1696 | default: |
1697 | break; | |
1698 | } | |
1699 | spin_unlock_irqrestore(sch->lock, flags); | |
1700 | /* Probe if necessary. */ | |
1701 | if (action == UNREGISTER_PROBE) | |
1702 | ret = css_probe_device(sch->schid); | |
1703 | ||
1704 | return ret; | |
1705 | } | |
1706 | ||
1da177e4 LT |
1707 | #ifdef CONFIG_CCW_CONSOLE |
1708 | static struct ccw_device console_cdev; | |
1f4e7eda | 1709 | static char console_cdev_name[10] = "0.x.xxxx"; |
1da177e4 LT |
1710 | static struct ccw_device_private console_private; |
1711 | static int console_cdev_in_use; | |
1712 | ||
2ec22984 CH |
1713 | static DEFINE_SPINLOCK(ccw_console_lock); |
1714 | ||
1715 | spinlock_t * cio_get_console_lock(void) | |
1716 | { | |
1717 | return &ccw_console_lock; | |
1718 | } | |
1719 | ||
0ae7a7b2 CH |
1720 | static int ccw_device_console_enable(struct ccw_device *cdev, |
1721 | struct subchannel *sch) | |
1da177e4 LT |
1722 | { |
1723 | int rc; | |
1724 | ||
cd6b4f27 CH |
1725 | /* Attach subchannel private data. */ |
1726 | sch->private = cio_get_console_priv(); | |
1727 | memset(sch->private, 0, sizeof(struct io_subchannel_private)); | |
0ae7a7b2 | 1728 | io_subchannel_init_fields(sch); |
f444cc0e SO |
1729 | rc = cio_commit_config(sch); |
1730 | if (rc) | |
1731 | return rc; | |
0ae7a7b2 | 1732 | sch->driver = &io_subchannel_driver; |
1da177e4 | 1733 | /* Initialize the ccw_device structure. */ |
292888c8 | 1734 | cdev->dev.parent= &sch->dev; |
1da177e4 LT |
1735 | rc = io_subchannel_recog(cdev, sch); |
1736 | if (rc) | |
1737 | return rc; | |
1738 | ||
1739 | /* Now wait for the async. recognition to come to an end. */ | |
1740 | spin_lock_irq(cdev->ccwlock); | |
1741 | while (!dev_fsm_final_state(cdev)) | |
1742 | wait_cons_dev(); | |
1743 | rc = -EIO; | |
1744 | if (cdev->private->state != DEV_STATE_OFFLINE) | |
1745 | goto out_unlock; | |
1746 | ccw_device_online(cdev); | |
1747 | while (!dev_fsm_final_state(cdev)) | |
1748 | wait_cons_dev(); | |
1749 | if (cdev->private->state != DEV_STATE_ONLINE) | |
1750 | goto out_unlock; | |
1751 | rc = 0; | |
1752 | out_unlock: | |
1753 | spin_unlock_irq(cdev->ccwlock); | |
1754 | return 0; | |
1755 | } | |
1756 | ||
1757 | struct ccw_device * | |
1758 | ccw_device_probe_console(void) | |
1759 | { | |
1760 | struct subchannel *sch; | |
1761 | int ret; | |
1762 | ||
1763 | if (xchg(&console_cdev_in_use, 1) != 0) | |
600b5d16 | 1764 | return ERR_PTR(-EBUSY); |
1da177e4 LT |
1765 | sch = cio_probe_console(); |
1766 | if (IS_ERR(sch)) { | |
1767 | console_cdev_in_use = 0; | |
1768 | return (void *) sch; | |
1769 | } | |
1770 | memset(&console_cdev, 0, sizeof(struct ccw_device)); | |
1771 | memset(&console_private, 0, sizeof(struct ccw_device_private)); | |
1772 | console_cdev.private = &console_private; | |
c1637532 | 1773 | console_private.cdev = &console_cdev; |
1da177e4 LT |
1774 | ret = ccw_device_console_enable(&console_cdev, sch); |
1775 | if (ret) { | |
1776 | cio_release_console(); | |
1777 | console_cdev_in_use = 0; | |
1778 | return ERR_PTR(ret); | |
1779 | } | |
1780 | console_cdev.online = 1; | |
1781 | return &console_cdev; | |
1782 | } | |
1f4e7eda CH |
1783 | |
1784 | ||
1785 | const char *cio_get_console_cdev_name(struct subchannel *sch) | |
1786 | { | |
1787 | snprintf(console_cdev_name, 10, "0.%x.%04x", | |
1788 | sch->schid.ssid, sch->schib.pmcw.dev); | |
1789 | return (const char *)console_cdev_name; | |
1790 | } | |
1da177e4 LT |
1791 | #endif |
1792 | ||
1793 | /* | |
1794 | * get ccw_device matching the busid, but only if owned by cdrv | |
1795 | */ | |
b0744bd2 CH |
1796 | static int |
1797 | __ccwdev_check_busid(struct device *dev, void *id) | |
1798 | { | |
1799 | char *bus_id; | |
1800 | ||
12975aef | 1801 | bus_id = id; |
b0744bd2 | 1802 | |
98df67b3 | 1803 | return (strcmp(bus_id, dev_name(dev)) == 0); |
b0744bd2 CH |
1804 | } |
1805 | ||
1806 | ||
b2ffd8e9 CH |
1807 | /** |
1808 | * get_ccwdev_by_busid() - obtain device from a bus id | |
1809 | * @cdrv: driver the device is owned by | |
1810 | * @bus_id: bus id of the device to be searched | |
1811 | * | |
1812 | * This function searches all devices owned by @cdrv for a device with a bus | |
1813 | * id matching @bus_id. | |
1814 | * Returns: | |
1815 | * If a match is found, its reference count of the found device is increased | |
1816 | * and it is returned; else %NULL is returned. | |
1817 | */ | |
1818 | struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv, | |
1819 | const char *bus_id) | |
1da177e4 | 1820 | { |
b0744bd2 | 1821 | struct device *dev; |
1da177e4 LT |
1822 | struct device_driver *drv; |
1823 | ||
1824 | drv = get_driver(&cdrv->driver); | |
1825 | if (!drv) | |
b0744bd2 | 1826 | return NULL; |
1da177e4 | 1827 | |
b0744bd2 CH |
1828 | dev = driver_find_device(drv, NULL, (void *)bus_id, |
1829 | __ccwdev_check_busid); | |
1da177e4 LT |
1830 | put_driver(drv); |
1831 | ||
d2c993d8 | 1832 | return dev ? to_ccwdev(dev) : NULL; |
1da177e4 LT |
1833 | } |
1834 | ||
1835 | /************************** device driver handling ************************/ | |
1836 | ||
1837 | /* This is the implementation of the ccw_driver class. The probe, remove | |
1838 | * and release methods are initially very similar to the device_driver | |
1839 | * implementations, with the difference that they have ccw_device | |
1840 | * arguments. | |
1841 | * | |
1842 | * A ccw driver also contains the information that is needed for | |
1843 | * device matching. | |
1844 | */ | |
1845 | static int | |
1846 | ccw_device_probe (struct device *dev) | |
1847 | { | |
1848 | struct ccw_device *cdev = to_ccwdev(dev); | |
1849 | struct ccw_driver *cdrv = to_ccwdrv(dev->driver); | |
1850 | int ret; | |
1851 | ||
1852 | cdev->drv = cdrv; /* to let the driver call _set_online */ | |
1853 | ||
1854 | ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV; | |
1855 | ||
1856 | if (ret) { | |
d2c993d8 | 1857 | cdev->drv = NULL; |
1da177e4 LT |
1858 | return ret; |
1859 | } | |
1860 | ||
1861 | return 0; | |
1862 | } | |
1863 | ||
1864 | static int | |
1865 | ccw_device_remove (struct device *dev) | |
1866 | { | |
1867 | struct ccw_device *cdev = to_ccwdev(dev); | |
1868 | struct ccw_driver *cdrv = cdev->drv; | |
1869 | int ret; | |
1870 | ||
1da177e4 LT |
1871 | if (cdrv->remove) |
1872 | cdrv->remove(cdev); | |
1873 | if (cdev->online) { | |
1874 | cdev->online = 0; | |
1875 | spin_lock_irq(cdev->ccwlock); | |
1876 | ret = ccw_device_offline(cdev); | |
1877 | spin_unlock_irq(cdev->ccwlock); | |
1878 | if (ret == 0) | |
1879 | wait_event(cdev->private->wait_q, | |
1880 | dev_fsm_final_state(cdev)); | |
1881 | else | |
139b83dd | 1882 | CIO_MSG_EVENT(0, "ccw_device_offline returned %d, " |
e556bbbd CH |
1883 | "device 0.%x.%04x\n", |
1884 | ret, cdev->private->dev_id.ssid, | |
1885 | cdev->private->dev_id.devno); | |
9cd67421 CH |
1886 | /* Give up reference obtained in ccw_device_set_online(). */ |
1887 | put_device(&cdev->dev); | |
1da177e4 LT |
1888 | } |
1889 | ccw_device_set_timeout(cdev, 0); | |
d2c993d8 | 1890 | cdev->drv = NULL; |
1da177e4 LT |
1891 | return 0; |
1892 | } | |
1893 | ||
958974fb CH |
1894 | static void ccw_device_shutdown(struct device *dev) |
1895 | { | |
1896 | struct ccw_device *cdev; | |
1897 | ||
1898 | cdev = to_ccwdev(dev); | |
1899 | if (cdev->drv && cdev->drv->shutdown) | |
1900 | cdev->drv->shutdown(cdev); | |
1842f2b1 | 1901 | disable_cmf(cdev); |
958974fb CH |
1902 | } |
1903 | ||
8bbace7e CH |
1904 | struct bus_type ccw_bus_type = { |
1905 | .name = "ccw", | |
1906 | .match = ccw_bus_match, | |
1907 | .uevent = ccw_uevent, | |
1908 | .probe = ccw_device_probe, | |
1909 | .remove = ccw_device_remove, | |
958974fb | 1910 | .shutdown = ccw_device_shutdown, |
8bbace7e CH |
1911 | }; |
1912 | ||
b2ffd8e9 CH |
1913 | /** |
1914 | * ccw_driver_register() - register a ccw driver | |
1915 | * @cdriver: driver to be registered | |
1916 | * | |
1917 | * This function is mainly a wrapper around driver_register(). | |
1918 | * Returns: | |
1919 | * %0 on success and a negative error value on failure. | |
1920 | */ | |
1921 | int ccw_driver_register(struct ccw_driver *cdriver) | |
1da177e4 LT |
1922 | { |
1923 | struct device_driver *drv = &cdriver->driver; | |
1924 | ||
1925 | drv->bus = &ccw_bus_type; | |
1926 | drv->name = cdriver->name; | |
4beee646 | 1927 | drv->owner = cdriver->owner; |
1da177e4 LT |
1928 | |
1929 | return driver_register(drv); | |
1930 | } | |
1931 | ||
b2ffd8e9 CH |
1932 | /** |
1933 | * ccw_driver_unregister() - deregister a ccw driver | |
1934 | * @cdriver: driver to be deregistered | |
1935 | * | |
1936 | * This function is mainly a wrapper around driver_unregister(). | |
1937 | */ | |
1938 | void ccw_driver_unregister(struct ccw_driver *cdriver) | |
1da177e4 LT |
1939 | { |
1940 | driver_unregister(&cdriver->driver); | |
1941 | } | |
1942 | ||
a8237fc4 CH |
1943 | /* Helper func for qdio. */ |
1944 | struct subchannel_id | |
1945 | ccw_device_get_subchannel_id(struct ccw_device *cdev) | |
1946 | { | |
1947 | struct subchannel *sch; | |
1948 | ||
1949 | sch = to_subchannel(cdev->dev.parent); | |
1950 | return sch->schid; | |
1951 | } | |
1952 | ||
1da177e4 LT |
1953 | MODULE_LICENSE("GPL"); |
1954 | EXPORT_SYMBOL(ccw_device_set_online); | |
1955 | EXPORT_SYMBOL(ccw_device_set_offline); | |
1956 | EXPORT_SYMBOL(ccw_driver_register); | |
1957 | EXPORT_SYMBOL(ccw_driver_unregister); | |
1958 | EXPORT_SYMBOL(get_ccwdev_by_busid); | |
1959 | EXPORT_SYMBOL(ccw_bus_type); | |
1960 | EXPORT_SYMBOL(ccw_device_work); | |
a8237fc4 | 1961 | EXPORT_SYMBOL_GPL(ccw_device_get_subchannel_id); |