1 // SPDX-License-Identifier: GPL-1.0+
2 /*
3  *  bus driver for ccw devices
4  *
5  *    Copyright IBM Corp. 2002, 2008
6  *    Author(s): Arnd Bergmann (arndb@de.ibm.com)
7  *		 Cornelia Huck (cornelia.huck@de.ibm.com)
8  *		 Martin Schwidefsky (schwidefsky@de.ibm.com)
9  */
10 
11 #define KMSG_COMPONENT "cio"
12 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 
14 #include <linux/export.h>
15 #include <linux/init.h>
16 #include <linux/spinlock.h>
17 #include <linux/errno.h>
18 #include <linux/err.h>
19 #include <linux/slab.h>
20 #include <linux/list.h>
21 #include <linux/device.h>
22 #include <linux/workqueue.h>
23 #include <linux/delay.h>
24 #include <linux/timer.h>
25 #include <linux/kernel_stat.h>
26 #include <linux/sched/signal.h>
27 
28 #include <asm/ccwdev.h>
29 #include <asm/cio.h>
30 #include <asm/param.h>		/* HZ */
31 #include <asm/cmb.h>
32 #include <asm/isc.h>
33 
34 #include "chp.h"
35 #include "cio.h"
36 #include "cio_debug.h"
37 #include "css.h"
38 #include "device.h"
39 #include "ioasm.h"
40 #include "io_sch.h"
41 #include "blacklist.h"
42 #include "chsc.h"
43 
44 static struct timer_list recovery_timer;
45 static DEFINE_SPINLOCK(recovery_lock);
46 static int recovery_phase;
47 static const unsigned long recovery_delay[] = { 3, 30, 300 };
48 
49 static atomic_t ccw_device_init_count = ATOMIC_INIT(0);
50 static DECLARE_WAIT_QUEUE_HEAD(ccw_device_init_wq);
51 static struct bus_type ccw_bus_type;
52 
53 /******************* bus type handling ***********************/
54 
55 /* The Linux driver model distinguishes between a bus type and
56  * the bus itself. Of course we only have one channel
57  * subsystem driver and one channel system per machine, but
58  * we still use the abstraction. T.R. says it's a good idea. */
59 static int
ccw_bus_match(struct device * dev,struct device_driver * drv)60 ccw_bus_match (struct device * dev, struct device_driver * drv)
61 {
62 	struct ccw_device *cdev = to_ccwdev(dev);
63 	struct ccw_driver *cdrv = to_ccwdrv(drv);
64 	const struct ccw_device_id *ids = cdrv->ids, *found;
65 
66 	if (!ids)
67 		return 0;
68 
69 	found = ccw_device_id_match(ids, &cdev->id);
70 	if (!found)
71 		return 0;
72 
73 	cdev->id.driver_info = found->driver_info;
74 
75 	return 1;
76 }
77 
78 /* Store modalias string delimited by prefix/suffix string into buffer with
79  * specified size. Return length of resulting string (excluding trailing '\0')
80  * even if string doesn't fit buffer (snprintf semantics). */
snprint_alias(char * buf,size_t size,struct ccw_device_id * id,const char * suffix)81 static int snprint_alias(char *buf, size_t size,
82 			 struct ccw_device_id *id, const char *suffix)
83 {
84 	int len;
85 
86 	len = snprintf(buf, size, "ccw:t%04Xm%02X", id->cu_type, id->cu_model);
87 	if (len > size)
88 		return len;
89 	buf += len;
90 	size -= len;
91 
92 	if (id->dev_type != 0)
93 		len += snprintf(buf, size, "dt%04Xdm%02X%s", id->dev_type,
94 				id->dev_model, suffix);
95 	else
96 		len += snprintf(buf, size, "dtdm%s", suffix);
97 
98 	return len;
99 }
100 
101 /* Set up environment variables for ccw device uevent. Return 0 on success,
102  * non-zero otherwise. */
ccw_uevent(struct device * dev,struct kobj_uevent_env * env)103 static int ccw_uevent(struct device *dev, struct kobj_uevent_env *env)
104 {
105 	struct ccw_device *cdev = to_ccwdev(dev);
106 	struct ccw_device_id *id = &(cdev->id);
107 	int ret;
108 	char modalias_buf[30];
109 
110 	/* CU_TYPE= */
111 	ret = add_uevent_var(env, "CU_TYPE=%04X", id->cu_type);
112 	if (ret)
113 		return ret;
114 
115 	/* CU_MODEL= */
116 	ret = add_uevent_var(env, "CU_MODEL=%02X", id->cu_model);
117 	if (ret)
118 		return ret;
119 
120 	/* The next two can be zero, that's ok for us */
121 	/* DEV_TYPE= */
122 	ret = add_uevent_var(env, "DEV_TYPE=%04X", id->dev_type);
123 	if (ret)
124 		return ret;
125 
126 	/* DEV_MODEL= */
127 	ret = add_uevent_var(env, "DEV_MODEL=%02X", id->dev_model);
128 	if (ret)
129 		return ret;
130 
131 	/* MODALIAS=  */
132 	snprint_alias(modalias_buf, sizeof(modalias_buf), id, "");
133 	ret = add_uevent_var(env, "MODALIAS=%s", modalias_buf);
134 	return ret;
135 }
136 
137 static void io_subchannel_irq(struct subchannel *);
138 static int io_subchannel_probe(struct subchannel *);
139 static int io_subchannel_remove(struct subchannel *);
140 static void io_subchannel_shutdown(struct subchannel *);
141 static int io_subchannel_sch_event(struct subchannel *, int);
142 static int io_subchannel_chp_event(struct subchannel *, struct chp_link *,
143 				   int);
144 static void recovery_func(struct timer_list *unused);
145 
146 static struct css_device_id io_subchannel_ids[] = {
147 	{ .match_flags = 0x1, .type = SUBCHANNEL_TYPE_IO, },
148 	{ /* end of list */ },
149 };
150 
io_subchannel_prepare(struct subchannel * sch)151 static int io_subchannel_prepare(struct subchannel *sch)
152 {
153 	struct ccw_device *cdev;
154 	/*
155 	 * Don't allow suspend while a ccw device registration
156 	 * is still outstanding.
157 	 */
158 	cdev = sch_get_cdev(sch);
159 	if (cdev && !device_is_registered(&cdev->dev))
160 		return -EAGAIN;
161 	return 0;
162 }
163 
io_subchannel_settle(void)164 static int io_subchannel_settle(void)
165 {
166 	int ret;
167 
168 	ret = wait_event_interruptible(ccw_device_init_wq,
169 				atomic_read(&ccw_device_init_count) == 0);
170 	if (ret)
171 		return -EINTR;
172 	flush_workqueue(cio_work_q);
173 	return 0;
174 }
175 
176 static struct css_driver io_subchannel_driver = {
177 	.drv = {
178 		.owner = THIS_MODULE,
179 		.name = "io_subchannel",
180 	},
181 	.subchannel_type = io_subchannel_ids,
182 	.irq = io_subchannel_irq,
183 	.sch_event = io_subchannel_sch_event,
184 	.chp_event = io_subchannel_chp_event,
185 	.probe = io_subchannel_probe,
186 	.remove = io_subchannel_remove,
187 	.shutdown = io_subchannel_shutdown,
188 	.prepare = io_subchannel_prepare,
189 	.settle = io_subchannel_settle,
190 };
191 
io_subchannel_init(void)192 int __init io_subchannel_init(void)
193 {
194 	int ret;
195 
196 	timer_setup(&recovery_timer, recovery_func, 0);
197 	ret = bus_register(&ccw_bus_type);
198 	if (ret)
199 		return ret;
200 	ret = css_driver_register(&io_subchannel_driver);
201 	if (ret)
202 		bus_unregister(&ccw_bus_type);
203 
204 	return ret;
205 }
206 
207 
208 /************************ device handling **************************/
209 
210 static ssize_t
devtype_show(struct device * dev,struct device_attribute * attr,char * buf)211 devtype_show (struct device *dev, struct device_attribute *attr, char *buf)
212 {
213 	struct ccw_device *cdev = to_ccwdev(dev);
214 	struct ccw_device_id *id = &(cdev->id);
215 
216 	if (id->dev_type != 0)
217 		return sprintf(buf, "%04x/%02x\n",
218 				id->dev_type, id->dev_model);
219 	else
220 		return sprintf(buf, "n/a\n");
221 }
222 
223 static ssize_t
cutype_show(struct device * dev,struct device_attribute * attr,char * buf)224 cutype_show (struct device *dev, struct device_attribute *attr, char *buf)
225 {
226 	struct ccw_device *cdev = to_ccwdev(dev);
227 	struct ccw_device_id *id = &(cdev->id);
228 
229 	return sprintf(buf, "%04x/%02x\n",
230 		       id->cu_type, id->cu_model);
231 }
232 
233 static ssize_t
modalias_show(struct device * dev,struct device_attribute * attr,char * buf)234 modalias_show (struct device *dev, struct device_attribute *attr, char *buf)
235 {
236 	struct ccw_device *cdev = to_ccwdev(dev);
237 	struct ccw_device_id *id = &(cdev->id);
238 	int len;
239 
240 	len = snprint_alias(buf, PAGE_SIZE, id, "\n");
241 
242 	return len > PAGE_SIZE ? PAGE_SIZE : len;
243 }
244 
245 static ssize_t
online_show(struct device * dev,struct device_attribute * attr,char * buf)246 online_show (struct device *dev, struct device_attribute *attr, char *buf)
247 {
248 	struct ccw_device *cdev = to_ccwdev(dev);
249 
250 	return sprintf(buf, cdev->online ? "1\n" : "0\n");
251 }
252 
ccw_device_is_orphan(struct ccw_device * cdev)253 int ccw_device_is_orphan(struct ccw_device *cdev)
254 {
255 	return sch_is_pseudo_sch(to_subchannel(cdev->dev.parent));
256 }
257 
ccw_device_unregister(struct ccw_device * cdev)258 static void ccw_device_unregister(struct ccw_device *cdev)
259 {
260 	if (device_is_registered(&cdev->dev)) {
261 		/* Undo device_add(). */
262 		device_del(&cdev->dev);
263 	}
264 	if (cdev->private->flags.initialized) {
265 		cdev->private->flags.initialized = 0;
266 		/* Release reference from device_initialize(). */
267 		put_device(&cdev->dev);
268 	}
269 }
270 
271 static void io_subchannel_quiesce(struct subchannel *);
272 
273 /**
274  * ccw_device_set_offline() - disable a ccw device for I/O
275  * @cdev: target ccw device
276  *
277  * This function calls the driver's set_offline() function for @cdev, if
278  * given, and then disables @cdev.
279  * Returns:
280  *   %0 on success and a negative error value on failure.
281  * Context:
282  *  enabled, ccw device lock not held
283  */
ccw_device_set_offline(struct ccw_device * cdev)284 int ccw_device_set_offline(struct ccw_device *cdev)
285 {
286 	struct subchannel *sch;
287 	int ret, state;
288 
289 	if (!cdev)
290 		return -ENODEV;
291 	if (!cdev->online || !cdev->drv)
292 		return -EINVAL;
293 
294 	if (cdev->drv->set_offline) {
295 		ret = cdev->drv->set_offline(cdev);
296 		if (ret != 0)
297 			return ret;
298 	}
299 	spin_lock_irq(cdev->ccwlock);
300 	sch = to_subchannel(cdev->dev.parent);
301 	cdev->online = 0;
302 	/* Wait until a final state or DISCONNECTED is reached */
303 	while (!dev_fsm_final_state(cdev) &&
304 	       cdev->private->state != DEV_STATE_DISCONNECTED) {
305 		spin_unlock_irq(cdev->ccwlock);
306 		wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
307 			   cdev->private->state == DEV_STATE_DISCONNECTED));
308 		spin_lock_irq(cdev->ccwlock);
309 	}
310 	do {
311 		ret = ccw_device_offline(cdev);
312 		if (!ret)
313 			break;
314 		CIO_MSG_EVENT(0, "ccw_device_offline returned %d, device "
315 			      "0.%x.%04x\n", ret, cdev->private->dev_id.ssid,
316 			      cdev->private->dev_id.devno);
317 		if (ret != -EBUSY)
318 			goto error;
319 		state = cdev->private->state;
320 		spin_unlock_irq(cdev->ccwlock);
321 		io_subchannel_quiesce(sch);
322 		spin_lock_irq(cdev->ccwlock);
323 		cdev->private->state = state;
324 	} while (ret == -EBUSY);
325 	spin_unlock_irq(cdev->ccwlock);
326 	wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
327 		   cdev->private->state == DEV_STATE_DISCONNECTED));
328 	/* Inform the user if set offline failed. */
329 	if (cdev->private->state == DEV_STATE_BOXED) {
330 		pr_warn("%s: The device entered boxed state while being set offline\n",
331 			dev_name(&cdev->dev));
332 	} else if (cdev->private->state == DEV_STATE_NOT_OPER) {
333 		pr_warn("%s: The device stopped operating while being set offline\n",
334 			dev_name(&cdev->dev));
335 	}
336 	/* Give up reference from ccw_device_set_online(). */
337 	put_device(&cdev->dev);
338 	return 0;
339 
340 error:
341 	cdev->private->state = DEV_STATE_OFFLINE;
342 	dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
343 	spin_unlock_irq(cdev->ccwlock);
344 	/* Give up reference from ccw_device_set_online(). */
345 	put_device(&cdev->dev);
346 	return -ENODEV;
347 }
348 
349 /**
350  * ccw_device_set_online() - enable a ccw device for I/O
351  * @cdev: target ccw device
352  *
353  * This function first enables @cdev and then calls the driver's set_online()
354  * function for @cdev, if given. If set_online() returns an error, @cdev is
355  * disabled again.
356  * Returns:
357  *   %0 on success and a negative error value on failure.
358  * Context:
359  *  enabled, ccw device lock not held
360  */
ccw_device_set_online(struct ccw_device * cdev)361 int ccw_device_set_online(struct ccw_device *cdev)
362 {
363 	int ret;
364 	int ret2;
365 
366 	if (!cdev)
367 		return -ENODEV;
368 	if (cdev->online || !cdev->drv)
369 		return -EINVAL;
370 	/* Hold on to an extra reference while device is online. */
371 	if (!get_device(&cdev->dev))
372 		return -ENODEV;
373 
374 	spin_lock_irq(cdev->ccwlock);
375 	ret = ccw_device_online(cdev);
376 	spin_unlock_irq(cdev->ccwlock);
377 	if (ret == 0)
378 		wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
379 	else {
380 		CIO_MSG_EVENT(0, "ccw_device_online returned %d, "
381 			      "device 0.%x.%04x\n",
382 			      ret, cdev->private->dev_id.ssid,
383 			      cdev->private->dev_id.devno);
384 		/* Give up online reference since onlining failed. */
385 		put_device(&cdev->dev);
386 		return ret;
387 	}
388 	spin_lock_irq(cdev->ccwlock);
389 	/* Check if online processing was successful */
390 	if ((cdev->private->state != DEV_STATE_ONLINE) &&
391 	    (cdev->private->state != DEV_STATE_W4SENSE)) {
392 		spin_unlock_irq(cdev->ccwlock);
393 		/* Inform the user that set online failed. */
394 		if (cdev->private->state == DEV_STATE_BOXED) {
395 			pr_warn("%s: Setting the device online failed because it is boxed\n",
396 				dev_name(&cdev->dev));
397 		} else if (cdev->private->state == DEV_STATE_NOT_OPER) {
398 			pr_warn("%s: Setting the device online failed because it is not operational\n",
399 				dev_name(&cdev->dev));
400 		}
401 		/* Give up online reference since onlining failed. */
402 		put_device(&cdev->dev);
403 		return -ENODEV;
404 	}
405 	spin_unlock_irq(cdev->ccwlock);
406 	if (cdev->drv->set_online)
407 		ret = cdev->drv->set_online(cdev);
408 	if (ret)
409 		goto rollback;
410 
411 	spin_lock_irq(cdev->ccwlock);
412 	cdev->online = 1;
413 	spin_unlock_irq(cdev->ccwlock);
414 	return 0;
415 
416 rollback:
417 	spin_lock_irq(cdev->ccwlock);
418 	/* Wait until a final state or DISCONNECTED is reached */
419 	while (!dev_fsm_final_state(cdev) &&
420 	       cdev->private->state != DEV_STATE_DISCONNECTED) {
421 		spin_unlock_irq(cdev->ccwlock);
422 		wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
423 			   cdev->private->state == DEV_STATE_DISCONNECTED));
424 		spin_lock_irq(cdev->ccwlock);
425 	}
426 	ret2 = ccw_device_offline(cdev);
427 	if (ret2)
428 		goto error;
429 	spin_unlock_irq(cdev->ccwlock);
430 	wait_event(cdev->private->wait_q, (dev_fsm_final_state(cdev) ||
431 		   cdev->private->state == DEV_STATE_DISCONNECTED));
432 	/* Give up online reference since onlining failed. */
433 	put_device(&cdev->dev);
434 	return ret;
435 
436 error:
437 	CIO_MSG_EVENT(0, "rollback ccw_device_offline returned %d, "
438 		      "device 0.%x.%04x\n",
439 		      ret2, cdev->private->dev_id.ssid,
440 		      cdev->private->dev_id.devno);
441 	cdev->private->state = DEV_STATE_OFFLINE;
442 	spin_unlock_irq(cdev->ccwlock);
443 	/* Give up online reference since onlining failed. */
444 	put_device(&cdev->dev);
445 	return ret;
446 }
447 
online_store_handle_offline(struct ccw_device * cdev)448 static int online_store_handle_offline(struct ccw_device *cdev)
449 {
450 	if (cdev->private->state == DEV_STATE_DISCONNECTED) {
451 		spin_lock_irq(cdev->ccwlock);
452 		ccw_device_sched_todo(cdev, CDEV_TODO_UNREG_EVAL);
453 		spin_unlock_irq(cdev->ccwlock);
454 		return 0;
455 	}
456 	if (cdev->drv && cdev->drv->set_offline)
457 		return ccw_device_set_offline(cdev);
458 	return -EINVAL;
459 }
460 
online_store_recog_and_online(struct ccw_device * cdev)461 static int online_store_recog_and_online(struct ccw_device *cdev)
462 {
463 	/* Do device recognition, if needed. */
464 	if (cdev->private->state == DEV_STATE_BOXED) {
465 		spin_lock_irq(cdev->ccwlock);
466 		ccw_device_recognition(cdev);
467 		spin_unlock_irq(cdev->ccwlock);
468 		wait_event(cdev->private->wait_q,
469 			   cdev->private->flags.recog_done);
470 		if (cdev->private->state != DEV_STATE_OFFLINE)
471 			/* recognition failed */
472 			return -EAGAIN;
473 	}
474 	if (cdev->drv && cdev->drv->set_online)
475 		return ccw_device_set_online(cdev);
476 	return -EINVAL;
477 }
478 
online_store_handle_online(struct ccw_device * cdev,int force)479 static int online_store_handle_online(struct ccw_device *cdev, int force)
480 {
481 	int ret;
482 
483 	ret = online_store_recog_and_online(cdev);
484 	if (ret && !force)
485 		return ret;
486 	if (force && cdev->private->state == DEV_STATE_BOXED) {
487 		ret = ccw_device_stlck(cdev);
488 		if (ret)
489 			return ret;
490 		if (cdev->id.cu_type == 0)
491 			cdev->private->state = DEV_STATE_NOT_OPER;
492 		ret = online_store_recog_and_online(cdev);
493 		if (ret)
494 			return ret;
495 	}
496 	return 0;
497 }
498 
online_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)499 static ssize_t online_store (struct device *dev, struct device_attribute *attr,
500 			     const char *buf, size_t count)
501 {
502 	struct ccw_device *cdev = to_ccwdev(dev);
503 	int force, ret;
504 	unsigned long i;
505 
506 	/* Prevent conflict between multiple on-/offline processing requests. */
507 	if (atomic_cmpxchg(&cdev->private->onoff, 0, 1) != 0)
508 		return -EAGAIN;
509 	/* Prevent conflict between internal I/Os and on-/offline processing. */
510 	if (!dev_fsm_final_state(cdev) &&
511 	    cdev->private->state != DEV_STATE_DISCONNECTED) {
512 		ret = -EAGAIN;
513 		goto out;
514 	}
515 	/* Prevent conflict between pending work and on-/offline processing.*/
516 	if (work_pending(&cdev->private->todo_work)) {
517 		ret = -EAGAIN;
518 		goto out;
519 	}
520 	if (!strncmp(buf, "force\n", count)) {
521 		force = 1;
522 		i = 1;
523 		ret = 0;
524 	} else {
525 		force = 0;
526 		ret = kstrtoul(buf, 16, &i);
527 	}
528 	if (ret)
529 		goto out;
530 
531 	device_lock(dev);
532 	switch (i) {
533 	case 0:
534 		ret = online_store_handle_offline(cdev);
535 		break;
536 	case 1:
537 		ret = online_store_handle_online(cdev, force);
538 		break;
539 	default:
540 		ret = -EINVAL;
541 	}
542 	device_unlock(dev);
543 
544 out:
545 	atomic_set(&cdev->private->onoff, 0);
546 	return (ret < 0) ? ret : count;
547 }
548 
549 static ssize_t
available_show(struct device * dev,struct device_attribute * attr,char * buf)550 available_show (struct device *dev, struct device_attribute *attr, char *buf)
551 {
552 	struct ccw_device *cdev = to_ccwdev(dev);
553 	struct subchannel *sch;
554 
555 	if (ccw_device_is_orphan(cdev))
556 		return sprintf(buf, "no device\n");
557 	switch (cdev->private->state) {
558 	case DEV_STATE_BOXED:
559 		return sprintf(buf, "boxed\n");
560 	case DEV_STATE_DISCONNECTED:
561 	case DEV_STATE_DISCONNECTED_SENSE_ID:
562 	case DEV_STATE_NOT_OPER:
563 		sch = to_subchannel(dev->parent);
564 		if (!sch->lpm)
565 			return sprintf(buf, "no path\n");
566 		else
567 			return sprintf(buf, "no device\n");
568 	default:
569 		/* All other states considered fine. */
570 		return sprintf(buf, "good\n");
571 	}
572 }
573 
574 static ssize_t
initiate_logging(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)575 initiate_logging(struct device *dev, struct device_attribute *attr,
576 		 const char *buf, size_t count)
577 {
578 	struct subchannel *sch = to_subchannel(dev);
579 	int rc;
580 
581 	rc = chsc_siosl(sch->schid);
582 	if (rc < 0) {
583 		pr_warn("Logging for subchannel 0.%x.%04x failed with errno=%d\n",
584 			sch->schid.ssid, sch->schid.sch_no, rc);
585 		return rc;
586 	}
587 	pr_notice("Logging for subchannel 0.%x.%04x was triggered\n",
588 		  sch->schid.ssid, sch->schid.sch_no);
589 	return count;
590 }
591 
vpm_show(struct device * dev,struct device_attribute * attr,char * buf)592 static ssize_t vpm_show(struct device *dev, struct device_attribute *attr,
593 			char *buf)
594 {
595 	struct subchannel *sch = to_subchannel(dev);
596 
597 	return sprintf(buf, "%02x\n", sch->vpm);
598 }
599 
600 static DEVICE_ATTR_RO(devtype);
601 static DEVICE_ATTR_RO(cutype);
602 static DEVICE_ATTR_RO(modalias);
603 static DEVICE_ATTR_RW(online);
604 static DEVICE_ATTR(availability, 0444, available_show, NULL);
605 static DEVICE_ATTR(logging, 0200, NULL, initiate_logging);
606 static DEVICE_ATTR_RO(vpm);
607 
608 static struct attribute *io_subchannel_attrs[] = {
609 	&dev_attr_logging.attr,
610 	&dev_attr_vpm.attr,
611 	NULL,
612 };
613 
614 static const struct attribute_group io_subchannel_attr_group = {
615 	.attrs = io_subchannel_attrs,
616 };
617 
618 static struct attribute * ccwdev_attrs[] = {
619 	&dev_attr_devtype.attr,
620 	&dev_attr_cutype.attr,
621 	&dev_attr_modalias.attr,
622 	&dev_attr_online.attr,
623 	&dev_attr_cmb_enable.attr,
624 	&dev_attr_availability.attr,
625 	NULL,
626 };
627 
628 static const struct attribute_group ccwdev_attr_group = {
629 	.attrs = ccwdev_attrs,
630 };
631 
632 static const struct attribute_group *ccwdev_attr_groups[] = {
633 	&ccwdev_attr_group,
634 	NULL,
635 };
636 
ccw_device_add(struct ccw_device * cdev)637 static int ccw_device_add(struct ccw_device *cdev)
638 {
639 	struct device *dev = &cdev->dev;
640 
641 	dev->bus = &ccw_bus_type;
642 	return device_add(dev);
643 }
644 
match_dev_id(struct device * dev,void * data)645 static int match_dev_id(struct device *dev, void *data)
646 {
647 	struct ccw_device *cdev = to_ccwdev(dev);
648 	struct ccw_dev_id *dev_id = data;
649 
650 	return ccw_dev_id_is_equal(&cdev->private->dev_id, dev_id);
651 }
652 
653 /**
654  * get_ccwdev_by_dev_id() - obtain device from a ccw device id
655  * @dev_id: id of the device to be searched
656  *
657  * This function searches all devices attached to the ccw bus for a device
658  * matching @dev_id.
659  * Returns:
660  *  If a device is found its reference count is increased and returned;
661  *  else %NULL is returned.
662  */
get_ccwdev_by_dev_id(struct ccw_dev_id * dev_id)663 struct ccw_device *get_ccwdev_by_dev_id(struct ccw_dev_id *dev_id)
664 {
665 	struct device *dev;
666 
667 	dev = bus_find_device(&ccw_bus_type, NULL, dev_id, match_dev_id);
668 
669 	return dev ? to_ccwdev(dev) : NULL;
670 }
671 EXPORT_SYMBOL_GPL(get_ccwdev_by_dev_id);
672 
ccw_device_do_unbind_bind(struct ccw_device * cdev)673 static void ccw_device_do_unbind_bind(struct ccw_device *cdev)
674 {
675 	int ret;
676 
677 	if (device_is_registered(&cdev->dev)) {
678 		device_release_driver(&cdev->dev);
679 		ret = device_attach(&cdev->dev);
680 		WARN_ON(ret == -ENODEV);
681 	}
682 }
683 
684 static void
ccw_device_release(struct device * dev)685 ccw_device_release(struct device *dev)
686 {
687 	struct ccw_device *cdev;
688 
689 	cdev = to_ccwdev(dev);
690 	/* Release reference of parent subchannel. */
691 	put_device(cdev->dev.parent);
692 	kfree(cdev->private);
693 	kfree(cdev);
694 }
695 
io_subchannel_allocate_dev(struct subchannel * sch)696 static struct ccw_device * io_subchannel_allocate_dev(struct subchannel *sch)
697 {
698 	struct ccw_device *cdev;
699 
700 	cdev  = kzalloc(sizeof(*cdev), GFP_KERNEL);
701 	if (cdev) {
702 		cdev->private = kzalloc(sizeof(struct ccw_device_private),
703 					GFP_KERNEL | GFP_DMA);
704 		if (cdev->private)
705 			return cdev;
706 	}
707 	kfree(cdev);
708 	return ERR_PTR(-ENOMEM);
709 }
710 
711 static void ccw_device_todo(struct work_struct *work);
712 
io_subchannel_initialize_dev(struct subchannel * sch,struct ccw_device * cdev)713 static int io_subchannel_initialize_dev(struct subchannel *sch,
714 					struct ccw_device *cdev)
715 {
716 	struct ccw_device_private *priv = cdev->private;
717 	int ret;
718 
719 	priv->cdev = cdev;
720 	priv->int_class = IRQIO_CIO;
721 	priv->state = DEV_STATE_NOT_OPER;
722 	priv->dev_id.devno = sch->schib.pmcw.dev;
723 	priv->dev_id.ssid = sch->schid.ssid;
724 
725 	INIT_WORK(&priv->todo_work, ccw_device_todo);
726 	INIT_LIST_HEAD(&priv->cmb_list);
727 	init_waitqueue_head(&priv->wait_q);
728 	timer_setup(&priv->timer, ccw_device_timeout, 0);
729 
730 	atomic_set(&priv->onoff, 0);
731 	cdev->ccwlock = sch->lock;
732 	cdev->dev.parent = &sch->dev;
733 	cdev->dev.release = ccw_device_release;
734 	cdev->dev.groups = ccwdev_attr_groups;
735 	/* Do first half of device_register. */
736 	device_initialize(&cdev->dev);
737 	ret = dev_set_name(&cdev->dev, "0.%x.%04x", cdev->private->dev_id.ssid,
738 			   cdev->private->dev_id.devno);
739 	if (ret)
740 		goto out_put;
741 	if (!get_device(&sch->dev)) {
742 		ret = -ENODEV;
743 		goto out_put;
744 	}
745 	priv->flags.initialized = 1;
746 	spin_lock_irq(sch->lock);
747 	sch_set_cdev(sch, cdev);
748 	spin_unlock_irq(sch->lock);
749 	return 0;
750 
751 out_put:
752 	/* Release reference from device_initialize(). */
753 	put_device(&cdev->dev);
754 	return ret;
755 }
756 
io_subchannel_create_ccwdev(struct subchannel * sch)757 static struct ccw_device * io_subchannel_create_ccwdev(struct subchannel *sch)
758 {
759 	struct ccw_device *cdev;
760 	int ret;
761 
762 	cdev = io_subchannel_allocate_dev(sch);
763 	if (!IS_ERR(cdev)) {
764 		ret = io_subchannel_initialize_dev(sch, cdev);
765 		if (ret)
766 			cdev = ERR_PTR(ret);
767 	}
768 	return cdev;
769 }
770 
771 static void io_subchannel_recog(struct ccw_device *, struct subchannel *);
772 
sch_create_and_recog_new_device(struct subchannel * sch)773 static void sch_create_and_recog_new_device(struct subchannel *sch)
774 {
775 	struct ccw_device *cdev;
776 
777 	/* Need to allocate a new ccw device. */
778 	cdev = io_subchannel_create_ccwdev(sch);
779 	if (IS_ERR(cdev)) {
780 		/* OK, we did everything we could... */
781 		css_sch_device_unregister(sch);
782 		return;
783 	}
784 	/* Start recognition for the new ccw device. */
785 	io_subchannel_recog(cdev, sch);
786 }
787 
788 /*
789  * Register recognized device.
790  */
io_subchannel_register(struct ccw_device * cdev)791 static void io_subchannel_register(struct ccw_device *cdev)
792 {
793 	struct subchannel *sch;
794 	int ret, adjust_init_count = 1;
795 	unsigned long flags;
796 
797 	sch = to_subchannel(cdev->dev.parent);
798 	/*
799 	 * Check if subchannel is still registered. It may have become
800 	 * unregistered if a machine check hit us after finishing
801 	 * device recognition but before the register work could be
802 	 * queued.
803 	 */
804 	if (!device_is_registered(&sch->dev))
805 		goto out_err;
806 	css_update_ssd_info(sch);
807 	/*
808 	 * io_subchannel_register() will also be called after device
809 	 * recognition has been done for a boxed device (which will already
810 	 * be registered). We need to reprobe since we may now have sense id
811 	 * information.
812 	 */
813 	if (device_is_registered(&cdev->dev)) {
814 		if (!cdev->drv) {
815 			ret = device_reprobe(&cdev->dev);
816 			if (ret)
817 				/* We can't do much here. */
818 				CIO_MSG_EVENT(0, "device_reprobe() returned"
819 					      " %d for 0.%x.%04x\n", ret,
820 					      cdev->private->dev_id.ssid,
821 					      cdev->private->dev_id.devno);
822 		}
823 		adjust_init_count = 0;
824 		goto out;
825 	}
826 	/*
827 	 * Now we know this subchannel will stay, we can throw
828 	 * our delayed uevent.
829 	 */
830 	if (dev_get_uevent_suppress(&sch->dev)) {
831 		dev_set_uevent_suppress(&sch->dev, 0);
832 		kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
833 	}
834 	/* make it known to the system */
835 	ret = ccw_device_add(cdev);
836 	if (ret) {
837 		CIO_MSG_EVENT(0, "Could not register ccw dev 0.%x.%04x: %d\n",
838 			      cdev->private->dev_id.ssid,
839 			      cdev->private->dev_id.devno, ret);
840 		spin_lock_irqsave(sch->lock, flags);
841 		sch_set_cdev(sch, NULL);
842 		spin_unlock_irqrestore(sch->lock, flags);
843 		/* Release initial device reference. */
844 		put_device(&cdev->dev);
845 		goto out_err;
846 	}
847 out:
848 	cdev->private->flags.recog_done = 1;
849 	wake_up(&cdev->private->wait_q);
850 out_err:
851 	if (adjust_init_count && atomic_dec_and_test(&ccw_device_init_count))
852 		wake_up(&ccw_device_init_wq);
853 }
854 
ccw_device_call_sch_unregister(struct ccw_device * cdev)855 static void ccw_device_call_sch_unregister(struct ccw_device *cdev)
856 {
857 	struct subchannel *sch;
858 
859 	/* Get subchannel reference for local processing. */
860 	if (!get_device(cdev->dev.parent))
861 		return;
862 	sch = to_subchannel(cdev->dev.parent);
863 	css_sch_device_unregister(sch);
864 	/* Release subchannel reference for local processing. */
865 	put_device(&sch->dev);
866 }
867 
868 /*
869  * subchannel recognition done. Called from the state machine.
870  */
871 void
io_subchannel_recog_done(struct ccw_device * cdev)872 io_subchannel_recog_done(struct ccw_device *cdev)
873 {
874 	if (css_init_done == 0) {
875 		cdev->private->flags.recog_done = 1;
876 		return;
877 	}
878 	switch (cdev->private->state) {
879 	case DEV_STATE_BOXED:
880 		/* Device did not respond in time. */
881 	case DEV_STATE_NOT_OPER:
882 		cdev->private->flags.recog_done = 1;
883 		/* Remove device found not operational. */
884 		ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
885 		if (atomic_dec_and_test(&ccw_device_init_count))
886 			wake_up(&ccw_device_init_wq);
887 		break;
888 	case DEV_STATE_OFFLINE:
889 		/*
890 		 * We can't register the device in interrupt context so
891 		 * we schedule a work item.
892 		 */
893 		ccw_device_sched_todo(cdev, CDEV_TODO_REGISTER);
894 		break;
895 	}
896 }
897 
io_subchannel_recog(struct ccw_device * cdev,struct subchannel * sch)898 static void io_subchannel_recog(struct ccw_device *cdev, struct subchannel *sch)
899 {
900 	/* Increase counter of devices currently in recognition. */
901 	atomic_inc(&ccw_device_init_count);
902 
903 	/* Start async. device sensing. */
904 	spin_lock_irq(sch->lock);
905 	ccw_device_recognition(cdev);
906 	spin_unlock_irq(sch->lock);
907 }
908 
ccw_device_move_to_sch(struct ccw_device * cdev,struct subchannel * sch)909 static int ccw_device_move_to_sch(struct ccw_device *cdev,
910 				  struct subchannel *sch)
911 {
912 	struct subchannel *old_sch;
913 	int rc, old_enabled = 0;
914 
915 	old_sch = to_subchannel(cdev->dev.parent);
916 	/* Obtain child reference for new parent. */
917 	if (!get_device(&sch->dev))
918 		return -ENODEV;
919 
920 	if (!sch_is_pseudo_sch(old_sch)) {
921 		spin_lock_irq(old_sch->lock);
922 		old_enabled = old_sch->schib.pmcw.ena;
923 		rc = 0;
924 		if (old_enabled)
925 			rc = cio_disable_subchannel(old_sch);
926 		spin_unlock_irq(old_sch->lock);
927 		if (rc == -EBUSY) {
928 			/* Release child reference for new parent. */
929 			put_device(&sch->dev);
930 			return rc;
931 		}
932 	}
933 
934 	mutex_lock(&sch->reg_mutex);
935 	rc = device_move(&cdev->dev, &sch->dev, DPM_ORDER_PARENT_BEFORE_DEV);
936 	mutex_unlock(&sch->reg_mutex);
937 	if (rc) {
938 		CIO_MSG_EVENT(0, "device_move(0.%x.%04x,0.%x.%04x)=%d\n",
939 			      cdev->private->dev_id.ssid,
940 			      cdev->private->dev_id.devno, sch->schid.ssid,
941 			      sch->schib.pmcw.dev, rc);
942 		if (old_enabled) {
943 			/* Try to reenable the old subchannel. */
944 			spin_lock_irq(old_sch->lock);
945 			cio_enable_subchannel(old_sch, (u32)(addr_t)old_sch);
946 			spin_unlock_irq(old_sch->lock);
947 		}
948 		/* Release child reference for new parent. */
949 		put_device(&sch->dev);
950 		return rc;
951 	}
952 	/* Clean up old subchannel. */
953 	if (!sch_is_pseudo_sch(old_sch)) {
954 		spin_lock_irq(old_sch->lock);
955 		sch_set_cdev(old_sch, NULL);
956 		spin_unlock_irq(old_sch->lock);
957 		css_schedule_eval(old_sch->schid);
958 	}
959 	/* Release child reference for old parent. */
960 	put_device(&old_sch->dev);
961 	/* Initialize new subchannel. */
962 	spin_lock_irq(sch->lock);
963 	cdev->ccwlock = sch->lock;
964 	if (!sch_is_pseudo_sch(sch))
965 		sch_set_cdev(sch, cdev);
966 	spin_unlock_irq(sch->lock);
967 	if (!sch_is_pseudo_sch(sch))
968 		css_update_ssd_info(sch);
969 	return 0;
970 }
971 
ccw_device_move_to_orph(struct ccw_device * cdev)972 static int ccw_device_move_to_orph(struct ccw_device *cdev)
973 {
974 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
975 	struct channel_subsystem *css = to_css(sch->dev.parent);
976 
977 	return ccw_device_move_to_sch(cdev, css->pseudo_subchannel);
978 }
979 
io_subchannel_irq(struct subchannel * sch)980 static void io_subchannel_irq(struct subchannel *sch)
981 {
982 	struct ccw_device *cdev;
983 
984 	cdev = sch_get_cdev(sch);
985 
986 	CIO_TRACE_EVENT(6, "IRQ");
987 	CIO_TRACE_EVENT(6, dev_name(&sch->dev));
988 	if (cdev)
989 		dev_fsm_event(cdev, DEV_EVENT_INTERRUPT);
990 	else
991 		inc_irq_stat(IRQIO_CIO);
992 }
993 
io_subchannel_init_config(struct subchannel * sch)994 void io_subchannel_init_config(struct subchannel *sch)
995 {
996 	memset(&sch->config, 0, sizeof(sch->config));
997 	sch->config.csense = 1;
998 }
999 
io_subchannel_init_fields(struct subchannel * sch)1000 static void io_subchannel_init_fields(struct subchannel *sch)
1001 {
1002 	if (cio_is_console(sch->schid))
1003 		sch->opm = 0xff;
1004 	else
1005 		sch->opm = chp_get_sch_opm(sch);
1006 	sch->lpm = sch->schib.pmcw.pam & sch->opm;
1007 	sch->isc = cio_is_console(sch->schid) ? CONSOLE_ISC : IO_SCH_ISC;
1008 
1009 	CIO_MSG_EVENT(6, "Detected device %04x on subchannel 0.%x.%04X"
1010 		      " - PIM = %02X, PAM = %02X, POM = %02X\n",
1011 		      sch->schib.pmcw.dev, sch->schid.ssid,
1012 		      sch->schid.sch_no, sch->schib.pmcw.pim,
1013 		      sch->schib.pmcw.pam, sch->schib.pmcw.pom);
1014 
1015 	io_subchannel_init_config(sch);
1016 }
1017 
1018 /*
1019  * Note: We always return 0 so that we bind to the device even on error.
1020  * This is needed so that our remove function is called on unregister.
1021  */
io_subchannel_probe(struct subchannel * sch)1022 static int io_subchannel_probe(struct subchannel *sch)
1023 {
1024 	struct io_subchannel_private *io_priv;
1025 	struct ccw_device *cdev;
1026 	int rc;
1027 
1028 	if (cio_is_console(sch->schid)) {
1029 		rc = sysfs_create_group(&sch->dev.kobj,
1030 					&io_subchannel_attr_group);
1031 		if (rc)
1032 			CIO_MSG_EVENT(0, "Failed to create io subchannel "
1033 				      "attributes for subchannel "
1034 				      "0.%x.%04x (rc=%d)\n",
1035 				      sch->schid.ssid, sch->schid.sch_no, rc);
1036 		/*
1037 		 * The console subchannel already has an associated ccw_device.
1038 		 * Throw the delayed uevent for the subchannel, register
1039 		 * the ccw_device and exit.
1040 		 */
1041 		if (dev_get_uevent_suppress(&sch->dev)) {
1042 			/* should always be the case for the console */
1043 			dev_set_uevent_suppress(&sch->dev, 0);
1044 			kobject_uevent(&sch->dev.kobj, KOBJ_ADD);
1045 		}
1046 		cdev = sch_get_cdev(sch);
1047 		rc = ccw_device_add(cdev);
1048 		if (rc) {
1049 			/* Release online reference. */
1050 			put_device(&cdev->dev);
1051 			goto out_schedule;
1052 		}
1053 		if (atomic_dec_and_test(&ccw_device_init_count))
1054 			wake_up(&ccw_device_init_wq);
1055 		return 0;
1056 	}
1057 	io_subchannel_init_fields(sch);
1058 	rc = cio_commit_config(sch);
1059 	if (rc)
1060 		goto out_schedule;
1061 	rc = sysfs_create_group(&sch->dev.kobj,
1062 				&io_subchannel_attr_group);
1063 	if (rc)
1064 		goto out_schedule;
1065 	/* Allocate I/O subchannel private data. */
1066 	io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1067 	if (!io_priv)
1068 		goto out_schedule;
1069 
1070 	set_io_private(sch, io_priv);
1071 	css_schedule_eval(sch->schid);
1072 	return 0;
1073 
1074 out_schedule:
1075 	spin_lock_irq(sch->lock);
1076 	css_sched_sch_todo(sch, SCH_TODO_UNREG);
1077 	spin_unlock_irq(sch->lock);
1078 	return 0;
1079 }
1080 
io_subchannel_remove(struct subchannel * sch)1081 static int io_subchannel_remove(struct subchannel *sch)
1082 {
1083 	struct io_subchannel_private *io_priv = to_io_private(sch);
1084 	struct ccw_device *cdev;
1085 
1086 	cdev = sch_get_cdev(sch);
1087 	if (!cdev)
1088 		goto out_free;
1089 
1090 	ccw_device_unregister(cdev);
1091 	spin_lock_irq(sch->lock);
1092 	sch_set_cdev(sch, NULL);
1093 	set_io_private(sch, NULL);
1094 	spin_unlock_irq(sch->lock);
1095 out_free:
1096 	kfree(io_priv);
1097 	sysfs_remove_group(&sch->dev.kobj, &io_subchannel_attr_group);
1098 	return 0;
1099 }
1100 
io_subchannel_verify(struct subchannel * sch)1101 static void io_subchannel_verify(struct subchannel *sch)
1102 {
1103 	struct ccw_device *cdev;
1104 
1105 	cdev = sch_get_cdev(sch);
1106 	if (cdev)
1107 		dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1108 }
1109 
io_subchannel_terminate_path(struct subchannel * sch,u8 mask)1110 static void io_subchannel_terminate_path(struct subchannel *sch, u8 mask)
1111 {
1112 	struct ccw_device *cdev;
1113 
1114 	cdev = sch_get_cdev(sch);
1115 	if (!cdev)
1116 		return;
1117 	if (cio_update_schib(sch))
1118 		goto err;
1119 	/* Check for I/O on path. */
1120 	if (scsw_actl(&sch->schib.scsw) == 0 || sch->schib.pmcw.lpum != mask)
1121 		goto out;
1122 	if (cdev->private->state == DEV_STATE_ONLINE) {
1123 		ccw_device_kill_io(cdev);
1124 		goto out;
1125 	}
1126 	if (cio_clear(sch))
1127 		goto err;
1128 out:
1129 	/* Trigger path verification. */
1130 	dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1131 	return;
1132 
1133 err:
1134 	dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1135 }
1136 
io_subchannel_chp_event(struct subchannel * sch,struct chp_link * link,int event)1137 static int io_subchannel_chp_event(struct subchannel *sch,
1138 				   struct chp_link *link, int event)
1139 {
1140 	struct ccw_device *cdev = sch_get_cdev(sch);
1141 	int mask;
1142 
1143 	mask = chp_ssd_get_mask(&sch->ssd_info, link);
1144 	if (!mask)
1145 		return 0;
1146 	switch (event) {
1147 	case CHP_VARY_OFF:
1148 		sch->opm &= ~mask;
1149 		sch->lpm &= ~mask;
1150 		if (cdev)
1151 			cdev->private->path_gone_mask |= mask;
1152 		io_subchannel_terminate_path(sch, mask);
1153 		break;
1154 	case CHP_VARY_ON:
1155 		sch->opm |= mask;
1156 		sch->lpm |= mask;
1157 		if (cdev)
1158 			cdev->private->path_new_mask |= mask;
1159 		io_subchannel_verify(sch);
1160 		break;
1161 	case CHP_OFFLINE:
1162 		if (cio_update_schib(sch))
1163 			return -ENODEV;
1164 		if (cdev)
1165 			cdev->private->path_gone_mask |= mask;
1166 		io_subchannel_terminate_path(sch, mask);
1167 		break;
1168 	case CHP_ONLINE:
1169 		if (cio_update_schib(sch))
1170 			return -ENODEV;
1171 		sch->lpm |= mask & sch->opm;
1172 		if (cdev)
1173 			cdev->private->path_new_mask |= mask;
1174 		io_subchannel_verify(sch);
1175 		break;
1176 	}
1177 	return 0;
1178 }
1179 
io_subchannel_quiesce(struct subchannel * sch)1180 static void io_subchannel_quiesce(struct subchannel *sch)
1181 {
1182 	struct ccw_device *cdev;
1183 	int ret;
1184 
1185 	spin_lock_irq(sch->lock);
1186 	cdev = sch_get_cdev(sch);
1187 	if (cio_is_console(sch->schid))
1188 		goto out_unlock;
1189 	if (!sch->schib.pmcw.ena)
1190 		goto out_unlock;
1191 	ret = cio_disable_subchannel(sch);
1192 	if (ret != -EBUSY)
1193 		goto out_unlock;
1194 	if (cdev->handler)
1195 		cdev->handler(cdev, cdev->private->intparm, ERR_PTR(-EIO));
1196 	while (ret == -EBUSY) {
1197 		cdev->private->state = DEV_STATE_QUIESCE;
1198 		cdev->private->iretry = 255;
1199 		ret = ccw_device_cancel_halt_clear(cdev);
1200 		if (ret == -EBUSY) {
1201 			ccw_device_set_timeout(cdev, HZ/10);
1202 			spin_unlock_irq(sch->lock);
1203 			wait_event(cdev->private->wait_q,
1204 				   cdev->private->state != DEV_STATE_QUIESCE);
1205 			spin_lock_irq(sch->lock);
1206 		}
1207 		ret = cio_disable_subchannel(sch);
1208 	}
1209 out_unlock:
1210 	spin_unlock_irq(sch->lock);
1211 }
1212 
io_subchannel_shutdown(struct subchannel * sch)1213 static void io_subchannel_shutdown(struct subchannel *sch)
1214 {
1215 	io_subchannel_quiesce(sch);
1216 }
1217 
device_is_disconnected(struct ccw_device * cdev)1218 static int device_is_disconnected(struct ccw_device *cdev)
1219 {
1220 	if (!cdev)
1221 		return 0;
1222 	return (cdev->private->state == DEV_STATE_DISCONNECTED ||
1223 		cdev->private->state == DEV_STATE_DISCONNECTED_SENSE_ID);
1224 }
1225 
recovery_check(struct device * dev,void * data)1226 static int recovery_check(struct device *dev, void *data)
1227 {
1228 	struct ccw_device *cdev = to_ccwdev(dev);
1229 	struct subchannel *sch;
1230 	int *redo = data;
1231 
1232 	spin_lock_irq(cdev->ccwlock);
1233 	switch (cdev->private->state) {
1234 	case DEV_STATE_ONLINE:
1235 		sch = to_subchannel(cdev->dev.parent);
1236 		if ((sch->schib.pmcw.pam & sch->opm) == sch->vpm)
1237 			break;
1238 		/* fall through */
1239 	case DEV_STATE_DISCONNECTED:
1240 		CIO_MSG_EVENT(3, "recovery: trigger 0.%x.%04x\n",
1241 			      cdev->private->dev_id.ssid,
1242 			      cdev->private->dev_id.devno);
1243 		dev_fsm_event(cdev, DEV_EVENT_VERIFY);
1244 		*redo = 1;
1245 		break;
1246 	case DEV_STATE_DISCONNECTED_SENSE_ID:
1247 		*redo = 1;
1248 		break;
1249 	}
1250 	spin_unlock_irq(cdev->ccwlock);
1251 
1252 	return 0;
1253 }
1254 
recovery_work_func(struct work_struct * unused)1255 static void recovery_work_func(struct work_struct *unused)
1256 {
1257 	int redo = 0;
1258 
1259 	bus_for_each_dev(&ccw_bus_type, NULL, &redo, recovery_check);
1260 	if (redo) {
1261 		spin_lock_irq(&recovery_lock);
1262 		if (!timer_pending(&recovery_timer)) {
1263 			if (recovery_phase < ARRAY_SIZE(recovery_delay) - 1)
1264 				recovery_phase++;
1265 			mod_timer(&recovery_timer, jiffies +
1266 				  recovery_delay[recovery_phase] * HZ);
1267 		}
1268 		spin_unlock_irq(&recovery_lock);
1269 	} else
1270 		CIO_MSG_EVENT(3, "recovery: end\n");
1271 }
1272 
1273 static DECLARE_WORK(recovery_work, recovery_work_func);
1274 
recovery_func(struct timer_list * unused)1275 static void recovery_func(struct timer_list *unused)
1276 {
1277 	/*
1278 	 * We can't do our recovery in softirq context and it's not
1279 	 * performance critical, so we schedule it.
1280 	 */
1281 	schedule_work(&recovery_work);
1282 }
1283 
ccw_device_schedule_recovery(void)1284 void ccw_device_schedule_recovery(void)
1285 {
1286 	unsigned long flags;
1287 
1288 	CIO_MSG_EVENT(3, "recovery: schedule\n");
1289 	spin_lock_irqsave(&recovery_lock, flags);
1290 	if (!timer_pending(&recovery_timer) || (recovery_phase != 0)) {
1291 		recovery_phase = 0;
1292 		mod_timer(&recovery_timer, jiffies + recovery_delay[0] * HZ);
1293 	}
1294 	spin_unlock_irqrestore(&recovery_lock, flags);
1295 }
1296 
purge_fn(struct device * dev,void * data)1297 static int purge_fn(struct device *dev, void *data)
1298 {
1299 	struct ccw_device *cdev = to_ccwdev(dev);
1300 	struct ccw_dev_id *id = &cdev->private->dev_id;
1301 
1302 	spin_lock_irq(cdev->ccwlock);
1303 	if (is_blacklisted(id->ssid, id->devno) &&
1304 	    (cdev->private->state == DEV_STATE_OFFLINE) &&
1305 	    (atomic_cmpxchg(&cdev->private->onoff, 0, 1) == 0)) {
1306 		CIO_MSG_EVENT(3, "ccw: purging 0.%x.%04x\n", id->ssid,
1307 			      id->devno);
1308 		ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1309 		atomic_set(&cdev->private->onoff, 0);
1310 	}
1311 	spin_unlock_irq(cdev->ccwlock);
1312 	/* Abort loop in case of pending signal. */
1313 	if (signal_pending(current))
1314 		return -EINTR;
1315 
1316 	return 0;
1317 }
1318 
1319 /**
1320  * ccw_purge_blacklisted - purge unused, blacklisted devices
1321  *
1322  * Unregister all ccw devices that are offline and on the blacklist.
1323  */
ccw_purge_blacklisted(void)1324 int ccw_purge_blacklisted(void)
1325 {
1326 	CIO_MSG_EVENT(2, "ccw: purging blacklisted devices\n");
1327 	bus_for_each_dev(&ccw_bus_type, NULL, NULL, purge_fn);
1328 	return 0;
1329 }
1330 
ccw_device_set_disconnected(struct ccw_device * cdev)1331 void ccw_device_set_disconnected(struct ccw_device *cdev)
1332 {
1333 	if (!cdev)
1334 		return;
1335 	ccw_device_set_timeout(cdev, 0);
1336 	cdev->private->flags.fake_irb = 0;
1337 	cdev->private->state = DEV_STATE_DISCONNECTED;
1338 	if (cdev->online)
1339 		ccw_device_schedule_recovery();
1340 }
1341 
ccw_device_set_notoper(struct ccw_device * cdev)1342 void ccw_device_set_notoper(struct ccw_device *cdev)
1343 {
1344 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1345 
1346 	CIO_TRACE_EVENT(2, "notoper");
1347 	CIO_TRACE_EVENT(2, dev_name(&sch->dev));
1348 	ccw_device_set_timeout(cdev, 0);
1349 	cio_disable_subchannel(sch);
1350 	cdev->private->state = DEV_STATE_NOT_OPER;
1351 }
1352 
1353 enum io_sch_action {
1354 	IO_SCH_UNREG,
1355 	IO_SCH_ORPH_UNREG,
1356 	IO_SCH_UNREG_CDEV,
1357 	IO_SCH_ATTACH,
1358 	IO_SCH_UNREG_ATTACH,
1359 	IO_SCH_ORPH_ATTACH,
1360 	IO_SCH_REPROBE,
1361 	IO_SCH_VERIFY,
1362 	IO_SCH_DISC,
1363 	IO_SCH_NOP,
1364 };
1365 
sch_get_action(struct subchannel * sch)1366 static enum io_sch_action sch_get_action(struct subchannel *sch)
1367 {
1368 	struct ccw_device *cdev;
1369 
1370 	cdev = sch_get_cdev(sch);
1371 	if (cio_update_schib(sch)) {
1372 		/* Not operational. */
1373 		if (!cdev)
1374 			return IO_SCH_UNREG;
1375 		if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1376 			return IO_SCH_UNREG;
1377 		return IO_SCH_ORPH_UNREG;
1378 	}
1379 	/* Operational. */
1380 	if (!cdev)
1381 		return IO_SCH_ATTACH;
1382 	if (sch->schib.pmcw.dev != cdev->private->dev_id.devno) {
1383 		if (ccw_device_notify(cdev, CIO_GONE) != NOTIFY_OK)
1384 			return IO_SCH_UNREG_ATTACH;
1385 		return IO_SCH_ORPH_ATTACH;
1386 	}
1387 	if ((sch->schib.pmcw.pam & sch->opm) == 0) {
1388 		if (ccw_device_notify(cdev, CIO_NO_PATH) != NOTIFY_OK)
1389 			return IO_SCH_UNREG_CDEV;
1390 		return IO_SCH_DISC;
1391 	}
1392 	if (device_is_disconnected(cdev))
1393 		return IO_SCH_REPROBE;
1394 	if (cdev->online && !cdev->private->flags.resuming)
1395 		return IO_SCH_VERIFY;
1396 	if (cdev->private->state == DEV_STATE_NOT_OPER)
1397 		return IO_SCH_UNREG_ATTACH;
1398 	return IO_SCH_NOP;
1399 }
1400 
1401 /**
1402  * io_subchannel_sch_event - process subchannel event
1403  * @sch: subchannel
1404  * @process: non-zero if function is called in process context
1405  *
1406  * An unspecified event occurred for this subchannel. Adjust data according
1407  * to the current operational state of the subchannel and device. Return
1408  * zero when the event has been handled sufficiently or -EAGAIN when this
1409  * function should be called again in process context.
1410  */
io_subchannel_sch_event(struct subchannel * sch,int process)1411 static int io_subchannel_sch_event(struct subchannel *sch, int process)
1412 {
1413 	unsigned long flags;
1414 	struct ccw_device *cdev;
1415 	struct ccw_dev_id dev_id;
1416 	enum io_sch_action action;
1417 	int rc = -EAGAIN;
1418 
1419 	spin_lock_irqsave(sch->lock, flags);
1420 	if (!device_is_registered(&sch->dev))
1421 		goto out_unlock;
1422 	if (work_pending(&sch->todo_work))
1423 		goto out_unlock;
1424 	cdev = sch_get_cdev(sch);
1425 	if (cdev && work_pending(&cdev->private->todo_work))
1426 		goto out_unlock;
1427 	action = sch_get_action(sch);
1428 	CIO_MSG_EVENT(2, "event: sch 0.%x.%04x, process=%d, action=%d\n",
1429 		      sch->schid.ssid, sch->schid.sch_no, process,
1430 		      action);
1431 	/* Perform immediate actions while holding the lock. */
1432 	switch (action) {
1433 	case IO_SCH_REPROBE:
1434 		/* Trigger device recognition. */
1435 		ccw_device_trigger_reprobe(cdev);
1436 		rc = 0;
1437 		goto out_unlock;
1438 	case IO_SCH_VERIFY:
1439 		/* Trigger path verification. */
1440 		io_subchannel_verify(sch);
1441 		rc = 0;
1442 		goto out_unlock;
1443 	case IO_SCH_DISC:
1444 		ccw_device_set_disconnected(cdev);
1445 		rc = 0;
1446 		goto out_unlock;
1447 	case IO_SCH_ORPH_UNREG:
1448 	case IO_SCH_ORPH_ATTACH:
1449 		ccw_device_set_disconnected(cdev);
1450 		break;
1451 	case IO_SCH_UNREG_CDEV:
1452 	case IO_SCH_UNREG_ATTACH:
1453 	case IO_SCH_UNREG:
1454 		if (!cdev)
1455 			break;
1456 		if (cdev->private->state == DEV_STATE_SENSE_ID) {
1457 			/*
1458 			 * Note: delayed work triggered by this event
1459 			 * and repeated calls to sch_event are synchronized
1460 			 * by the above check for work_pending(cdev).
1461 			 */
1462 			dev_fsm_event(cdev, DEV_EVENT_NOTOPER);
1463 		} else
1464 			ccw_device_set_notoper(cdev);
1465 		break;
1466 	case IO_SCH_NOP:
1467 		rc = 0;
1468 		goto out_unlock;
1469 	default:
1470 		break;
1471 	}
1472 	spin_unlock_irqrestore(sch->lock, flags);
1473 	/* All other actions require process context. */
1474 	if (!process)
1475 		goto out;
1476 	/* Handle attached ccw device. */
1477 	switch (action) {
1478 	case IO_SCH_ORPH_UNREG:
1479 	case IO_SCH_ORPH_ATTACH:
1480 		/* Move ccw device to orphanage. */
1481 		rc = ccw_device_move_to_orph(cdev);
1482 		if (rc)
1483 			goto out;
1484 		break;
1485 	case IO_SCH_UNREG_CDEV:
1486 	case IO_SCH_UNREG_ATTACH:
1487 		spin_lock_irqsave(sch->lock, flags);
1488 		if (cdev->private->flags.resuming) {
1489 			/* Device will be handled later. */
1490 			rc = 0;
1491 			goto out_unlock;
1492 		}
1493 		sch_set_cdev(sch, NULL);
1494 		spin_unlock_irqrestore(sch->lock, flags);
1495 		/* Unregister ccw device. */
1496 		ccw_device_unregister(cdev);
1497 		break;
1498 	default:
1499 		break;
1500 	}
1501 	/* Handle subchannel. */
1502 	switch (action) {
1503 	case IO_SCH_ORPH_UNREG:
1504 	case IO_SCH_UNREG:
1505 		if (!cdev || !cdev->private->flags.resuming)
1506 			css_sch_device_unregister(sch);
1507 		break;
1508 	case IO_SCH_ORPH_ATTACH:
1509 	case IO_SCH_UNREG_ATTACH:
1510 	case IO_SCH_ATTACH:
1511 		dev_id.ssid = sch->schid.ssid;
1512 		dev_id.devno = sch->schib.pmcw.dev;
1513 		cdev = get_ccwdev_by_dev_id(&dev_id);
1514 		if (!cdev) {
1515 			sch_create_and_recog_new_device(sch);
1516 			break;
1517 		}
1518 		rc = ccw_device_move_to_sch(cdev, sch);
1519 		if (rc) {
1520 			/* Release reference from get_ccwdev_by_dev_id() */
1521 			put_device(&cdev->dev);
1522 			goto out;
1523 		}
1524 		spin_lock_irqsave(sch->lock, flags);
1525 		ccw_device_trigger_reprobe(cdev);
1526 		spin_unlock_irqrestore(sch->lock, flags);
1527 		/* Release reference from get_ccwdev_by_dev_id() */
1528 		put_device(&cdev->dev);
1529 		break;
1530 	default:
1531 		break;
1532 	}
1533 	return 0;
1534 
1535 out_unlock:
1536 	spin_unlock_irqrestore(sch->lock, flags);
1537 out:
1538 	return rc;
1539 }
1540 
ccw_device_set_int_class(struct ccw_device * cdev)1541 static void ccw_device_set_int_class(struct ccw_device *cdev)
1542 {
1543 	struct ccw_driver *cdrv = cdev->drv;
1544 
1545 	/* Note: we interpret class 0 in this context as an uninitialized
1546 	 * field since it translates to a non-I/O interrupt class. */
1547 	if (cdrv->int_class != 0)
1548 		cdev->private->int_class = cdrv->int_class;
1549 	else
1550 		cdev->private->int_class = IRQIO_CIO;
1551 }
1552 
1553 #ifdef CONFIG_CCW_CONSOLE
ccw_device_enable_console(struct ccw_device * cdev)1554 int __init ccw_device_enable_console(struct ccw_device *cdev)
1555 {
1556 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1557 	int rc;
1558 
1559 	if (!cdev->drv || !cdev->handler)
1560 		return -EINVAL;
1561 
1562 	io_subchannel_init_fields(sch);
1563 	rc = cio_commit_config(sch);
1564 	if (rc)
1565 		return rc;
1566 	sch->driver = &io_subchannel_driver;
1567 	io_subchannel_recog(cdev, sch);
1568 	/* Now wait for the async. recognition to come to an end. */
1569 	spin_lock_irq(cdev->ccwlock);
1570 	while (!dev_fsm_final_state(cdev))
1571 		ccw_device_wait_idle(cdev);
1572 
1573 	/* Hold on to an extra reference while device is online. */
1574 	get_device(&cdev->dev);
1575 	rc = ccw_device_online(cdev);
1576 	if (rc)
1577 		goto out_unlock;
1578 
1579 	while (!dev_fsm_final_state(cdev))
1580 		ccw_device_wait_idle(cdev);
1581 
1582 	if (cdev->private->state == DEV_STATE_ONLINE)
1583 		cdev->online = 1;
1584 	else
1585 		rc = -EIO;
1586 out_unlock:
1587 	spin_unlock_irq(cdev->ccwlock);
1588 	if (rc) /* Give up online reference since onlining failed. */
1589 		put_device(&cdev->dev);
1590 	return rc;
1591 }
1592 
ccw_device_create_console(struct ccw_driver * drv)1593 struct ccw_device * __init ccw_device_create_console(struct ccw_driver *drv)
1594 {
1595 	struct io_subchannel_private *io_priv;
1596 	struct ccw_device *cdev;
1597 	struct subchannel *sch;
1598 
1599 	sch = cio_probe_console();
1600 	if (IS_ERR(sch))
1601 		return ERR_CAST(sch);
1602 
1603 	io_priv = kzalloc(sizeof(*io_priv), GFP_KERNEL | GFP_DMA);
1604 	if (!io_priv) {
1605 		put_device(&sch->dev);
1606 		return ERR_PTR(-ENOMEM);
1607 	}
1608 	set_io_private(sch, io_priv);
1609 	cdev = io_subchannel_create_ccwdev(sch);
1610 	if (IS_ERR(cdev)) {
1611 		put_device(&sch->dev);
1612 		kfree(io_priv);
1613 		return cdev;
1614 	}
1615 	cdev->drv = drv;
1616 	ccw_device_set_int_class(cdev);
1617 	return cdev;
1618 }
1619 
ccw_device_destroy_console(struct ccw_device * cdev)1620 void __init ccw_device_destroy_console(struct ccw_device *cdev)
1621 {
1622 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1623 	struct io_subchannel_private *io_priv = to_io_private(sch);
1624 
1625 	set_io_private(sch, NULL);
1626 	put_device(&sch->dev);
1627 	put_device(&cdev->dev);
1628 	kfree(io_priv);
1629 }
1630 
1631 /**
1632  * ccw_device_wait_idle() - busy wait for device to become idle
1633  * @cdev: ccw device
1634  *
1635  * Poll until activity control is zero, that is, no function or data
1636  * transfer is pending/active.
1637  * Called with device lock being held.
1638  */
ccw_device_wait_idle(struct ccw_device * cdev)1639 void ccw_device_wait_idle(struct ccw_device *cdev)
1640 {
1641 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1642 
1643 	while (1) {
1644 		cio_tsch(sch);
1645 		if (sch->schib.scsw.cmd.actl == 0)
1646 			break;
1647 		udelay_simple(100);
1648 	}
1649 }
1650 
1651 static int ccw_device_pm_restore(struct device *dev);
1652 
ccw_device_force_console(struct ccw_device * cdev)1653 int ccw_device_force_console(struct ccw_device *cdev)
1654 {
1655 	return ccw_device_pm_restore(&cdev->dev);
1656 }
1657 EXPORT_SYMBOL_GPL(ccw_device_force_console);
1658 #endif
1659 
1660 /*
1661  * get ccw_device matching the busid, but only if owned by cdrv
1662  */
1663 static int
__ccwdev_check_busid(struct device * dev,void * id)1664 __ccwdev_check_busid(struct device *dev, void *id)
1665 {
1666 	char *bus_id;
1667 
1668 	bus_id = id;
1669 
1670 	return (strcmp(bus_id, dev_name(dev)) == 0);
1671 }
1672 
1673 
1674 /**
1675  * get_ccwdev_by_busid() - obtain device from a bus id
1676  * @cdrv: driver the device is owned by
1677  * @bus_id: bus id of the device to be searched
1678  *
1679  * This function searches all devices owned by @cdrv for a device with a bus
1680  * id matching @bus_id.
1681  * Returns:
1682  *  If a match is found, its reference count of the found device is increased
1683  *  and it is returned; else %NULL is returned.
1684  */
get_ccwdev_by_busid(struct ccw_driver * cdrv,const char * bus_id)1685 struct ccw_device *get_ccwdev_by_busid(struct ccw_driver *cdrv,
1686 				       const char *bus_id)
1687 {
1688 	struct device *dev;
1689 
1690 	dev = driver_find_device(&cdrv->driver, NULL, (void *)bus_id,
1691 				 __ccwdev_check_busid);
1692 
1693 	return dev ? to_ccwdev(dev) : NULL;
1694 }
1695 
1696 /************************** device driver handling ************************/
1697 
1698 /* This is the implementation of the ccw_driver class. The probe, remove
1699  * and release methods are initially very similar to the device_driver
1700  * implementations, with the difference that they have ccw_device
1701  * arguments.
1702  *
1703  * A ccw driver also contains the information that is needed for
1704  * device matching.
1705  */
1706 static int
ccw_device_probe(struct device * dev)1707 ccw_device_probe (struct device *dev)
1708 {
1709 	struct ccw_device *cdev = to_ccwdev(dev);
1710 	struct ccw_driver *cdrv = to_ccwdrv(dev->driver);
1711 	int ret;
1712 
1713 	cdev->drv = cdrv; /* to let the driver call _set_online */
1714 	ccw_device_set_int_class(cdev);
1715 	ret = cdrv->probe ? cdrv->probe(cdev) : -ENODEV;
1716 	if (ret) {
1717 		cdev->drv = NULL;
1718 		cdev->private->int_class = IRQIO_CIO;
1719 		return ret;
1720 	}
1721 
1722 	return 0;
1723 }
1724 
ccw_device_remove(struct device * dev)1725 static int ccw_device_remove(struct device *dev)
1726 {
1727 	struct ccw_device *cdev = to_ccwdev(dev);
1728 	struct ccw_driver *cdrv = cdev->drv;
1729 	struct subchannel *sch;
1730 	int ret;
1731 
1732 	if (cdrv->remove)
1733 		cdrv->remove(cdev);
1734 
1735 	spin_lock_irq(cdev->ccwlock);
1736 	if (cdev->online) {
1737 		cdev->online = 0;
1738 		ret = ccw_device_offline(cdev);
1739 		spin_unlock_irq(cdev->ccwlock);
1740 		if (ret == 0)
1741 			wait_event(cdev->private->wait_q,
1742 				   dev_fsm_final_state(cdev));
1743 		else
1744 			CIO_MSG_EVENT(0, "ccw_device_offline returned %d, "
1745 				      "device 0.%x.%04x\n",
1746 				      ret, cdev->private->dev_id.ssid,
1747 				      cdev->private->dev_id.devno);
1748 		/* Give up reference obtained in ccw_device_set_online(). */
1749 		put_device(&cdev->dev);
1750 		spin_lock_irq(cdev->ccwlock);
1751 	}
1752 	ccw_device_set_timeout(cdev, 0);
1753 	cdev->drv = NULL;
1754 	cdev->private->int_class = IRQIO_CIO;
1755 	sch = to_subchannel(cdev->dev.parent);
1756 	spin_unlock_irq(cdev->ccwlock);
1757 	io_subchannel_quiesce(sch);
1758 	__disable_cmf(cdev);
1759 
1760 	return 0;
1761 }
1762 
ccw_device_shutdown(struct device * dev)1763 static void ccw_device_shutdown(struct device *dev)
1764 {
1765 	struct ccw_device *cdev;
1766 
1767 	cdev = to_ccwdev(dev);
1768 	if (cdev->drv && cdev->drv->shutdown)
1769 		cdev->drv->shutdown(cdev);
1770 	__disable_cmf(cdev);
1771 }
1772 
ccw_device_pm_prepare(struct device * dev)1773 static int ccw_device_pm_prepare(struct device *dev)
1774 {
1775 	struct ccw_device *cdev = to_ccwdev(dev);
1776 
1777 	if (work_pending(&cdev->private->todo_work))
1778 		return -EAGAIN;
1779 	/* Fail while device is being set online/offline. */
1780 	if (atomic_read(&cdev->private->onoff))
1781 		return -EAGAIN;
1782 
1783 	if (cdev->online && cdev->drv && cdev->drv->prepare)
1784 		return cdev->drv->prepare(cdev);
1785 
1786 	return 0;
1787 }
1788 
ccw_device_pm_complete(struct device * dev)1789 static void ccw_device_pm_complete(struct device *dev)
1790 {
1791 	struct ccw_device *cdev = to_ccwdev(dev);
1792 
1793 	if (cdev->online && cdev->drv && cdev->drv->complete)
1794 		cdev->drv->complete(cdev);
1795 }
1796 
ccw_device_pm_freeze(struct device * dev)1797 static int ccw_device_pm_freeze(struct device *dev)
1798 {
1799 	struct ccw_device *cdev = to_ccwdev(dev);
1800 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1801 	int ret, cm_enabled;
1802 
1803 	/* Fail suspend while device is in transistional state. */
1804 	if (!dev_fsm_final_state(cdev))
1805 		return -EAGAIN;
1806 	if (!cdev->online)
1807 		return 0;
1808 	if (cdev->drv && cdev->drv->freeze) {
1809 		ret = cdev->drv->freeze(cdev);
1810 		if (ret)
1811 			return ret;
1812 	}
1813 
1814 	spin_lock_irq(sch->lock);
1815 	cm_enabled = cdev->private->cmb != NULL;
1816 	spin_unlock_irq(sch->lock);
1817 	if (cm_enabled) {
1818 		/* Don't have the css write on memory. */
1819 		ret = ccw_set_cmf(cdev, 0);
1820 		if (ret)
1821 			return ret;
1822 	}
1823 	/* From here on, disallow device driver I/O. */
1824 	spin_lock_irq(sch->lock);
1825 	ret = cio_disable_subchannel(sch);
1826 	spin_unlock_irq(sch->lock);
1827 
1828 	return ret;
1829 }
1830 
ccw_device_pm_thaw(struct device * dev)1831 static int ccw_device_pm_thaw(struct device *dev)
1832 {
1833 	struct ccw_device *cdev = to_ccwdev(dev);
1834 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1835 	int ret, cm_enabled;
1836 
1837 	if (!cdev->online)
1838 		return 0;
1839 
1840 	spin_lock_irq(sch->lock);
1841 	/* Allow device driver I/O again. */
1842 	ret = cio_enable_subchannel(sch, (u32)(addr_t)sch);
1843 	cm_enabled = cdev->private->cmb != NULL;
1844 	spin_unlock_irq(sch->lock);
1845 	if (ret)
1846 		return ret;
1847 
1848 	if (cm_enabled) {
1849 		ret = ccw_set_cmf(cdev, 1);
1850 		if (ret)
1851 			return ret;
1852 	}
1853 
1854 	if (cdev->drv && cdev->drv->thaw)
1855 		ret = cdev->drv->thaw(cdev);
1856 
1857 	return ret;
1858 }
1859 
__ccw_device_pm_restore(struct ccw_device * cdev)1860 static void __ccw_device_pm_restore(struct ccw_device *cdev)
1861 {
1862 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
1863 
1864 	spin_lock_irq(sch->lock);
1865 	if (cio_is_console(sch->schid)) {
1866 		cio_enable_subchannel(sch, (u32)(addr_t)sch);
1867 		goto out_unlock;
1868 	}
1869 	/*
1870 	 * While we were sleeping, devices may have gone or become
1871 	 * available again. Kick re-detection.
1872 	 */
1873 	cdev->private->flags.resuming = 1;
1874 	cdev->private->path_new_mask = LPM_ANYPATH;
1875 	css_sched_sch_todo(sch, SCH_TODO_EVAL);
1876 	spin_unlock_irq(sch->lock);
1877 	css_wait_for_slow_path();
1878 
1879 	/* cdev may have been moved to a different subchannel. */
1880 	sch = to_subchannel(cdev->dev.parent);
1881 	spin_lock_irq(sch->lock);
1882 	if (cdev->private->state != DEV_STATE_ONLINE &&
1883 	    cdev->private->state != DEV_STATE_OFFLINE)
1884 		goto out_unlock;
1885 
1886 	ccw_device_recognition(cdev);
1887 	spin_unlock_irq(sch->lock);
1888 	wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev) ||
1889 		   cdev->private->state == DEV_STATE_DISCONNECTED);
1890 	spin_lock_irq(sch->lock);
1891 
1892 out_unlock:
1893 	cdev->private->flags.resuming = 0;
1894 	spin_unlock_irq(sch->lock);
1895 }
1896 
resume_handle_boxed(struct ccw_device * cdev)1897 static int resume_handle_boxed(struct ccw_device *cdev)
1898 {
1899 	cdev->private->state = DEV_STATE_BOXED;
1900 	if (ccw_device_notify(cdev, CIO_BOXED) == NOTIFY_OK)
1901 		return 0;
1902 	ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1903 	return -ENODEV;
1904 }
1905 
resume_handle_disc(struct ccw_device * cdev)1906 static int resume_handle_disc(struct ccw_device *cdev)
1907 {
1908 	cdev->private->state = DEV_STATE_DISCONNECTED;
1909 	if (ccw_device_notify(cdev, CIO_GONE) == NOTIFY_OK)
1910 		return 0;
1911 	ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1912 	return -ENODEV;
1913 }
1914 
ccw_device_pm_restore(struct device * dev)1915 static int ccw_device_pm_restore(struct device *dev)
1916 {
1917 	struct ccw_device *cdev = to_ccwdev(dev);
1918 	struct subchannel *sch;
1919 	int ret = 0;
1920 
1921 	__ccw_device_pm_restore(cdev);
1922 	sch = to_subchannel(cdev->dev.parent);
1923 	spin_lock_irq(sch->lock);
1924 	if (cio_is_console(sch->schid))
1925 		goto out_restore;
1926 
1927 	/* check recognition results */
1928 	switch (cdev->private->state) {
1929 	case DEV_STATE_OFFLINE:
1930 	case DEV_STATE_ONLINE:
1931 		cdev->private->flags.donotify = 0;
1932 		break;
1933 	case DEV_STATE_BOXED:
1934 		ret = resume_handle_boxed(cdev);
1935 		if (ret)
1936 			goto out_unlock;
1937 		goto out_restore;
1938 	default:
1939 		ret = resume_handle_disc(cdev);
1940 		if (ret)
1941 			goto out_unlock;
1942 		goto out_restore;
1943 	}
1944 	/* check if the device type has changed */
1945 	if (!ccw_device_test_sense_data(cdev)) {
1946 		ccw_device_update_sense_data(cdev);
1947 		ccw_device_sched_todo(cdev, CDEV_TODO_REBIND);
1948 		ret = -ENODEV;
1949 		goto out_unlock;
1950 	}
1951 	if (!cdev->online)
1952 		goto out_unlock;
1953 
1954 	if (ccw_device_online(cdev)) {
1955 		ret = resume_handle_disc(cdev);
1956 		if (ret)
1957 			goto out_unlock;
1958 		goto out_restore;
1959 	}
1960 	spin_unlock_irq(sch->lock);
1961 	wait_event(cdev->private->wait_q, dev_fsm_final_state(cdev));
1962 	spin_lock_irq(sch->lock);
1963 
1964 	if (ccw_device_notify(cdev, CIO_OPER) == NOTIFY_BAD) {
1965 		ccw_device_sched_todo(cdev, CDEV_TODO_UNREG);
1966 		ret = -ENODEV;
1967 		goto out_unlock;
1968 	}
1969 
1970 	/* reenable cmf, if needed */
1971 	if (cdev->private->cmb) {
1972 		spin_unlock_irq(sch->lock);
1973 		ret = ccw_set_cmf(cdev, 1);
1974 		spin_lock_irq(sch->lock);
1975 		if (ret) {
1976 			CIO_MSG_EVENT(2, "resume: cdev 0.%x.%04x: cmf failed "
1977 				      "(rc=%d)\n", cdev->private->dev_id.ssid,
1978 				      cdev->private->dev_id.devno, ret);
1979 			ret = 0;
1980 		}
1981 	}
1982 
1983 out_restore:
1984 	spin_unlock_irq(sch->lock);
1985 	if (cdev->online && cdev->drv && cdev->drv->restore)
1986 		ret = cdev->drv->restore(cdev);
1987 	return ret;
1988 
1989 out_unlock:
1990 	spin_unlock_irq(sch->lock);
1991 	return ret;
1992 }
1993 
1994 static const struct dev_pm_ops ccw_pm_ops = {
1995 	.prepare = ccw_device_pm_prepare,
1996 	.complete = ccw_device_pm_complete,
1997 	.freeze = ccw_device_pm_freeze,
1998 	.thaw = ccw_device_pm_thaw,
1999 	.restore = ccw_device_pm_restore,
2000 };
2001 
2002 static struct bus_type ccw_bus_type = {
2003 	.name   = "ccw",
2004 	.match  = ccw_bus_match,
2005 	.uevent = ccw_uevent,
2006 	.probe  = ccw_device_probe,
2007 	.remove = ccw_device_remove,
2008 	.shutdown = ccw_device_shutdown,
2009 	.pm = &ccw_pm_ops,
2010 };
2011 
2012 /**
2013  * ccw_driver_register() - register a ccw driver
2014  * @cdriver: driver to be registered
2015  *
2016  * This function is mainly a wrapper around driver_register().
2017  * Returns:
2018  *   %0 on success and a negative error value on failure.
2019  */
ccw_driver_register(struct ccw_driver * cdriver)2020 int ccw_driver_register(struct ccw_driver *cdriver)
2021 {
2022 	struct device_driver *drv = &cdriver->driver;
2023 
2024 	drv->bus = &ccw_bus_type;
2025 
2026 	return driver_register(drv);
2027 }
2028 
2029 /**
2030  * ccw_driver_unregister() - deregister a ccw driver
2031  * @cdriver: driver to be deregistered
2032  *
2033  * This function is mainly a wrapper around driver_unregister().
2034  */
ccw_driver_unregister(struct ccw_driver * cdriver)2035 void ccw_driver_unregister(struct ccw_driver *cdriver)
2036 {
2037 	driver_unregister(&cdriver->driver);
2038 }
2039 
ccw_device_todo(struct work_struct * work)2040 static void ccw_device_todo(struct work_struct *work)
2041 {
2042 	struct ccw_device_private *priv;
2043 	struct ccw_device *cdev;
2044 	struct subchannel *sch;
2045 	enum cdev_todo todo;
2046 
2047 	priv = container_of(work, struct ccw_device_private, todo_work);
2048 	cdev = priv->cdev;
2049 	sch = to_subchannel(cdev->dev.parent);
2050 	/* Find out todo. */
2051 	spin_lock_irq(cdev->ccwlock);
2052 	todo = priv->todo;
2053 	priv->todo = CDEV_TODO_NOTHING;
2054 	CIO_MSG_EVENT(4, "cdev_todo: cdev=0.%x.%04x todo=%d\n",
2055 		      priv->dev_id.ssid, priv->dev_id.devno, todo);
2056 	spin_unlock_irq(cdev->ccwlock);
2057 	/* Perform todo. */
2058 	switch (todo) {
2059 	case CDEV_TODO_ENABLE_CMF:
2060 		cmf_reenable(cdev);
2061 		break;
2062 	case CDEV_TODO_REBIND:
2063 		ccw_device_do_unbind_bind(cdev);
2064 		break;
2065 	case CDEV_TODO_REGISTER:
2066 		io_subchannel_register(cdev);
2067 		break;
2068 	case CDEV_TODO_UNREG_EVAL:
2069 		if (!sch_is_pseudo_sch(sch))
2070 			css_schedule_eval(sch->schid);
2071 		/* fall-through */
2072 	case CDEV_TODO_UNREG:
2073 		if (sch_is_pseudo_sch(sch))
2074 			ccw_device_unregister(cdev);
2075 		else
2076 			ccw_device_call_sch_unregister(cdev);
2077 		break;
2078 	default:
2079 		break;
2080 	}
2081 	/* Release workqueue ref. */
2082 	put_device(&cdev->dev);
2083 }
2084 
2085 /**
2086  * ccw_device_sched_todo - schedule ccw device operation
2087  * @cdev: ccw device
2088  * @todo: todo
2089  *
2090  * Schedule the operation identified by @todo to be performed on the slow path
2091  * workqueue. Do nothing if another operation with higher priority is already
2092  * scheduled. Needs to be called with ccwdev lock held.
2093  */
ccw_device_sched_todo(struct ccw_device * cdev,enum cdev_todo todo)2094 void ccw_device_sched_todo(struct ccw_device *cdev, enum cdev_todo todo)
2095 {
2096 	CIO_MSG_EVENT(4, "cdev_todo: sched cdev=0.%x.%04x todo=%d\n",
2097 		      cdev->private->dev_id.ssid, cdev->private->dev_id.devno,
2098 		      todo);
2099 	if (cdev->private->todo >= todo)
2100 		return;
2101 	cdev->private->todo = todo;
2102 	/* Get workqueue ref. */
2103 	if (!get_device(&cdev->dev))
2104 		return;
2105 	if (!queue_work(cio_work_q, &cdev->private->todo_work)) {
2106 		/* Already queued, release workqueue ref. */
2107 		put_device(&cdev->dev);
2108 	}
2109 }
2110 
2111 /**
2112  * ccw_device_siosl() - initiate logging
2113  * @cdev: ccw device
2114  *
2115  * This function is used to invoke model-dependent logging within the channel
2116  * subsystem.
2117  */
ccw_device_siosl(struct ccw_device * cdev)2118 int ccw_device_siosl(struct ccw_device *cdev)
2119 {
2120 	struct subchannel *sch = to_subchannel(cdev->dev.parent);
2121 
2122 	return chsc_siosl(sch->schid);
2123 }
2124 EXPORT_SYMBOL_GPL(ccw_device_siosl);
2125 
2126 EXPORT_SYMBOL(ccw_device_set_online);
2127 EXPORT_SYMBOL(ccw_device_set_offline);
2128 EXPORT_SYMBOL(ccw_driver_register);
2129 EXPORT_SYMBOL(ccw_driver_unregister);
2130 EXPORT_SYMBOL(get_ccwdev_by_busid);
2131