1/*
2 *    DIAGNOSE X'2C4' instruction based HMC FTP services, useable on z/VM
3 *
4 *    Copyright IBM Corp. 2013
5 *    Author(s): Ralf Hoppe (rhoppe@de.ibm.com)
6 *
7 */
8
9#define KMSG_COMPONENT "hmcdrv"
10#define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
11
12#include <linux/kernel.h>
13#include <linux/mm.h>
14#include <linux/irq.h>
15#include <linux/wait.h>
16#include <linux/string.h>
17#include <asm/ctl_reg.h>
18
19#include "hmcdrv_ftp.h"
20#include "diag_ftp.h"
21
22/* DIAGNOSE X'2C4' return codes in Ry */
23#define DIAG_FTP_RET_OK	0 /* HMC FTP started successfully */
24#define DIAG_FTP_RET_EBUSY	4 /* HMC FTP service currently busy */
25#define DIAG_FTP_RET_EIO	8 /* HMC FTP service I/O error */
26/* and an artificial extension */
27#define DIAG_FTP_RET_EPERM	2 /* HMC FTP service privilege error */
28
29/* FTP service status codes (after INTR at guest real location 133) */
30#define DIAG_FTP_STAT_OK	0U /* request completed successfully */
31#define DIAG_FTP_STAT_PGCC	4U /* program check condition */
32#define DIAG_FTP_STAT_PGIOE	8U /* paging I/O error */
33#define DIAG_FTP_STAT_TIMEOUT	12U /* timeout */
34#define DIAG_FTP_STAT_EBASE	16U /* base of error codes from SCLP */
35#define DIAG_FTP_STAT_LDFAIL	(DIAG_FTP_STAT_EBASE + 1U) /* failed */
36#define DIAG_FTP_STAT_LDNPERM	(DIAG_FTP_STAT_EBASE + 2U) /* not allowed */
37#define DIAG_FTP_STAT_LDRUNS	(DIAG_FTP_STAT_EBASE + 3U) /* runs */
38#define DIAG_FTP_STAT_LDNRUNS	(DIAG_FTP_STAT_EBASE + 4U) /* not runs */
39
40/**
41 * struct diag_ftp_ldfpl - load file FTP parameter list (LDFPL)
42 * @bufaddr: real buffer address (at 4k boundary)
43 * @buflen: length of buffer
44 * @offset: dir/file offset
45 * @intparm: interruption parameter (unused)
46 * @transferred: bytes transferred
47 * @fsize: file size, filled on GET
48 * @failaddr: failing address
49 * @spare: padding
50 * @fident: file name - ASCII
51 */
52struct diag_ftp_ldfpl {
53	u64 bufaddr;
54	u64 buflen;
55	u64 offset;
56	u64 intparm;
57	u64 transferred;
58	u64 fsize;
59	u64 failaddr;
60	u64 spare;
61	u8 fident[HMCDRV_FTP_FIDENT_MAX];
62} __packed;
63
64static DECLARE_COMPLETION(diag_ftp_rx_complete);
65static int diag_ftp_subcode;
66
67/**
68 * diag_ftp_handler() - FTP services IRQ handler
69 * @extirq: external interrupt (sub-) code
70 * @param32: 32-bit interruption parameter from &struct diag_ftp_ldfpl
71 * @param64: unused (for 64-bit interrupt parameters)
72 */
73static void diag_ftp_handler(struct ext_code extirq,
74			     unsigned int param32,
75			     unsigned long param64)
76{
77	if ((extirq.subcode >> 8) != 8)
78		return; /* not a FTP services sub-code */
79
80	inc_irq_stat(IRQEXT_FTP);
81	diag_ftp_subcode = extirq.subcode & 0xffU;
82	complete(&diag_ftp_rx_complete);
83}
84
85/**
86 * diag_ftp_2c4() - DIAGNOSE X'2C4' service call
87 * @fpl: pointer to prepared LDFPL
88 * @cmd: FTP command to be executed
89 *
90 * Performs a DIAGNOSE X'2C4' call with (input/output) FTP parameter list
91 * @fpl and FTP function code @cmd. In case of an error the function does
92 * nothing and returns an (negative) error code.
93 *
94 * Notes:
95 * 1. This function only initiates a transfer, so the caller must wait
96 *    for completion (asynchronous execution).
97 * 2. The FTP parameter list @fpl must be aligned to a double-word boundary.
98 * 3. fpl->bufaddr must be a real address, 4k aligned
99 */
100static int diag_ftp_2c4(struct diag_ftp_ldfpl *fpl,
101			enum hmcdrv_ftp_cmdid cmd)
102{
103	int rc;
104
105	asm volatile(
106		"	diag	%[addr],%[cmd],0x2c4\n"
107		"0:	j	2f\n"
108		"1:	la	%[rc],%[err]\n"
109		"2:\n"
110		EX_TABLE(0b, 1b)
111		: [rc] "=d" (rc), "+m" (*fpl)
112		: [cmd] "0" (cmd), [addr] "d" (virt_to_phys(fpl)),
113		  [err] "i" (DIAG_FTP_RET_EPERM)
114		: "cc");
115
116	switch (rc) {
117	case DIAG_FTP_RET_OK:
118		return 0;
119	case DIAG_FTP_RET_EBUSY:
120		return -EBUSY;
121	case DIAG_FTP_RET_EPERM:
122		return -EPERM;
123	case DIAG_FTP_RET_EIO:
124	default:
125		return -EIO;
126	}
127}
128
129/**
130 * diag_ftp_cmd() - executes a DIAG X'2C4' FTP command, targeting a HMC
131 * @ftp: pointer to FTP command specification
132 * @fsize: return of file size (or NULL if undesirable)
133 *
134 * Attention: Notice that this function is not reentrant - so the caller
135 * must ensure locking.
136 *
137 * Return: number of bytes read/written or a (negative) error code
138 */
139ssize_t diag_ftp_cmd(const struct hmcdrv_ftp_cmdspec *ftp, size_t *fsize)
140{
141	struct diag_ftp_ldfpl *ldfpl;
142	ssize_t len;
143#ifdef DEBUG
144	unsigned long start_jiffies;
145
146	pr_debug("starting DIAG X'2C4' on '%s', requesting %zd bytes\n",
147		 ftp->fname, ftp->len);
148	start_jiffies = jiffies;
149#endif
150	init_completion(&diag_ftp_rx_complete);
151
152	ldfpl = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
153	if (!ldfpl) {
154		len = -ENOMEM;
155		goto out;
156	}
157
158	len = strlcpy(ldfpl->fident, ftp->fname, sizeof(ldfpl->fident));
159	if (len >= HMCDRV_FTP_FIDENT_MAX) {
160		len = -EINVAL;
161		goto out_free;
162	}
163
164	ldfpl->transferred = 0;
165	ldfpl->fsize = 0;
166	ldfpl->offset = ftp->ofs;
167	ldfpl->buflen = ftp->len;
168	ldfpl->bufaddr = virt_to_phys(ftp->buf);
169
170	len = diag_ftp_2c4(ldfpl, ftp->id);
171	if (len)
172		goto out_free;
173
174	/*
175	 * There is no way to cancel the running diag X'2C4', the code
176	 * needs to wait unconditionally until the transfer is complete.
177	 */
178	wait_for_completion(&diag_ftp_rx_complete);
179
180#ifdef DEBUG
181	pr_debug("completed DIAG X'2C4' after %lu ms\n",
182		 (jiffies - start_jiffies) * 1000 / HZ);
183	pr_debug("status of DIAG X'2C4' is %u, with %lld/%lld bytes\n",
184		 diag_ftp_subcode, ldfpl->transferred, ldfpl->fsize);
185#endif
186
187	switch (diag_ftp_subcode) {
188	case DIAG_FTP_STAT_OK: /* success */
189		len = ldfpl->transferred;
190		if (fsize)
191			*fsize = ldfpl->fsize;
192		break;
193	case DIAG_FTP_STAT_LDNPERM:
194		len = -EPERM;
195		break;
196	case DIAG_FTP_STAT_LDRUNS:
197		len = -EBUSY;
198		break;
199	case DIAG_FTP_STAT_LDFAIL:
200		len = -ENOENT; /* no such file or media */
201		break;
202	default:
203		len = -EIO;
204		break;
205	}
206
207out_free:
208	free_page((unsigned long) ldfpl);
209out:
210	return len;
211}
212
213/**
214 * diag_ftp_startup() - startup of FTP services, when running on z/VM
215 *
216 * Return: 0 on success, else an (negative) error code
217 */
218int diag_ftp_startup(void)
219{
220	int rc;
221
222	rc = register_external_irq(EXT_IRQ_CP_SERVICE, diag_ftp_handler);
223	if (rc)
224		return rc;
225
226	ctl_set_bit(0, 63 - 22);
227	return 0;
228}
229
230/**
231 * diag_ftp_shutdown() - shutdown of FTP services, when running on z/VM
232 */
233void diag_ftp_shutdown(void)
234{
235	ctl_clear_bit(0, 63 - 22);
236	unregister_external_irq(EXT_IRQ_CP_SERVICE, diag_ftp_handler);
237}
238