1
2
3
4
5
6
7
8#include <linux/export.h>
9#include <linux/init.h>
10#include <linux/errno.h>
11#include <linux/slab.h>
12#include <linux/list.h>
13#include <linux/device.h>
14#include <linux/delay.h>
15#include <linux/completion.h>
16
17#include <asm/ccwdev.h>
18#include <asm/idals.h>
19#include <asm/chpid.h>
20#include <asm/fcx.h>
21
22#include "cio.h"
23#include "cio_debug.h"
24#include "css.h"
25#include "chsc.h"
26#include "device.h"
27#include "chp.h"
28
29
30
31
32
33
34
35
36
37
38
39int ccw_device_set_options_mask(struct ccw_device *cdev, unsigned long flags)
40{
41
42
43
44 if ((flags & CCWDEV_EARLY_NOTIFICATION) &&
45 (flags & CCWDEV_REPORT_ALL))
46 return -EINVAL;
47 cdev->private->options.fast = (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
48 cdev->private->options.repall = (flags & CCWDEV_REPORT_ALL) != 0;
49 cdev->private->options.pgroup = (flags & CCWDEV_DO_PATHGROUP) != 0;
50 cdev->private->options.force = (flags & CCWDEV_ALLOW_FORCE) != 0;
51 cdev->private->options.mpath = (flags & CCWDEV_DO_MULTIPATH) != 0;
52 return 0;
53}
54
55
56
57
58
59
60
61
62
63
64int ccw_device_set_options(struct ccw_device *cdev, unsigned long flags)
65{
66
67
68
69 if (((flags & CCWDEV_EARLY_NOTIFICATION) &&
70 (flags & CCWDEV_REPORT_ALL)) ||
71 ((flags & CCWDEV_EARLY_NOTIFICATION) &&
72 cdev->private->options.repall) ||
73 ((flags & CCWDEV_REPORT_ALL) &&
74 cdev->private->options.fast))
75 return -EINVAL;
76 cdev->private->options.fast |= (flags & CCWDEV_EARLY_NOTIFICATION) != 0;
77 cdev->private->options.repall |= (flags & CCWDEV_REPORT_ALL) != 0;
78 cdev->private->options.pgroup |= (flags & CCWDEV_DO_PATHGROUP) != 0;
79 cdev->private->options.force |= (flags & CCWDEV_ALLOW_FORCE) != 0;
80 cdev->private->options.mpath |= (flags & CCWDEV_DO_MULTIPATH) != 0;
81 return 0;
82}
83
84
85
86
87
88
89
90
91void ccw_device_clear_options(struct ccw_device *cdev, unsigned long flags)
92{
93 cdev->private->options.fast &= (flags & CCWDEV_EARLY_NOTIFICATION) == 0;
94 cdev->private->options.repall &= (flags & CCWDEV_REPORT_ALL) == 0;
95 cdev->private->options.pgroup &= (flags & CCWDEV_DO_PATHGROUP) == 0;
96 cdev->private->options.force &= (flags & CCWDEV_ALLOW_FORCE) == 0;
97 cdev->private->options.mpath &= (flags & CCWDEV_DO_MULTIPATH) == 0;
98}
99
100
101
102
103
104
105
106int ccw_device_is_pathgroup(struct ccw_device *cdev)
107{
108 return cdev->private->flags.pgroup;
109}
110EXPORT_SYMBOL(ccw_device_is_pathgroup);
111
112
113
114
115
116
117
118int ccw_device_is_multipath(struct ccw_device *cdev)
119{
120 return cdev->private->flags.mpath;
121}
122EXPORT_SYMBOL(ccw_device_is_multipath);
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137int ccw_device_clear(struct ccw_device *cdev, unsigned long intparm)
138{
139 struct subchannel *sch;
140 int ret;
141
142 if (!cdev || !cdev->dev.parent)
143 return -ENODEV;
144 sch = to_subchannel(cdev->dev.parent);
145 if (!sch->schib.pmcw.ena)
146 return -EINVAL;
147 if (cdev->private->state == DEV_STATE_NOT_OPER)
148 return -ENODEV;
149 if (cdev->private->state != DEV_STATE_ONLINE &&
150 cdev->private->state != DEV_STATE_W4SENSE)
151 return -EINVAL;
152
153 ret = cio_clear(sch);
154 if (ret == 0)
155 cdev->private->intparm = intparm;
156 return ret;
157}
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191int ccw_device_start_timeout_key(struct ccw_device *cdev, struct ccw1 *cpa,
192 unsigned long intparm, __u8 lpm, __u8 key,
193 unsigned long flags, int expires)
194{
195 struct subchannel *sch;
196 int ret;
197
198 if (!cdev || !cdev->dev.parent)
199 return -ENODEV;
200 sch = to_subchannel(cdev->dev.parent);
201 if (!sch->schib.pmcw.ena)
202 return -EINVAL;
203 if (cdev->private->state == DEV_STATE_NOT_OPER)
204 return -ENODEV;
205 if (cdev->private->state == DEV_STATE_VERIFY) {
206
207 if (!cdev->private->flags.fake_irb) {
208 cdev->private->flags.fake_irb = FAKE_CMD_IRB;
209 cdev->private->intparm = intparm;
210 return 0;
211 } else
212
213 return -EBUSY;
214 }
215 if (cdev->private->state != DEV_STATE_ONLINE ||
216 ((sch->schib.scsw.cmd.stctl & SCSW_STCTL_PRIM_STATUS) &&
217 !(sch->schib.scsw.cmd.stctl & SCSW_STCTL_SEC_STATUS)) ||
218 cdev->private->flags.doverify)
219 return -EBUSY;
220 ret = cio_set_options (sch, flags);
221 if (ret)
222 return ret;
223
224 if (lpm) {
225 lpm &= sch->lpm;
226 if (lpm == 0)
227 return -EACCES;
228 }
229 ret = cio_start_key (sch, cpa, lpm, key);
230 switch (ret) {
231 case 0:
232 cdev->private->intparm = intparm;
233 if (expires)
234 ccw_device_set_timeout(cdev, expires);
235 break;
236 case -EACCES:
237 case -ENODEV:
238 dev_fsm_event(cdev, DEV_EVENT_VERIFY);
239 break;
240 }
241 return ret;
242}
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271int ccw_device_start_key(struct ccw_device *cdev, struct ccw1 *cpa,
272 unsigned long intparm, __u8 lpm, __u8 key,
273 unsigned long flags)
274{
275 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm, key,
276 flags, 0);
277}
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305int ccw_device_start(struct ccw_device *cdev, struct ccw1 *cpa,
306 unsigned long intparm, __u8 lpm, unsigned long flags)
307{
308 return ccw_device_start_key(cdev, cpa, intparm, lpm,
309 PAGE_DEFAULT_KEY, flags);
310}
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343int ccw_device_start_timeout(struct ccw_device *cdev, struct ccw1 *cpa,
344 unsigned long intparm, __u8 lpm,
345 unsigned long flags, int expires)
346{
347 return ccw_device_start_timeout_key(cdev, cpa, intparm, lpm,
348 PAGE_DEFAULT_KEY, flags,
349 expires);
350}
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370int ccw_device_halt(struct ccw_device *cdev, unsigned long intparm)
371{
372 struct subchannel *sch;
373 int ret;
374
375 if (!cdev || !cdev->dev.parent)
376 return -ENODEV;
377 sch = to_subchannel(cdev->dev.parent);
378 if (!sch->schib.pmcw.ena)
379 return -EINVAL;
380 if (cdev->private->state == DEV_STATE_NOT_OPER)
381 return -ENODEV;
382 if (cdev->private->state != DEV_STATE_ONLINE &&
383 cdev->private->state != DEV_STATE_W4SENSE)
384 return -EINVAL;
385
386 ret = cio_halt(sch);
387 if (ret == 0)
388 cdev->private->intparm = intparm;
389 return ret;
390}
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405int ccw_device_resume(struct ccw_device *cdev)
406{
407 struct subchannel *sch;
408
409 if (!cdev || !cdev->dev.parent)
410 return -ENODEV;
411 sch = to_subchannel(cdev->dev.parent);
412 if (!sch->schib.pmcw.ena)
413 return -EINVAL;
414 if (cdev->private->state == DEV_STATE_NOT_OPER)
415 return -ENODEV;
416 if (cdev->private->state != DEV_STATE_ONLINE ||
417 !(sch->schib.scsw.cmd.actl & SCSW_ACTL_SUSPENDED))
418 return -EINVAL;
419 return cio_resume(sch);
420}
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436struct ciw *ccw_device_get_ciw(struct ccw_device *cdev, __u32 ct)
437{
438 int ciw_cnt;
439
440 if (cdev->private->flags.esid == 0)
441 return NULL;
442 for (ciw_cnt = 0; ciw_cnt < MAX_CIWS; ciw_cnt++)
443 if (cdev->private->dma_area->senseid.ciw[ciw_cnt].ct == ct)
444 return cdev->private->dma_area->senseid.ciw + ciw_cnt;
445 return NULL;
446}
447
448
449
450
451
452
453
454
455__u8 ccw_device_get_path_mask(struct ccw_device *cdev)
456{
457 struct subchannel *sch;
458
459 if (!cdev->dev.parent)
460 return 0;
461
462 sch = to_subchannel(cdev->dev.parent);
463 return sch->lpm;
464}
465
466
467
468
469
470
471
472
473
474struct channel_path_desc_fmt0 *ccw_device_get_chp_desc(struct ccw_device *cdev,
475 int chp_idx)
476{
477 struct subchannel *sch;
478 struct chp_id chpid;
479
480 sch = to_subchannel(cdev->dev.parent);
481 chp_id_init(&chpid);
482 chpid.id = sch->schib.pmcw.chpid[chp_idx];
483 return chp_get_chp_desc(chpid);
484}
485
486
487
488
489
490
491
492
493
494u8 *ccw_device_get_util_str(struct ccw_device *cdev, int chp_idx)
495{
496 struct subchannel *sch = to_subchannel(cdev->dev.parent);
497 struct channel_path *chp;
498 struct chp_id chpid;
499 u8 *util_str;
500
501 chp_id_init(&chpid);
502 chpid.id = sch->schib.pmcw.chpid[chp_idx];
503 chp = chpid_to_chp(chpid);
504
505 util_str = kmalloc(sizeof(chp->desc_fmt3.util_str), GFP_KERNEL);
506 if (!util_str)
507 return NULL;
508
509 mutex_lock(&chp->lock);
510 memcpy(util_str, chp->desc_fmt3.util_str, sizeof(chp->desc_fmt3.util_str));
511 mutex_unlock(&chp->lock);
512
513 return util_str;
514}
515
516
517
518
519
520
521void ccw_device_get_id(struct ccw_device *cdev, struct ccw_dev_id *dev_id)
522{
523 *dev_id = cdev->private->dev_id;
524}
525EXPORT_SYMBOL(ccw_device_get_id);
526
527
528
529
530
531
532
533
534
535
536
537
538
539int ccw_device_tm_start_timeout_key(struct ccw_device *cdev, struct tcw *tcw,
540 unsigned long intparm, u8 lpm, u8 key,
541 int expires)
542{
543 struct subchannel *sch;
544 int rc;
545
546 sch = to_subchannel(cdev->dev.parent);
547 if (!sch->schib.pmcw.ena)
548 return -EINVAL;
549 if (cdev->private->state == DEV_STATE_VERIFY) {
550
551 if (!cdev->private->flags.fake_irb) {
552 cdev->private->flags.fake_irb = FAKE_TM_IRB;
553 cdev->private->intparm = intparm;
554 return 0;
555 } else
556
557 return -EBUSY;
558 }
559 if (cdev->private->state != DEV_STATE_ONLINE)
560 return -EIO;
561
562 if (lpm) {
563 lpm &= sch->lpm;
564 if (lpm == 0)
565 return -EACCES;
566 }
567 rc = cio_tm_start_key(sch, tcw, lpm, key);
568 if (rc == 0) {
569 cdev->private->intparm = intparm;
570 if (expires)
571 ccw_device_set_timeout(cdev, expires);
572 }
573 return rc;
574}
575EXPORT_SYMBOL(ccw_device_tm_start_timeout_key);
576
577
578
579
580
581
582
583
584
585
586
587
588int ccw_device_tm_start_key(struct ccw_device *cdev, struct tcw *tcw,
589 unsigned long intparm, u8 lpm, u8 key)
590{
591 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm, key, 0);
592}
593EXPORT_SYMBOL(ccw_device_tm_start_key);
594
595
596
597
598
599
600
601
602
603
604
605int ccw_device_tm_start(struct ccw_device *cdev, struct tcw *tcw,
606 unsigned long intparm, u8 lpm)
607{
608 return ccw_device_tm_start_key(cdev, tcw, intparm, lpm,
609 PAGE_DEFAULT_KEY);
610}
611EXPORT_SYMBOL(ccw_device_tm_start);
612
613
614
615
616
617
618
619
620
621
622
623
624int ccw_device_tm_start_timeout(struct ccw_device *cdev, struct tcw *tcw,
625 unsigned long intparm, u8 lpm, int expires)
626{
627 return ccw_device_tm_start_timeout_key(cdev, tcw, intparm, lpm,
628 PAGE_DEFAULT_KEY, expires);
629}
630EXPORT_SYMBOL(ccw_device_tm_start_timeout);
631
632
633
634
635
636
637
638
639
640int ccw_device_get_mdc(struct ccw_device *cdev, u8 mask)
641{
642 struct subchannel *sch = to_subchannel(cdev->dev.parent);
643 struct channel_path *chp;
644 struct chp_id chpid;
645 int mdc = 0, i;
646
647
648 if (mask)
649 mask &= sch->lpm;
650 else
651 mask = sch->lpm;
652
653 chp_id_init(&chpid);
654 for (i = 0; i < 8; i++) {
655 if (!(mask & (0x80 >> i)))
656 continue;
657 chpid.id = sch->schib.pmcw.chpid[i];
658 chp = chpid_to_chp(chpid);
659 if (!chp)
660 continue;
661
662 mutex_lock(&chp->lock);
663 if (!chp->desc_fmt1.f) {
664 mutex_unlock(&chp->lock);
665 return 0;
666 }
667 if (!chp->desc_fmt1.r)
668 mdc = 1;
669 mdc = mdc ? min_t(int, mdc, chp->desc_fmt1.mdc) :
670 chp->desc_fmt1.mdc;
671 mutex_unlock(&chp->lock);
672 }
673
674 return mdc;
675}
676EXPORT_SYMBOL(ccw_device_get_mdc);
677
678
679
680
681
682
683
684
685int ccw_device_tm_intrg(struct ccw_device *cdev)
686{
687 struct subchannel *sch = to_subchannel(cdev->dev.parent);
688
689 if (!sch->schib.pmcw.ena)
690 return -EINVAL;
691 if (cdev->private->state != DEV_STATE_ONLINE)
692 return -EIO;
693 if (!scsw_is_tm(&sch->schib.scsw) ||
694 !(scsw_actl(&sch->schib.scsw) & SCSW_ACTL_START_PEND))
695 return -EINVAL;
696 return cio_tm_intrg(sch);
697}
698EXPORT_SYMBOL(ccw_device_tm_intrg);
699
700
701
702
703
704
705void ccw_device_get_schid(struct ccw_device *cdev, struct subchannel_id *schid)
706{
707 struct subchannel *sch = to_subchannel(cdev->dev.parent);
708
709 *schid = sch->schid;
710}
711EXPORT_SYMBOL_GPL(ccw_device_get_schid);
712
713
714
715
716
717
718
719
720
721
722
723
724
725int ccw_device_pnso(struct ccw_device *cdev,
726 struct chsc_pnso_area *pnso_area, u8 oc,
727 struct chsc_pnso_resume_token resume_token, int cnc)
728{
729 struct subchannel_id schid;
730
731 ccw_device_get_schid(cdev, &schid);
732 return chsc_pnso(schid, pnso_area, oc, resume_token, cnc);
733}
734EXPORT_SYMBOL_GPL(ccw_device_pnso);
735
736
737
738
739
740
741int ccw_device_get_cssid(struct ccw_device *cdev, u8 *cssid)
742{
743 struct device *sch_dev = cdev->dev.parent;
744 struct channel_subsystem *css = to_css(sch_dev->parent);
745
746 if (css->id_valid)
747 *cssid = css->cssid;
748 return css->id_valid ? 0 : -ENODEV;
749}
750EXPORT_SYMBOL_GPL(ccw_device_get_cssid);
751
752
753
754
755
756
757int ccw_device_get_iid(struct ccw_device *cdev, u8 *iid)
758{
759 struct device *sch_dev = cdev->dev.parent;
760 struct channel_subsystem *css = to_css(sch_dev->parent);
761
762 if (css->id_valid)
763 *iid = css->iid;
764 return css->id_valid ? 0 : -ENODEV;
765}
766EXPORT_SYMBOL_GPL(ccw_device_get_iid);
767
768
769
770
771
772
773
774int ccw_device_get_chpid(struct ccw_device *cdev, int chp_idx, u8 *chpid)
775{
776 struct subchannel *sch = to_subchannel(cdev->dev.parent);
777 int mask;
778
779 if ((chp_idx < 0) || (chp_idx > 7))
780 return -EINVAL;
781 mask = 0x80 >> chp_idx;
782 if (!(sch->schib.pmcw.pim & mask))
783 return -ENODEV;
784
785 *chpid = sch->schib.pmcw.chpid[chp_idx];
786 return 0;
787}
788EXPORT_SYMBOL_GPL(ccw_device_get_chpid);
789
790
791
792
793
794
795
796int ccw_device_get_chid(struct ccw_device *cdev, int chp_idx, u16 *chid)
797{
798 struct chp_id cssid_chpid;
799 struct channel_path *chp;
800 int rc;
801
802 chp_id_init(&cssid_chpid);
803 rc = ccw_device_get_chpid(cdev, chp_idx, &cssid_chpid.id);
804 if (rc)
805 return rc;
806 chp = chpid_to_chp(cssid_chpid);
807 if (!chp)
808 return -ENODEV;
809
810 mutex_lock(&chp->lock);
811 if (chp->desc_fmt1.flags & 0x10)
812 *chid = chp->desc_fmt1.chid;
813 else
814 rc = -ENODEV;
815 mutex_unlock(&chp->lock);
816
817 return rc;
818}
819EXPORT_SYMBOL_GPL(ccw_device_get_chid);
820
821
822
823
824
825
826void *ccw_device_dma_zalloc(struct ccw_device *cdev, size_t size)
827{
828 void *addr;
829
830 if (!get_device(&cdev->dev))
831 return NULL;
832 addr = cio_gp_dma_zalloc(cdev->private->dma_pool, &cdev->dev, size);
833 if (IS_ERR_OR_NULL(addr))
834 put_device(&cdev->dev);
835 return addr;
836}
837EXPORT_SYMBOL(ccw_device_dma_zalloc);
838
839void ccw_device_dma_free(struct ccw_device *cdev, void *cpu_addr, size_t size)
840{
841 if (!cpu_addr)
842 return;
843 cio_gp_dma_free(cdev->private->dma_pool, cpu_addr, size);
844 put_device(&cdev->dev);
845}
846EXPORT_SYMBOL(ccw_device_dma_free);
847
848EXPORT_SYMBOL(ccw_device_set_options_mask);
849EXPORT_SYMBOL(ccw_device_set_options);
850EXPORT_SYMBOL(ccw_device_clear_options);
851EXPORT_SYMBOL(ccw_device_clear);
852EXPORT_SYMBOL(ccw_device_halt);
853EXPORT_SYMBOL(ccw_device_resume);
854EXPORT_SYMBOL(ccw_device_start_timeout);
855EXPORT_SYMBOL(ccw_device_start);
856EXPORT_SYMBOL(ccw_device_start_timeout_key);
857EXPORT_SYMBOL(ccw_device_start_key);
858EXPORT_SYMBOL(ccw_device_get_ciw);
859EXPORT_SYMBOL(ccw_device_get_path_mask);
860EXPORT_SYMBOL_GPL(ccw_device_get_chp_desc);
861EXPORT_SYMBOL_GPL(ccw_device_get_util_str);
862