[S390] cio: css_probe_device() must be called enabled.
[safe/jmp/linux-2.6] / drivers / s390 / cio / css.c
1 /*
2  *  drivers/s390/cio/css.c
3  *  driver for channel subsystem
4  *
5  *    Copyright (C) 2002 IBM Deutschland Entwicklung GmbH,
6  *                       IBM Corporation
7  *    Author(s): Arnd Bergmann (arndb@de.ibm.com)
8  *               Cornelia Huck (cornelia.huck@de.ibm.com)
9  */
10 #include <linux/module.h>
11 #include <linux/init.h>
12 #include <linux/device.h>
13 #include <linux/slab.h>
14 #include <linux/errno.h>
15 #include <linux/list.h>
16
17 #include "css.h"
18 #include "cio.h"
19 #include "cio_debug.h"
20 #include "ioasm.h"
21 #include "chsc.h"
22 #include "device.h"
23
24 int need_rescan = 0;
25 int css_init_done = 0;
26 static int need_reprobe = 0;
27 static int max_ssid = 0;
28
29 struct channel_subsystem *css[__MAX_CSSID + 1];
30
31 int css_characteristics_avail = 0;
32
33 inline int
34 for_each_subchannel(int(*fn)(struct subchannel_id, void *), void *data)
35 {
36         struct subchannel_id schid;
37         int ret;
38
39         init_subchannel_id(&schid);
40         ret = -ENODEV;
41         do {
42                 do {
43                         ret = fn(schid, data);
44                         if (ret)
45                                 break;
46                 } while (schid.sch_no++ < __MAX_SUBCHANNEL);
47                 schid.sch_no = 0;
48         } while (schid.ssid++ < max_ssid);
49         return ret;
50 }
51
52 static struct subchannel *
53 css_alloc_subchannel(struct subchannel_id schid)
54 {
55         struct subchannel *sch;
56         int ret;
57
58         sch = kmalloc (sizeof (*sch), GFP_KERNEL | GFP_DMA);
59         if (sch == NULL)
60                 return ERR_PTR(-ENOMEM);
61         ret = cio_validate_subchannel (sch, schid);
62         if (ret < 0) {
63                 kfree(sch);
64                 return ERR_PTR(ret);
65         }
66
67         if (sch->st != SUBCHANNEL_TYPE_IO) {
68                 /* For now we ignore all non-io subchannels. */
69                 kfree(sch);
70                 return ERR_PTR(-EINVAL);
71         }
72
73         /* 
74          * Set intparm to subchannel address.
75          * This is fine even on 64bit since the subchannel is always located
76          * under 2G.
77          */
78         sch->schib.pmcw.intparm = (__u32)(unsigned long)sch;
79         ret = cio_modify(sch);
80         if (ret) {
81                 kfree(sch);
82                 return ERR_PTR(ret);
83         }
84         return sch;
85 }
86
87 static void
88 css_free_subchannel(struct subchannel *sch)
89 {
90         if (sch) {
91                 /* Reset intparm to zeroes. */
92                 sch->schib.pmcw.intparm = 0;
93                 cio_modify(sch);
94                 kfree(sch);
95         }
96         
97 }
98
99 static void
100 css_subchannel_release(struct device *dev)
101 {
102         struct subchannel *sch;
103
104         sch = to_subchannel(dev);
105         if (!cio_is_console(sch->schid))
106                 kfree(sch);
107 }
108
109 extern int css_get_ssd_info(struct subchannel *sch);
110
111
112 int css_sch_device_register(struct subchannel *sch)
113 {
114         int ret;
115
116         mutex_lock(&sch->reg_mutex);
117         ret = device_register(&sch->dev);
118         mutex_unlock(&sch->reg_mutex);
119         return ret;
120 }
121
122 void css_sch_device_unregister(struct subchannel *sch)
123 {
124         mutex_lock(&sch->reg_mutex);
125         device_unregister(&sch->dev);
126         mutex_unlock(&sch->reg_mutex);
127 }
128
129 static int
130 css_register_subchannel(struct subchannel *sch)
131 {
132         int ret;
133
134         /* Initialize the subchannel structure */
135         sch->dev.parent = &css[0]->device;
136         sch->dev.bus = &css_bus_type;
137         sch->dev.release = &css_subchannel_release;
138         
139         /* make it known to the system */
140         ret = css_sch_device_register(sch);
141         if (ret)
142                 printk (KERN_WARNING "%s: could not register %s\n",
143                         __func__, sch->dev.bus_id);
144         else
145                 css_get_ssd_info(sch);
146         return ret;
147 }
148
149 int
150 css_probe_device(struct subchannel_id schid)
151 {
152         int ret;
153         struct subchannel *sch;
154
155         sch = css_alloc_subchannel(schid);
156         if (IS_ERR(sch))
157                 return PTR_ERR(sch);
158         ret = css_register_subchannel(sch);
159         if (ret)
160                 css_free_subchannel(sch);
161         return ret;
162 }
163
164 static int
165 check_subchannel(struct device * dev, void * data)
166 {
167         struct subchannel *sch;
168         struct subchannel_id *schid = data;
169
170         sch = to_subchannel(dev);
171         return schid_equal(&sch->schid, schid);
172 }
173
174 struct subchannel *
175 get_subchannel_by_schid(struct subchannel_id schid)
176 {
177         struct device *dev;
178
179         dev = bus_find_device(&css_bus_type, NULL,
180                               &schid, check_subchannel);
181
182         return dev ? to_subchannel(dev) : NULL;
183 }
184
185 static inline int css_get_subchannel_status(struct subchannel *sch)
186 {
187         struct schib schib;
188
189         if (stsch(sch->schid, &schib) || !schib.pmcw.dnv)
190                 return CIO_GONE;
191         if (sch->schib.pmcw.dnv && (schib.pmcw.dev != sch->schib.pmcw.dev))
192                 return CIO_REVALIDATE;
193         if (!sch->lpm)
194                 return CIO_NO_PATH;
195         return CIO_OPER;
196 }
197
198 static int css_evaluate_known_subchannel(struct subchannel *sch, int slow)
199 {
200         int event, ret, disc;
201         unsigned long flags;
202         enum { NONE, UNREGISTER, UNREGISTER_PROBE, REPROBE } action;
203
204         spin_lock_irqsave(&sch->lock, flags);
205         disc = device_is_disconnected(sch);
206         if (disc && slow) {
207                 /* Disconnected devices are evaluated directly only.*/
208                 spin_unlock_irqrestore(&sch->lock, flags);
209                 return 0;
210         }
211         /* No interrupt after machine check - kill pending timers. */
212         device_kill_pending_timer(sch);
213         if (!disc && !slow) {
214                 /* Non-disconnected devices are evaluated on the slow path. */
215                 spin_unlock_irqrestore(&sch->lock, flags);
216                 return -EAGAIN;
217         }
218         event = css_get_subchannel_status(sch);
219         CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, %s, %s path.\n",
220                       sch->schid.ssid, sch->schid.sch_no, event,
221                       disc ? "disconnected" : "normal",
222                       slow ? "slow" : "fast");
223         /* Analyze subchannel status. */
224         action = NONE;
225         switch (event) {
226         case CIO_NO_PATH:
227                 if (disc) {
228                         /* Check if paths have become available. */
229                         action = REPROBE;
230                         break;
231                 }
232                 /* fall through */
233         case CIO_GONE:
234                 /* Prevent unwanted effects when opening lock. */
235                 cio_disable_subchannel(sch);
236                 device_set_disconnected(sch);
237                 /* Ask driver what to do with device. */
238                 action = UNREGISTER;
239                 if (sch->driver && sch->driver->notify) {
240                         spin_unlock_irqrestore(&sch->lock, flags);
241                         ret = sch->driver->notify(&sch->dev, event);
242                         spin_lock_irqsave(&sch->lock, flags);
243                         if (ret)
244                                 action = NONE;
245                 }
246                 break;
247         case CIO_REVALIDATE:
248                 /* Device will be removed, so no notify necessary. */
249                 if (disc)
250                         /* Reprobe because immediate unregister might block. */
251                         action = REPROBE;
252                 else
253                         action = UNREGISTER_PROBE;
254                 break;
255         case CIO_OPER:
256                 if (disc)
257                         /* Get device operational again. */
258                         action = REPROBE;
259                 break;
260         }
261         /* Perform action. */
262         ret = 0;
263         switch (action) {
264         case UNREGISTER:
265         case UNREGISTER_PROBE:
266                 /* Unregister device (will use subchannel lock). */
267                 spin_unlock_irqrestore(&sch->lock, flags);
268                 css_sch_device_unregister(sch);
269                 spin_lock_irqsave(&sch->lock, flags);
270
271                 /* Reset intparm to zeroes. */
272                 sch->schib.pmcw.intparm = 0;
273                 cio_modify(sch);
274                 break;
275         case REPROBE:
276                 device_trigger_reprobe(sch);
277                 break;
278         default:
279                 break;
280         }
281         spin_unlock_irqrestore(&sch->lock, flags);
282         /* Probe if necessary. */
283         if (action == UNREGISTER_PROBE)
284                 ret = css_probe_device(sch->schid);
285
286         return ret;
287 }
288
289 static int css_evaluate_new_subchannel(struct subchannel_id schid, int slow)
290 {
291         struct schib schib;
292
293         if (!slow) {
294                 /* Will be done on the slow path. */
295                 return -EAGAIN;
296         }
297         if (stsch(schid, &schib) || !schib.pmcw.dnv) {
298                 /* Unusable - ignore. */
299                 return 0;
300         }
301         CIO_MSG_EVENT(4, "Evaluating schid 0.%x.%04x, event %d, unknown, "
302                          "slow path.\n", schid.ssid, schid.sch_no, CIO_OPER);
303
304         return css_probe_device(schid);
305 }
306
307 static int css_evaluate_subchannel(struct subchannel_id schid, int slow)
308 {
309         struct subchannel *sch;
310         int ret;
311
312         sch = get_subchannel_by_schid(schid);
313         if (sch) {
314                 ret = css_evaluate_known_subchannel(sch, slow);
315                 put_device(&sch->dev);
316         } else
317                 ret = css_evaluate_new_subchannel(schid, slow);
318
319         return ret;
320 }
321
322 static int
323 css_rescan_devices(struct subchannel_id schid, void *data)
324 {
325         return css_evaluate_subchannel(schid, 1);
326 }
327
328 struct slow_subchannel {
329         struct list_head slow_list;
330         struct subchannel_id schid;
331 };
332
333 static LIST_HEAD(slow_subchannels_head);
334 static DEFINE_SPINLOCK(slow_subchannel_lock);
335
336 static void
337 css_trigger_slow_path(void)
338 {
339         CIO_TRACE_EVENT(4, "slowpath");
340
341         if (need_rescan) {
342                 need_rescan = 0;
343                 for_each_subchannel(css_rescan_devices, NULL);
344                 return;
345         }
346
347         spin_lock_irq(&slow_subchannel_lock);
348         while (!list_empty(&slow_subchannels_head)) {
349                 struct slow_subchannel *slow_sch =
350                         list_entry(slow_subchannels_head.next,
351                                    struct slow_subchannel, slow_list);
352
353                 list_del_init(slow_subchannels_head.next);
354                 spin_unlock_irq(&slow_subchannel_lock);
355                 css_evaluate_subchannel(slow_sch->schid, 1);
356                 spin_lock_irq(&slow_subchannel_lock);
357                 kfree(slow_sch);
358         }
359         spin_unlock_irq(&slow_subchannel_lock);
360 }
361
362 typedef void (*workfunc)(void *);
363 DECLARE_WORK(slow_path_work, (workfunc)css_trigger_slow_path, NULL);
364 struct workqueue_struct *slow_path_wq;
365
366 /* Reprobe subchannel if unregistered. */
367 static int reprobe_subchannel(struct subchannel_id schid, void *data)
368 {
369         struct subchannel *sch;
370         int ret;
371
372         CIO_DEBUG(KERN_INFO, 6, "cio: reprobe 0.%x.%04x\n",
373                   schid.ssid, schid.sch_no);
374         if (need_reprobe)
375                 return -EAGAIN;
376
377         sch = get_subchannel_by_schid(schid);
378         if (sch) {
379                 /* Already known. */
380                 put_device(&sch->dev);
381                 return 0;
382         }
383
384         ret = css_probe_device(schid);
385         switch (ret) {
386         case 0:
387                 break;
388         case -ENXIO:
389         case -ENOMEM:
390                 /* These should abort looping */
391                 break;
392         default:
393                 ret = 0;
394         }
395
396         return ret;
397 }
398
399 /* Work function used to reprobe all unregistered subchannels. */
400 static void reprobe_all(void *data)
401 {
402         int ret;
403
404         CIO_MSG_EVENT(2, "reprobe start\n");
405
406         need_reprobe = 0;
407         /* Make sure initial subchannel scan is done. */
408         wait_event(ccw_device_init_wq,
409                    atomic_read(&ccw_device_init_count) == 0);
410         ret = for_each_subchannel(reprobe_subchannel, NULL);
411
412         CIO_MSG_EVENT(2, "reprobe done (rc=%d, need_reprobe=%d)\n", ret,
413                       need_reprobe);
414 }
415
416 DECLARE_WORK(css_reprobe_work, reprobe_all, NULL);
417
418 /* Schedule reprobing of all unregistered subchannels. */
419 void css_schedule_reprobe(void)
420 {
421         need_reprobe = 1;
422         queue_work(ccw_device_work, &css_reprobe_work);
423 }
424
425 EXPORT_SYMBOL_GPL(css_schedule_reprobe);
426
427 /*
428  * Rescan for new devices. FIXME: This is slow.
429  * This function is called when we have lost CRWs due to overflows and we have
430  * to do subchannel housekeeping.
431  */
432 void
433 css_reiterate_subchannels(void)
434 {
435         css_clear_subchannel_slow_list();
436         need_rescan = 1;
437 }
438
439 /*
440  * Called from the machine check handler for subchannel report words.
441  */
442 int
443 css_process_crw(int rsid1, int rsid2)
444 {
445         int ret;
446         struct subchannel_id mchk_schid;
447
448         CIO_CRW_EVENT(2, "source is subchannel %04X, subsystem id %x\n",
449                       rsid1, rsid2);
450
451         if (need_rescan)
452                 /* We need to iterate all subchannels anyway. */
453                 return -EAGAIN;
454
455         init_subchannel_id(&mchk_schid);
456         mchk_schid.sch_no = rsid1;
457         if (rsid2 != 0)
458                 mchk_schid.ssid = (rsid2 >> 8) & 3;
459
460         /* 
461          * Since we are always presented with IPI in the CRW, we have to
462          * use stsch() to find out if the subchannel in question has come
463          * or gone.
464          */
465         ret = css_evaluate_subchannel(mchk_schid, 0);
466         if (ret == -EAGAIN) {
467                 if (css_enqueue_subchannel_slow(mchk_schid)) {
468                         css_clear_subchannel_slow_list();
469                         need_rescan = 1;
470                 }
471         }
472         return ret;
473 }
474
475 static int __init
476 __init_channel_subsystem(struct subchannel_id schid, void *data)
477 {
478         struct subchannel *sch;
479         int ret;
480
481         if (cio_is_console(schid))
482                 sch = cio_get_console_subchannel();
483         else {
484                 sch = css_alloc_subchannel(schid);
485                 if (IS_ERR(sch))
486                         ret = PTR_ERR(sch);
487                 else
488                         ret = 0;
489                 switch (ret) {
490                 case 0:
491                         break;
492                 case -ENOMEM:
493                         panic("Out of memory in init_channel_subsystem\n");
494                 /* -ENXIO: no more subchannels. */
495                 case -ENXIO:
496                         return ret;
497                 /* -EIO: this subchannel set not supported. */
498                 case -EIO:
499                         return ret;
500                 default:
501                         return 0;
502                 }
503         }
504         /*
505          * We register ALL valid subchannels in ioinfo, even those
506          * that have been present before init_channel_subsystem.
507          * These subchannels can't have been registered yet (kmalloc
508          * not working) so we do it now. This is true e.g. for the
509          * console subchannel.
510          */
511         css_register_subchannel(sch);
512         return 0;
513 }
514
515 static void __init
516 css_generate_pgid(struct channel_subsystem *css, u32 tod_high)
517 {
518         if (css_characteristics_avail && css_general_characteristics.mcss) {
519                 css->global_pgid.pgid_high.ext_cssid.version = 0x80;
520                 css->global_pgid.pgid_high.ext_cssid.cssid = css->cssid;
521         } else {
522 #ifdef CONFIG_SMP
523                 css->global_pgid.pgid_high.cpu_addr = hard_smp_processor_id();
524 #else
525                 css->global_pgid.pgid_high.cpu_addr = 0;
526 #endif
527         }
528         css->global_pgid.cpu_id = ((cpuid_t *) __LC_CPUID)->ident;
529         css->global_pgid.cpu_model = ((cpuid_t *) __LC_CPUID)->machine;
530         css->global_pgid.tod_high = tod_high;
531
532 }
533
534 static void
535 channel_subsystem_release(struct device *dev)
536 {
537         struct channel_subsystem *css;
538
539         css = to_css(dev);
540         mutex_destroy(&css->mutex);
541         kfree(css);
542 }
543
544 static ssize_t
545 css_cm_enable_show(struct device *dev, struct device_attribute *attr,
546                    char *buf)
547 {
548         struct channel_subsystem *css = to_css(dev);
549
550         if (!css)
551                 return 0;
552         return sprintf(buf, "%x\n", css->cm_enabled);
553 }
554
555 static ssize_t
556 css_cm_enable_store(struct device *dev, struct device_attribute *attr,
557                     const char *buf, size_t count)
558 {
559         struct channel_subsystem *css = to_css(dev);
560         int ret;
561
562         switch (buf[0]) {
563         case '0':
564                 ret = css->cm_enabled ? chsc_secm(css, 0) : 0;
565                 break;
566         case '1':
567                 ret = css->cm_enabled ? 0 : chsc_secm(css, 1);
568                 break;
569         default:
570                 ret = -EINVAL;
571         }
572         return ret < 0 ? ret : count;
573 }
574
575 static DEVICE_ATTR(cm_enable, 0644, css_cm_enable_show, css_cm_enable_store);
576
577 static inline void __init
578 setup_css(int nr)
579 {
580         u32 tod_high;
581
582         memset(css[nr], 0, sizeof(struct channel_subsystem));
583         mutex_init(&css[nr]->mutex);
584         css[nr]->valid = 1;
585         css[nr]->cssid = nr;
586         sprintf(css[nr]->device.bus_id, "css%x", nr);
587         css[nr]->device.release = channel_subsystem_release;
588         tod_high = (u32) (get_clock() >> 32);
589         css_generate_pgid(css[nr], tod_high);
590 }
591
592 /*
593  * Now that the driver core is running, we can setup our channel subsystem.
594  * The struct subchannel's are created during probing (except for the
595  * static console subchannel).
596  */
597 static int __init
598 init_channel_subsystem (void)
599 {
600         int ret, i;
601
602         if (chsc_determine_css_characteristics() == 0)
603                 css_characteristics_avail = 1;
604
605         if ((ret = bus_register(&css_bus_type)))
606                 goto out;
607
608         /* Try to enable MSS. */
609         ret = chsc_enable_facility(CHSC_SDA_OC_MSS);
610         switch (ret) {
611         case 0: /* Success. */
612                 max_ssid = __MAX_SSID;
613                 break;
614         case -ENOMEM:
615                 goto out_bus;
616         default:
617                 max_ssid = 0;
618         }
619         /* Setup css structure. */
620         for (i = 0; i <= __MAX_CSSID; i++) {
621                 css[i] = kmalloc(sizeof(struct channel_subsystem), GFP_KERNEL);
622                 if (!css[i]) {
623                         ret = -ENOMEM;
624                         goto out_unregister;
625                 }
626                 setup_css(i);
627                 ret = device_register(&css[i]->device);
628                 if (ret)
629                         goto out_free;
630                 if (css_characteristics_avail &&
631                     css_chsc_characteristics.secm) {
632                         ret = device_create_file(&css[i]->device,
633                                                  &dev_attr_cm_enable);
634                         if (ret)
635                                 goto out_device;
636                 }
637         }
638         css_init_done = 1;
639
640         ctl_set_bit(6, 28);
641
642         for_each_subchannel(__init_channel_subsystem, NULL);
643         return 0;
644 out_device:
645         device_unregister(&css[i]->device);
646 out_free:
647         kfree(css[i]);
648 out_unregister:
649         while (i > 0) {
650                 i--;
651                 if (css_characteristics_avail && css_chsc_characteristics.secm)
652                         device_remove_file(&css[i]->device,
653                                            &dev_attr_cm_enable);
654                 device_unregister(&css[i]->device);
655         }
656 out_bus:
657         bus_unregister(&css_bus_type);
658 out:
659         return ret;
660 }
661
662 /*
663  * find a driver for a subchannel. They identify by the subchannel
664  * type with the exception that the console subchannel driver has its own
665  * subchannel type although the device is an i/o subchannel
666  */
667 static int
668 css_bus_match (struct device *dev, struct device_driver *drv)
669 {
670         struct subchannel *sch = container_of (dev, struct subchannel, dev);
671         struct css_driver *driver = container_of (drv, struct css_driver, drv);
672
673         if (sch->st == driver->subchannel_type)
674                 return 1;
675
676         return 0;
677 }
678
679 static int
680 css_probe (struct device *dev)
681 {
682         struct subchannel *sch;
683
684         sch = to_subchannel(dev);
685         sch->driver = container_of (dev->driver, struct css_driver, drv);
686         return (sch->driver->probe ? sch->driver->probe(sch) : 0);
687 }
688
689 static int
690 css_remove (struct device *dev)
691 {
692         struct subchannel *sch;
693
694         sch = to_subchannel(dev);
695         return (sch->driver->remove ? sch->driver->remove(sch) : 0);
696 }
697
698 static void
699 css_shutdown (struct device *dev)
700 {
701         struct subchannel *sch;
702
703         sch = to_subchannel(dev);
704         if (sch->driver->shutdown)
705                 sch->driver->shutdown(sch);
706 }
707
708 struct bus_type css_bus_type = {
709         .name     = "css",
710         .match    = css_bus_match,
711         .probe    = css_probe,
712         .remove   = css_remove,
713         .shutdown = css_shutdown,
714 };
715
716 subsys_initcall(init_channel_subsystem);
717
718 int
719 css_enqueue_subchannel_slow(struct subchannel_id schid)
720 {
721         struct slow_subchannel *new_slow_sch;
722         unsigned long flags;
723
724         new_slow_sch = kzalloc(sizeof(struct slow_subchannel), GFP_ATOMIC);
725         if (!new_slow_sch)
726                 return -ENOMEM;
727         new_slow_sch->schid = schid;
728         spin_lock_irqsave(&slow_subchannel_lock, flags);
729         list_add_tail(&new_slow_sch->slow_list, &slow_subchannels_head);
730         spin_unlock_irqrestore(&slow_subchannel_lock, flags);
731         return 0;
732 }
733
734 void
735 css_clear_subchannel_slow_list(void)
736 {
737         unsigned long flags;
738
739         spin_lock_irqsave(&slow_subchannel_lock, flags);
740         while (!list_empty(&slow_subchannels_head)) {
741                 struct slow_subchannel *slow_sch =
742                         list_entry(slow_subchannels_head.next,
743                                    struct slow_subchannel, slow_list);
744
745                 list_del_init(slow_subchannels_head.next);
746                 kfree(slow_sch);
747         }
748         spin_unlock_irqrestore(&slow_subchannel_lock, flags);
749 }
750
751
752
753 int
754 css_slow_subchannels_exist(void)
755 {
756         return (!list_empty(&slow_subchannels_head));
757 }
758
759 MODULE_LICENSE("GPL");
760 EXPORT_SYMBOL(css_bus_type);
761 EXPORT_SYMBOL_GPL(css_characteristics_avail);