xref: /linux/drivers/s390/char/sclp_cmd.c (revision d0b73b488c55df905ea8faaad079f8535629ed26)
1 /*
2  * Copyright IBM Corp. 2007,2012
3  *
4  * Author(s): Heiko Carstens <heiko.carstens@de.ibm.com>,
5  *	      Peter Oberparleiter <peter.oberparleiter@de.ibm.com>
6  */
7 
8 #define KMSG_COMPONENT "sclp_cmd"
9 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
10 
11 #include <linux/completion.h>
12 #include <linux/init.h>
13 #include <linux/errno.h>
14 #include <linux/err.h>
15 #include <linux/export.h>
16 #include <linux/slab.h>
17 #include <linux/string.h>
18 #include <linux/mm.h>
19 #include <linux/mmzone.h>
20 #include <linux/memory.h>
21 #include <linux/module.h>
22 #include <linux/platform_device.h>
23 #include <asm/ctl_reg.h>
24 #include <asm/chpid.h>
25 #include <asm/setup.h>
26 #include <asm/page.h>
27 #include <asm/sclp.h>
28 
29 #include "sclp.h"
30 
31 #define SCLP_CMDW_READ_SCP_INFO		0x00020001
32 #define SCLP_CMDW_READ_SCP_INFO_FORCED	0x00120001
33 
34 struct read_info_sccb {
35 	struct	sccb_header header;	/* 0-7 */
36 	u16	rnmax;			/* 8-9 */
37 	u8	rnsize;			/* 10 */
38 	u8	_reserved0[24 - 11];	/* 11-15 */
39 	u8	loadparm[8];		/* 24-31 */
40 	u8	_reserved1[48 - 32];	/* 32-47 */
41 	u64	facilities;		/* 48-55 */
42 	u8	_reserved2[84 - 56];	/* 56-83 */
43 	u8	fac84;			/* 84 */
44 	u8	fac85;			/* 85 */
45 	u8	_reserved3[91 - 86];	/* 86-90 */
46 	u8	flags;			/* 91 */
47 	u8	_reserved4[100 - 92];	/* 92-99 */
48 	u32	rnsize2;		/* 100-103 */
49 	u64	rnmax2;			/* 104-111 */
50 	u8	_reserved5[4096 - 112];	/* 112-4095 */
51 } __attribute__((packed, aligned(PAGE_SIZE)));
52 
53 static struct init_sccb __initdata early_event_mask_sccb __aligned(PAGE_SIZE);
54 static struct read_info_sccb __initdata early_read_info_sccb;
55 static int __initdata early_read_info_sccb_valid;
56 
57 u64 sclp_facilities;
58 static u8 sclp_fac84;
59 static u8 sclp_fac85;
60 static unsigned long long rzm;
61 static unsigned long long rnmax;
62 
63 static int __init sclp_cmd_sync_early(sclp_cmdw_t cmd, void *sccb)
64 {
65 	int rc;
66 
67 	__ctl_set_bit(0, 9);
68 	rc = sclp_service_call(cmd, sccb);
69 	if (rc)
70 		goto out;
71 	__load_psw_mask(PSW_DEFAULT_KEY | PSW_MASK_BASE | PSW_MASK_EA |
72 			PSW_MASK_BA | PSW_MASK_EXT | PSW_MASK_WAIT);
73 	local_irq_disable();
74 out:
75 	/* Contents of the sccb might have changed. */
76 	barrier();
77 	__ctl_clear_bit(0, 9);
78 	return rc;
79 }
80 
81 static void __init sclp_read_info_early(void)
82 {
83 	int rc;
84 	int i;
85 	struct read_info_sccb *sccb;
86 	sclp_cmdw_t commands[] = {SCLP_CMDW_READ_SCP_INFO_FORCED,
87 				  SCLP_CMDW_READ_SCP_INFO};
88 
89 	sccb = &early_read_info_sccb;
90 	for (i = 0; i < ARRAY_SIZE(commands); i++) {
91 		do {
92 			memset(sccb, 0, sizeof(*sccb));
93 			sccb->header.length = sizeof(*sccb);
94 			sccb->header.function_code = 0x80;
95 			sccb->header.control_mask[2] = 0x80;
96 			rc = sclp_cmd_sync_early(commands[i], sccb);
97 		} while (rc == -EBUSY);
98 
99 		if (rc)
100 			break;
101 		if (sccb->header.response_code == 0x10) {
102 			early_read_info_sccb_valid = 1;
103 			break;
104 		}
105 		if (sccb->header.response_code != 0x1f0)
106 			break;
107 	}
108 }
109 
110 static void __init sclp_event_mask_early(void)
111 {
112 	struct init_sccb *sccb = &early_event_mask_sccb;
113 	int rc;
114 
115 	do {
116 		memset(sccb, 0, sizeof(*sccb));
117 		sccb->header.length = sizeof(*sccb);
118 		sccb->mask_length = sizeof(sccb_mask_t);
119 		rc = sclp_cmd_sync_early(SCLP_CMDW_WRITE_EVENT_MASK, sccb);
120 	} while (rc == -EBUSY);
121 }
122 
123 void __init sclp_facilities_detect(void)
124 {
125 	struct read_info_sccb *sccb;
126 
127 	sclp_read_info_early();
128 	if (!early_read_info_sccb_valid)
129 		return;
130 
131 	sccb = &early_read_info_sccb;
132 	sclp_facilities = sccb->facilities;
133 	sclp_fac84 = sccb->fac84;
134 	sclp_fac85 = sccb->fac85;
135 	rnmax = sccb->rnmax ? sccb->rnmax : sccb->rnmax2;
136 	rzm = sccb->rnsize ? sccb->rnsize : sccb->rnsize2;
137 	rzm <<= 20;
138 
139 	sclp_event_mask_early();
140 }
141 
142 bool __init sclp_has_linemode(void)
143 {
144 	struct init_sccb *sccb = &early_event_mask_sccb;
145 
146 	if (sccb->header.response_code != 0x20)
147 		return 0;
148 	if (sccb->sclp_send_mask & (EVTYP_MSG_MASK | EVTYP_PMSGCMD_MASK))
149 		return 1;
150 	return 0;
151 }
152 
153 bool __init sclp_has_vt220(void)
154 {
155 	struct init_sccb *sccb = &early_event_mask_sccb;
156 
157 	if (sccb->header.response_code != 0x20)
158 		return 0;
159 	if (sccb->sclp_send_mask & EVTYP_VT220MSG_MASK)
160 		return 1;
161 	return 0;
162 }
163 
164 unsigned long long sclp_get_rnmax(void)
165 {
166 	return rnmax;
167 }
168 
169 unsigned long long sclp_get_rzm(void)
170 {
171 	return rzm;
172 }
173 
174 u8 sclp_get_fac85(void)
175 {
176 	return sclp_fac85;
177 }
178 EXPORT_SYMBOL_GPL(sclp_get_fac85);
179 
180 /*
181  * This function will be called after sclp_facilities_detect(), which gets
182  * called from early.c code. Therefore the sccb should have valid contents.
183  */
184 void __init sclp_get_ipl_info(struct sclp_ipl_info *info)
185 {
186 	struct read_info_sccb *sccb;
187 
188 	if (!early_read_info_sccb_valid)
189 		return;
190 	sccb = &early_read_info_sccb;
191 	info->is_valid = 1;
192 	if (sccb->flags & 0x2)
193 		info->has_dump = 1;
194 	memcpy(&info->loadparm, &sccb->loadparm, LOADPARM_LEN);
195 }
196 
197 static void sclp_sync_callback(struct sclp_req *req, void *data)
198 {
199 	struct completion *completion = data;
200 
201 	complete(completion);
202 }
203 
204 static int do_sync_request(sclp_cmdw_t cmd, void *sccb)
205 {
206 	struct completion completion;
207 	struct sclp_req *request;
208 	int rc;
209 
210 	request = kzalloc(sizeof(*request), GFP_KERNEL);
211 	if (!request)
212 		return -ENOMEM;
213 	request->command = cmd;
214 	request->sccb = sccb;
215 	request->status = SCLP_REQ_FILLED;
216 	request->callback = sclp_sync_callback;
217 	request->callback_data = &completion;
218 	init_completion(&completion);
219 
220 	/* Perform sclp request. */
221 	rc = sclp_add_request(request);
222 	if (rc)
223 		goto out;
224 	wait_for_completion(&completion);
225 
226 	/* Check response. */
227 	if (request->status != SCLP_REQ_DONE) {
228 		pr_warning("sync request failed (cmd=0x%08x, "
229 			   "status=0x%02x)\n", cmd, request->status);
230 		rc = -EIO;
231 	}
232 out:
233 	kfree(request);
234 	return rc;
235 }
236 
237 /*
238  * CPU configuration related functions.
239  */
240 
241 #define SCLP_CMDW_READ_CPU_INFO		0x00010001
242 #define SCLP_CMDW_CONFIGURE_CPU		0x00110001
243 #define SCLP_CMDW_DECONFIGURE_CPU	0x00100001
244 
245 struct read_cpu_info_sccb {
246 	struct	sccb_header header;
247 	u16	nr_configured;
248 	u16	offset_configured;
249 	u16	nr_standby;
250 	u16	offset_standby;
251 	u8	reserved[4096 - 16];
252 } __attribute__((packed, aligned(PAGE_SIZE)));
253 
254 static void sclp_fill_cpu_info(struct sclp_cpu_info *info,
255 			       struct read_cpu_info_sccb *sccb)
256 {
257 	char *page = (char *) sccb;
258 
259 	memset(info, 0, sizeof(*info));
260 	info->configured = sccb->nr_configured;
261 	info->standby = sccb->nr_standby;
262 	info->combined = sccb->nr_configured + sccb->nr_standby;
263 	info->has_cpu_type = sclp_fac84 & 0x1;
264 	memcpy(&info->cpu, page + sccb->offset_configured,
265 	       info->combined * sizeof(struct sclp_cpu_entry));
266 }
267 
268 int sclp_get_cpu_info(struct sclp_cpu_info *info)
269 {
270 	int rc;
271 	struct read_cpu_info_sccb *sccb;
272 
273 	if (!SCLP_HAS_CPU_INFO)
274 		return -EOPNOTSUPP;
275 	sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
276 	if (!sccb)
277 		return -ENOMEM;
278 	sccb->header.length = sizeof(*sccb);
279 	rc = do_sync_request(SCLP_CMDW_READ_CPU_INFO, sccb);
280 	if (rc)
281 		goto out;
282 	if (sccb->header.response_code != 0x0010) {
283 		pr_warning("readcpuinfo failed (response=0x%04x)\n",
284 			   sccb->header.response_code);
285 		rc = -EIO;
286 		goto out;
287 	}
288 	sclp_fill_cpu_info(info, sccb);
289 out:
290 	free_page((unsigned long) sccb);
291 	return rc;
292 }
293 
294 struct cpu_configure_sccb {
295 	struct sccb_header header;
296 } __attribute__((packed, aligned(8)));
297 
298 static int do_cpu_configure(sclp_cmdw_t cmd)
299 {
300 	struct cpu_configure_sccb *sccb;
301 	int rc;
302 
303 	if (!SCLP_HAS_CPU_RECONFIG)
304 		return -EOPNOTSUPP;
305 	/*
306 	 * This is not going to cross a page boundary since we force
307 	 * kmalloc to have a minimum alignment of 8 bytes on s390.
308 	 */
309 	sccb = kzalloc(sizeof(*sccb), GFP_KERNEL | GFP_DMA);
310 	if (!sccb)
311 		return -ENOMEM;
312 	sccb->header.length = sizeof(*sccb);
313 	rc = do_sync_request(cmd, sccb);
314 	if (rc)
315 		goto out;
316 	switch (sccb->header.response_code) {
317 	case 0x0020:
318 	case 0x0120:
319 		break;
320 	default:
321 		pr_warning("configure cpu failed (cmd=0x%08x, "
322 			   "response=0x%04x)\n", cmd,
323 			   sccb->header.response_code);
324 		rc = -EIO;
325 		break;
326 	}
327 out:
328 	kfree(sccb);
329 	return rc;
330 }
331 
332 int sclp_cpu_configure(u8 cpu)
333 {
334 	return do_cpu_configure(SCLP_CMDW_CONFIGURE_CPU | cpu << 8);
335 }
336 
337 int sclp_cpu_deconfigure(u8 cpu)
338 {
339 	return do_cpu_configure(SCLP_CMDW_DECONFIGURE_CPU | cpu << 8);
340 }
341 
342 #ifdef CONFIG_MEMORY_HOTPLUG
343 
344 static DEFINE_MUTEX(sclp_mem_mutex);
345 static LIST_HEAD(sclp_mem_list);
346 static u8 sclp_max_storage_id;
347 static unsigned long sclp_storage_ids[256 / BITS_PER_LONG];
348 static int sclp_mem_state_changed;
349 
350 struct memory_increment {
351 	struct list_head list;
352 	u16 rn;
353 	int standby;
354 	int usecount;
355 };
356 
357 struct assign_storage_sccb {
358 	struct sccb_header header;
359 	u16 rn;
360 } __packed;
361 
362 int arch_get_memory_phys_device(unsigned long start_pfn)
363 {
364 	if (!rzm)
365 		return 0;
366 	return PFN_PHYS(start_pfn) >> ilog2(rzm);
367 }
368 
369 static unsigned long long rn2addr(u16 rn)
370 {
371 	return (unsigned long long) (rn - 1) * rzm;
372 }
373 
374 static int do_assign_storage(sclp_cmdw_t cmd, u16 rn)
375 {
376 	struct assign_storage_sccb *sccb;
377 	int rc;
378 
379 	sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
380 	if (!sccb)
381 		return -ENOMEM;
382 	sccb->header.length = PAGE_SIZE;
383 	sccb->rn = rn;
384 	rc = do_sync_request(cmd, sccb);
385 	if (rc)
386 		goto out;
387 	switch (sccb->header.response_code) {
388 	case 0x0020:
389 	case 0x0120:
390 		break;
391 	default:
392 		pr_warning("assign storage failed (cmd=0x%08x, "
393 			   "response=0x%04x, rn=0x%04x)\n", cmd,
394 			   sccb->header.response_code, rn);
395 		rc = -EIO;
396 		break;
397 	}
398 out:
399 	free_page((unsigned long) sccb);
400 	return rc;
401 }
402 
403 static int sclp_assign_storage(u16 rn)
404 {
405 	unsigned long long start;
406 	int rc;
407 
408 	rc = do_assign_storage(0x000d0001, rn);
409 	if (rc)
410 		return rc;
411 	start = rn2addr(rn);
412 	storage_key_init_range(start, start + rzm);
413 	return 0;
414 }
415 
416 static int sclp_unassign_storage(u16 rn)
417 {
418 	return do_assign_storage(0x000c0001, rn);
419 }
420 
421 struct attach_storage_sccb {
422 	struct sccb_header header;
423 	u16 :16;
424 	u16 assigned;
425 	u32 :32;
426 	u32 entries[0];
427 } __packed;
428 
429 static int sclp_attach_storage(u8 id)
430 {
431 	struct attach_storage_sccb *sccb;
432 	int rc;
433 	int i;
434 
435 	sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
436 	if (!sccb)
437 		return -ENOMEM;
438 	sccb->header.length = PAGE_SIZE;
439 	rc = do_sync_request(0x00080001 | id << 8, sccb);
440 	if (rc)
441 		goto out;
442 	switch (sccb->header.response_code) {
443 	case 0x0020:
444 		set_bit(id, sclp_storage_ids);
445 		for (i = 0; i < sccb->assigned; i++) {
446 			if (sccb->entries[i])
447 				sclp_unassign_storage(sccb->entries[i] >> 16);
448 		}
449 		break;
450 	default:
451 		rc = -EIO;
452 		break;
453 	}
454 out:
455 	free_page((unsigned long) sccb);
456 	return rc;
457 }
458 
459 static int sclp_mem_change_state(unsigned long start, unsigned long size,
460 				 int online)
461 {
462 	struct memory_increment *incr;
463 	unsigned long long istart;
464 	int rc = 0;
465 
466 	list_for_each_entry(incr, &sclp_mem_list, list) {
467 		istart = rn2addr(incr->rn);
468 		if (start + size - 1 < istart)
469 			break;
470 		if (start > istart + rzm - 1)
471 			continue;
472 		if (online) {
473 			if (incr->usecount++)
474 				continue;
475 			/*
476 			 * Don't break the loop if one assign fails. Loop may
477 			 * be walked again on CANCEL and we can't save
478 			 * information if state changed before or not.
479 			 * So continue and increase usecount for all increments.
480 			 */
481 			rc |= sclp_assign_storage(incr->rn);
482 		} else {
483 			if (--incr->usecount)
484 				continue;
485 			sclp_unassign_storage(incr->rn);
486 		}
487 	}
488 	return rc ? -EIO : 0;
489 }
490 
491 static int sclp_mem_notifier(struct notifier_block *nb,
492 			     unsigned long action, void *data)
493 {
494 	unsigned long start, size;
495 	struct memory_notify *arg;
496 	unsigned char id;
497 	int rc = 0;
498 
499 	arg = data;
500 	start = arg->start_pfn << PAGE_SHIFT;
501 	size = arg->nr_pages << PAGE_SHIFT;
502 	mutex_lock(&sclp_mem_mutex);
503 	for_each_clear_bit(id, sclp_storage_ids, sclp_max_storage_id + 1)
504 		sclp_attach_storage(id);
505 	switch (action) {
506 	case MEM_ONLINE:
507 	case MEM_GOING_OFFLINE:
508 	case MEM_CANCEL_OFFLINE:
509 		break;
510 	case MEM_GOING_ONLINE:
511 		rc = sclp_mem_change_state(start, size, 1);
512 		break;
513 	case MEM_CANCEL_ONLINE:
514 		sclp_mem_change_state(start, size, 0);
515 		break;
516 	case MEM_OFFLINE:
517 		sclp_mem_change_state(start, size, 0);
518 		break;
519 	default:
520 		rc = -EINVAL;
521 		break;
522 	}
523 	if (!rc)
524 		sclp_mem_state_changed = 1;
525 	mutex_unlock(&sclp_mem_mutex);
526 	return rc ? NOTIFY_BAD : NOTIFY_OK;
527 }
528 
529 static struct notifier_block sclp_mem_nb = {
530 	.notifier_call = sclp_mem_notifier,
531 };
532 
533 static void __init add_memory_merged(u16 rn)
534 {
535 	static u16 first_rn, num;
536 	unsigned long long start, size;
537 
538 	if (rn && first_rn && (first_rn + num == rn)) {
539 		num++;
540 		return;
541 	}
542 	if (!first_rn)
543 		goto skip_add;
544 	start = rn2addr(first_rn);
545 	size = (unsigned long long ) num * rzm;
546 	if (start >= VMEM_MAX_PHYS)
547 		goto skip_add;
548 	if (start + size > VMEM_MAX_PHYS)
549 		size = VMEM_MAX_PHYS - start;
550 	if (memory_end_set && (start >= memory_end))
551 		goto skip_add;
552 	if (memory_end_set && (start + size > memory_end))
553 		size = memory_end - start;
554 	add_memory(0, start, size);
555 skip_add:
556 	first_rn = rn;
557 	num = 1;
558 }
559 
560 static void __init sclp_add_standby_memory(void)
561 {
562 	struct memory_increment *incr;
563 
564 	list_for_each_entry(incr, &sclp_mem_list, list)
565 		if (incr->standby)
566 			add_memory_merged(incr->rn);
567 	add_memory_merged(0);
568 }
569 
570 static void __init insert_increment(u16 rn, int standby, int assigned)
571 {
572 	struct memory_increment *incr, *new_incr;
573 	struct list_head *prev;
574 	u16 last_rn;
575 
576 	new_incr = kzalloc(sizeof(*new_incr), GFP_KERNEL);
577 	if (!new_incr)
578 		return;
579 	new_incr->rn = rn;
580 	new_incr->standby = standby;
581 	if (!standby)
582 		new_incr->usecount = 1;
583 	last_rn = 0;
584 	prev = &sclp_mem_list;
585 	list_for_each_entry(incr, &sclp_mem_list, list) {
586 		if (assigned && incr->rn > rn)
587 			break;
588 		if (!assigned && incr->rn - last_rn > 1)
589 			break;
590 		last_rn = incr->rn;
591 		prev = &incr->list;
592 	}
593 	if (!assigned)
594 		new_incr->rn = last_rn + 1;
595 	if (new_incr->rn > rnmax) {
596 		kfree(new_incr);
597 		return;
598 	}
599 	list_add(&new_incr->list, prev);
600 }
601 
602 static int sclp_mem_freeze(struct device *dev)
603 {
604 	if (!sclp_mem_state_changed)
605 		return 0;
606 	pr_err("Memory hotplug state changed, suspend refused.\n");
607 	return -EPERM;
608 }
609 
610 struct read_storage_sccb {
611 	struct sccb_header header;
612 	u16 max_id;
613 	u16 assigned;
614 	u16 standby;
615 	u16 :16;
616 	u32 entries[0];
617 } __packed;
618 
619 static const struct dev_pm_ops sclp_mem_pm_ops = {
620 	.freeze		= sclp_mem_freeze,
621 };
622 
623 static struct platform_driver sclp_mem_pdrv = {
624 	.driver = {
625 		.name	= "sclp_mem",
626 		.pm	= &sclp_mem_pm_ops,
627 	},
628 };
629 
630 static int __init sclp_detect_standby_memory(void)
631 {
632 	struct platform_device *sclp_pdev;
633 	struct read_storage_sccb *sccb;
634 	int i, id, assigned, rc;
635 
636 	if (!early_read_info_sccb_valid)
637 		return 0;
638 	if ((sclp_facilities & 0xe00000000000ULL) != 0xe00000000000ULL)
639 		return 0;
640 	rc = -ENOMEM;
641 	sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
642 	if (!sccb)
643 		goto out;
644 	assigned = 0;
645 	for (id = 0; id <= sclp_max_storage_id; id++) {
646 		memset(sccb, 0, PAGE_SIZE);
647 		sccb->header.length = PAGE_SIZE;
648 		rc = do_sync_request(0x00040001 | id << 8, sccb);
649 		if (rc)
650 			goto out;
651 		switch (sccb->header.response_code) {
652 		case 0x0010:
653 			set_bit(id, sclp_storage_ids);
654 			for (i = 0; i < sccb->assigned; i++) {
655 				if (!sccb->entries[i])
656 					continue;
657 				assigned++;
658 				insert_increment(sccb->entries[i] >> 16, 0, 1);
659 			}
660 			break;
661 		case 0x0310:
662 			break;
663 		case 0x0410:
664 			for (i = 0; i < sccb->assigned; i++) {
665 				if (!sccb->entries[i])
666 					continue;
667 				assigned++;
668 				insert_increment(sccb->entries[i] >> 16, 1, 1);
669 			}
670 			break;
671 		default:
672 			rc = -EIO;
673 			break;
674 		}
675 		if (!rc)
676 			sclp_max_storage_id = sccb->max_id;
677 	}
678 	if (rc || list_empty(&sclp_mem_list))
679 		goto out;
680 	for (i = 1; i <= rnmax - assigned; i++)
681 		insert_increment(0, 1, 0);
682 	rc = register_memory_notifier(&sclp_mem_nb);
683 	if (rc)
684 		goto out;
685 	rc = platform_driver_register(&sclp_mem_pdrv);
686 	if (rc)
687 		goto out;
688 	sclp_pdev = platform_device_register_simple("sclp_mem", -1, NULL, 0);
689 	rc = IS_ERR(sclp_pdev) ? PTR_ERR(sclp_pdev) : 0;
690 	if (rc)
691 		goto out_driver;
692 	sclp_add_standby_memory();
693 	goto out;
694 out_driver:
695 	platform_driver_unregister(&sclp_mem_pdrv);
696 out:
697 	free_page((unsigned long) sccb);
698 	return rc;
699 }
700 __initcall(sclp_detect_standby_memory);
701 
702 #endif /* CONFIG_MEMORY_HOTPLUG */
703 
704 /*
705  * PCI I/O adapter configuration related functions.
706  */
707 #define SCLP_CMDW_CONFIGURE_PCI			0x001a0001
708 #define SCLP_CMDW_DECONFIGURE_PCI		0x001b0001
709 
710 #define SCLP_RECONFIG_PCI_ATPYE			2
711 
712 struct pci_cfg_sccb {
713 	struct sccb_header header;
714 	u8 atype;		/* adapter type */
715 	u8 reserved1;
716 	u16 reserved2;
717 	u32 aid;		/* adapter identifier */
718 } __packed;
719 
720 static int do_pci_configure(sclp_cmdw_t cmd, u32 fid)
721 {
722 	struct pci_cfg_sccb *sccb;
723 	int rc;
724 
725 	if (!SCLP_HAS_PCI_RECONFIG)
726 		return -EOPNOTSUPP;
727 
728 	sccb = (struct pci_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
729 	if (!sccb)
730 		return -ENOMEM;
731 
732 	sccb->header.length = PAGE_SIZE;
733 	sccb->atype = SCLP_RECONFIG_PCI_ATPYE;
734 	sccb->aid = fid;
735 	rc = do_sync_request(cmd, sccb);
736 	if (rc)
737 		goto out;
738 	switch (sccb->header.response_code) {
739 	case 0x0020:
740 	case 0x0120:
741 		break;
742 	default:
743 		pr_warn("configure PCI I/O adapter failed: cmd=0x%08x  response=0x%04x\n",
744 			cmd, sccb->header.response_code);
745 		rc = -EIO;
746 		break;
747 	}
748 out:
749 	free_page((unsigned long) sccb);
750 	return rc;
751 }
752 
753 int sclp_pci_configure(u32 fid)
754 {
755 	return do_pci_configure(SCLP_CMDW_CONFIGURE_PCI, fid);
756 }
757 EXPORT_SYMBOL(sclp_pci_configure);
758 
759 int sclp_pci_deconfigure(u32 fid)
760 {
761 	return do_pci_configure(SCLP_CMDW_DECONFIGURE_PCI, fid);
762 }
763 EXPORT_SYMBOL(sclp_pci_deconfigure);
764 
765 /*
766  * Channel path configuration related functions.
767  */
768 
769 #define SCLP_CMDW_CONFIGURE_CHPATH		0x000f0001
770 #define SCLP_CMDW_DECONFIGURE_CHPATH		0x000e0001
771 #define SCLP_CMDW_READ_CHPATH_INFORMATION	0x00030001
772 
773 struct chp_cfg_sccb {
774 	struct sccb_header header;
775 	u8 ccm;
776 	u8 reserved[6];
777 	u8 cssid;
778 } __attribute__((packed));
779 
780 static int do_chp_configure(sclp_cmdw_t cmd)
781 {
782 	struct chp_cfg_sccb *sccb;
783 	int rc;
784 
785 	if (!SCLP_HAS_CHP_RECONFIG)
786 		return -EOPNOTSUPP;
787 	/* Prepare sccb. */
788 	sccb = (struct chp_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
789 	if (!sccb)
790 		return -ENOMEM;
791 	sccb->header.length = sizeof(*sccb);
792 	rc = do_sync_request(cmd, sccb);
793 	if (rc)
794 		goto out;
795 	switch (sccb->header.response_code) {
796 	case 0x0020:
797 	case 0x0120:
798 	case 0x0440:
799 	case 0x0450:
800 		break;
801 	default:
802 		pr_warning("configure channel-path failed "
803 			   "(cmd=0x%08x, response=0x%04x)\n", cmd,
804 			   sccb->header.response_code);
805 		rc = -EIO;
806 		break;
807 	}
808 out:
809 	free_page((unsigned long) sccb);
810 	return rc;
811 }
812 
813 /**
814  * sclp_chp_configure - perform configure channel-path sclp command
815  * @chpid: channel-path ID
816  *
817  * Perform configure channel-path command sclp command for specified chpid.
818  * Return 0 after command successfully finished, non-zero otherwise.
819  */
820 int sclp_chp_configure(struct chp_id chpid)
821 {
822 	return do_chp_configure(SCLP_CMDW_CONFIGURE_CHPATH | chpid.id << 8);
823 }
824 
825 /**
826  * sclp_chp_deconfigure - perform deconfigure channel-path sclp command
827  * @chpid: channel-path ID
828  *
829  * Perform deconfigure channel-path command sclp command for specified chpid
830  * and wait for completion. On success return 0. Return non-zero otherwise.
831  */
832 int sclp_chp_deconfigure(struct chp_id chpid)
833 {
834 	return do_chp_configure(SCLP_CMDW_DECONFIGURE_CHPATH | chpid.id << 8);
835 }
836 
837 struct chp_info_sccb {
838 	struct sccb_header header;
839 	u8 recognized[SCLP_CHP_INFO_MASK_SIZE];
840 	u8 standby[SCLP_CHP_INFO_MASK_SIZE];
841 	u8 configured[SCLP_CHP_INFO_MASK_SIZE];
842 	u8 ccm;
843 	u8 reserved[6];
844 	u8 cssid;
845 } __attribute__((packed));
846 
847 /**
848  * sclp_chp_read_info - perform read channel-path information sclp command
849  * @info: resulting channel-path information data
850  *
851  * Perform read channel-path information sclp command and wait for completion.
852  * On success, store channel-path information in @info and return 0. Return
853  * non-zero otherwise.
854  */
855 int sclp_chp_read_info(struct sclp_chp_info *info)
856 {
857 	struct chp_info_sccb *sccb;
858 	int rc;
859 
860 	if (!SCLP_HAS_CHP_INFO)
861 		return -EOPNOTSUPP;
862 	/* Prepare sccb. */
863 	sccb = (struct chp_info_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
864 	if (!sccb)
865 		return -ENOMEM;
866 	sccb->header.length = sizeof(*sccb);
867 	rc = do_sync_request(SCLP_CMDW_READ_CHPATH_INFORMATION, sccb);
868 	if (rc)
869 		goto out;
870 	if (sccb->header.response_code != 0x0010) {
871 		pr_warning("read channel-path info failed "
872 			   "(response=0x%04x)\n", sccb->header.response_code);
873 		rc = -EIO;
874 		goto out;
875 	}
876 	memcpy(info->recognized, sccb->recognized, SCLP_CHP_INFO_MASK_SIZE);
877 	memcpy(info->standby, sccb->standby, SCLP_CHP_INFO_MASK_SIZE);
878 	memcpy(info->configured, sccb->configured, SCLP_CHP_INFO_MASK_SIZE);
879 out:
880 	free_page((unsigned long) sccb);
881 	return rc;
882 }
883