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