1 // SPDX-License-Identifier: GPL-2.0
3 * DIAGNOSE X'2C4' instruction based HMC FTP services, useable on z/VM
5 * Copyright IBM Corp. 2013
10 #define KMSG_COMPONENT "hmcdrv"
11 #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
13 #include <linux/kernel.h>
15 #include <linux/irq.h>
16 #include <linux/wait.h>
17 #include <linux/string.h>
18 #include <asm/asm-extable.h>
19 #include <asm/ctlreg.h>
22 #include "hmcdrv_ftp.h"
25 /* DIAGNOSE X'2C4' return codes in Ry */
26 #define DIAG_FTP_RET_OK 0 /* HMC FTP started successfully */
27 #define DIAG_FTP_RET_EBUSY 4 /* HMC FTP service currently busy */
28 #define DIAG_FTP_RET_EIO 8 /* HMC FTP service I/O error */
29 /* and an artificial extension */
30 #define DIAG_FTP_RET_EPERM 2 /* HMC FTP service privilege error */
32 /* FTP service status codes (after INTR at guest real location 133) */
33 #define DIAG_FTP_STAT_OK 0U /* request completed successfully */
34 #define DIAG_FTP_STAT_PGCC 4U /* program check condition */
35 #define DIAG_FTP_STAT_PGIOE 8U /* paging I/O error */
36 #define DIAG_FTP_STAT_TIMEOUT 12U /* timeout */
37 #define DIAG_FTP_STAT_EBASE 16U /* base of error codes from SCLP */
38 #define DIAG_FTP_STAT_LDFAIL (DIAG_FTP_STAT_EBASE + 1U) /* failed */
39 #define DIAG_FTP_STAT_LDNPERM (DIAG_FTP_STAT_EBASE + 2U) /* not allowed */
40 #define DIAG_FTP_STAT_LDRUNS (DIAG_FTP_STAT_EBASE + 3U) /* runs */
41 #define DIAG_FTP_STAT_LDNRUNS (DIAG_FTP_STAT_EBASE + 4U) /* not runs */
44 * struct diag_ftp_ldfpl - load file FTP parameter list (LDFPL)
45 * @bufaddr: real buffer address (at 4k boundary)
46 * @buflen: length of buffer
47 * @offset: dir/file offset
48 * @intparm: interruption parameter (unused)
49 * @transferred: bytes transferred
50 * @fsize: file size, filled on GET
51 * @failaddr: failing address
53 * @fident: file name - ASCII
55 struct diag_ftp_ldfpl {
64 u8 fident[HMCDRV_FTP_FIDENT_MAX];
67 static DECLARE_COMPLETION(diag_ftp_rx_complete);
68 static int diag_ftp_subcode;
71 * diag_ftp_handler() - FTP services IRQ handler
72 * @extirq: external interrupt (sub-) code
73 * @param32: 32-bit interruption parameter from &struct diag_ftp_ldfpl
74 * @param64: unused (for 64-bit interrupt parameters)
76 static void diag_ftp_handler(struct ext_code extirq,
78 unsigned long param64)
80 if ((extirq.subcode >> 8) != 8)
81 return; /* not a FTP services sub-code */
83 inc_irq_stat(IRQEXT_FTP);
84 diag_ftp_subcode = extirq.subcode & 0xffU;
85 complete(&diag_ftp_rx_complete);
89 * diag_ftp_2c4() - DIAGNOSE X'2C4' service call
90 * @fpl: pointer to prepared LDFPL
91 * @cmd: FTP command to be executed
93 * Performs a DIAGNOSE X'2C4' call with (input/output) FTP parameter list
94 * @fpl and FTP function code @cmd. In case of an error the function does
95 * nothing and returns an (negative) error code.
98 * 1. This function only initiates a transfer, so the caller must wait
99 * for completion (asynchronous execution).
100 * 2. The FTP parameter list @fpl must be aligned to a double-word boundary.
101 * 3. fpl->bufaddr must be a real address, 4k aligned
103 static int diag_ftp_2c4(struct diag_ftp_ldfpl *fpl,
104 enum hmcdrv_ftp_cmdid cmd)
108 diag_stat_inc(DIAG_STAT_X2C4);
110 " diag %[addr],%[cmd],0x2c4\n"
112 "1: la %[rc],%[err]\n"
115 : [rc] "=d" (rc), "+m" (*fpl)
116 : [cmd] "0" (cmd), [addr] "d" (virt_to_phys(fpl)),
117 [err] "i" (DIAG_FTP_RET_EPERM)
121 case DIAG_FTP_RET_OK:
123 case DIAG_FTP_RET_EBUSY:
125 case DIAG_FTP_RET_EPERM:
127 case DIAG_FTP_RET_EIO:
134 * diag_ftp_cmd() - executes a DIAG X'2C4' FTP command, targeting a HMC
135 * @ftp: pointer to FTP command specification
136 * @fsize: return of file size (or NULL if undesirable)
138 * Attention: Notice that this function is not reentrant - so the caller
139 * must ensure locking.
141 * Return: number of bytes read/written or a (negative) error code
143 ssize_t diag_ftp_cmd(const struct hmcdrv_ftp_cmdspec *ftp, size_t *fsize)
145 struct diag_ftp_ldfpl *ldfpl;
148 unsigned long start_jiffies;
150 pr_debug("starting DIAG X'2C4' on '%s', requesting %zd bytes\n",
151 ftp->fname, ftp->len);
152 start_jiffies = jiffies;
154 init_completion(&diag_ftp_rx_complete);
156 ldfpl = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
162 len = strscpy(ldfpl->fident, ftp->fname, sizeof(ldfpl->fident));
168 ldfpl->transferred = 0;
170 ldfpl->offset = ftp->ofs;
171 ldfpl->buflen = ftp->len;
172 ldfpl->bufaddr = virt_to_phys(ftp->buf);
174 len = diag_ftp_2c4(ldfpl, ftp->id);
179 * There is no way to cancel the running diag X'2C4', the code
180 * needs to wait unconditionally until the transfer is complete.
182 wait_for_completion(&diag_ftp_rx_complete);
185 pr_debug("completed DIAG X'2C4' after %lu ms\n",
186 (jiffies - start_jiffies) * 1000 / HZ);
187 pr_debug("status of DIAG X'2C4' is %u, with %lld/%lld bytes\n",
188 diag_ftp_subcode, ldfpl->transferred, ldfpl->fsize);
191 switch (diag_ftp_subcode) {
192 case DIAG_FTP_STAT_OK: /* success */
193 len = ldfpl->transferred;
195 *fsize = ldfpl->fsize;
197 case DIAG_FTP_STAT_LDNPERM:
200 case DIAG_FTP_STAT_LDRUNS:
203 case DIAG_FTP_STAT_LDFAIL:
204 len = -ENOENT; /* no such file or media */
212 free_page((unsigned long) ldfpl);
218 * diag_ftp_startup() - startup of FTP services, when running on z/VM
220 * Return: 0 on success, else an (negative) error code
222 int diag_ftp_startup(void)
226 rc = register_external_irq(EXT_IRQ_CP_SERVICE, diag_ftp_handler);
230 irq_subclass_register(IRQ_SUBCLASS_SERVICE_SIGNAL);
235 * diag_ftp_shutdown() - shutdown of FTP services, when running on z/VM
237 void diag_ftp_shutdown(void)
239 irq_subclass_unregister(IRQ_SUBCLASS_SERVICE_SIGNAL);
240 unregister_external_irq(EXT_IRQ_CP_SERVICE, diag_ftp_handler);