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