Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   3)  *    HMC Drive FTP Services
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   5)  *    Copyright IBM Corp. 2013
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   6)  *    Author(s): Ralf Hoppe (rhoppe@de.ibm.com)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   7)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   8) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   9) #define KMSG_COMPONENT "hmcdrv"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  10) #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  11) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  12) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  13) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  14) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  15) #include <linux/export.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  17) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  18) #include <linux/crc16.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  19) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  20) #include "hmcdrv_ftp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  21) #include "hmcdrv_cache.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  22) #include "sclp_ftp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  23) #include "diag_ftp.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  24) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  25) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  26)  * struct hmcdrv_ftp_ops - HMC drive FTP operations
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  27)  * @startup: startup function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  28)  * @shutdown: shutdown function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  29)  * @cmd: FTP transfer function
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  30)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  31) struct hmcdrv_ftp_ops {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  32) 	int (*startup)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  33) 	void (*shutdown)(void);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  34) 	ssize_t (*transfer)(const struct hmcdrv_ftp_cmdspec *ftp,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  35) 			    size_t *fsize);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  37) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  38) static enum hmcdrv_ftp_cmdid hmcdrv_ftp_cmd_getid(const char *cmd, int len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  39) static int hmcdrv_ftp_parse(char *cmd, struct hmcdrv_ftp_cmdspec *ftp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  40) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  41) static const struct hmcdrv_ftp_ops *hmcdrv_ftp_funcs; /* current operations */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  42) static DEFINE_MUTEX(hmcdrv_ftp_mutex); /* mutex for hmcdrv_ftp_funcs */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  43) static unsigned hmcdrv_ftp_refcnt; /* start/shutdown reference counter */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  44) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  45) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  46)  * hmcdrv_ftp_cmd_getid() - determine FTP command ID from a command string
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  47)  * @cmd: FTP command string (NOT zero-terminated)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  48)  * @len: length of FTP command string in @cmd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  49)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  50) static enum hmcdrv_ftp_cmdid hmcdrv_ftp_cmd_getid(const char *cmd, int len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  51) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  52) 	/* HMC FTP command descriptor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  53) 	struct hmcdrv_ftp_cmd_desc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  54) 		const char *str;	   /* command string */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  55) 		enum hmcdrv_ftp_cmdid cmd; /* associated command as enum */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  56) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  57) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  58) 	/* Description of all HMC drive FTP commands
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  59) 	 *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  60) 	 * Notes:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  61) 	 * 1. Array size should be a prime number.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  62) 	 * 2. Do not change the order of commands in table (because the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  63) 	 *    index is determined by CRC % ARRAY_SIZE).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  64) 	 * 3. Original command 'nlist' was renamed, else the CRC would
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  65) 	 *    collide with 'append' (see point 2).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  66) 	 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  67) 	static const struct hmcdrv_ftp_cmd_desc ftpcmds[7] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  68) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  69) 		{.str = "get", /* [0] get (CRC = 0x68eb) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  70) 		 .cmd = HMCDRV_FTP_GET},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  71) 		{.str = "dir", /* [1] dir (CRC = 0x6a9e) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  72) 		 .cmd = HMCDRV_FTP_DIR},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  73) 		{.str = "delete", /* [2] delete (CRC = 0x53ae) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  74) 		 .cmd = HMCDRV_FTP_DELETE},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  75) 		{.str = "nls", /* [3] nls (CRC = 0xf87c) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  76) 		 .cmd = HMCDRV_FTP_NLIST},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  77) 		{.str = "put", /* [4] put (CRC = 0xac56) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  78) 		 .cmd = HMCDRV_FTP_PUT},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  79) 		{.str = "append", /* [5] append (CRC = 0xf56e) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  80) 		 .cmd = HMCDRV_FTP_APPEND},
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  81) 		{.str = NULL} /* [6] unused */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  82) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  83) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  84) 	const struct hmcdrv_ftp_cmd_desc *pdesc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  85) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  86) 	u16 crc = 0xffffU;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  87) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  88) 	if (len == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  89) 		return HMCDRV_FTP_NOOP; /* error indiactor */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  91) 	crc = crc16(crc, cmd, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  92) 	pdesc = ftpcmds + (crc % ARRAY_SIZE(ftpcmds));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  93) 	pr_debug("FTP command '%s' has CRC 0x%04x, at table pos. %lu\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  94) 		 cmd, crc, (crc % ARRAY_SIZE(ftpcmds)));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  95) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  96) 	if (!pdesc->str || strncmp(pdesc->str, cmd, len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  97) 		return HMCDRV_FTP_NOOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  98) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  99) 	pr_debug("FTP command '%s' found, with ID %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) 		 pdesc->str, pdesc->cmd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) 	return pdesc->cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)  * hmcdrv_ftp_parse() - HMC drive FTP command parser
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107)  * @cmd: FTP command string "<cmd> <filename>"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)  * @ftp: Pointer to FTP command specification buffer (output)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)  * Return: 0 on success, else a (negative) error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) static int hmcdrv_ftp_parse(char *cmd, struct hmcdrv_ftp_cmdspec *ftp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) 	char *start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) 	int argc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) 	ftp->id = HMCDRV_FTP_NOOP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) 	ftp->fname = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) 	while (*cmd != '\0') {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) 		while (isspace(*cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) 			++cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) 		if (*cmd == '\0')
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) 		start = cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) 		switch (argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) 		case 0: /* 1st argument (FTP command) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) 			while ((*cmd != '\0') && !isspace(*cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) 				++cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) 			ftp->id = hmcdrv_ftp_cmd_getid(start, cmd - start);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) 		case 1: /* 2nd / last argument (rest of line) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) 			while ((*cmd != '\0') && !iscntrl(*cmd))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) 				++cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) 			ftp->fname = start;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) 			fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) 			*cmd = '\0';
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) 		} /* switch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) 		++argc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) 	} /* while */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) 	if (!ftp->fname || (ftp->id == HMCDRV_FTP_NOOP))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)  * hmcdrv_ftp_do() - perform a HMC drive FTP, with data from kernel-space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)  * @ftp: pointer to FTP command specification
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159)  * Return: number of bytes read/written or a negative error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) ssize_t hmcdrv_ftp_do(const struct hmcdrv_ftp_cmdspec *ftp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) 	ssize_t len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) 	mutex_lock(&hmcdrv_ftp_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) 	if (hmcdrv_ftp_funcs && hmcdrv_ftp_refcnt) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) 		pr_debug("starting transfer, cmd %d for '%s' at %lld with %zd bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) 			 ftp->id, ftp->fname, (long long) ftp->ofs, ftp->len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) 		len = hmcdrv_cache_cmd(ftp, hmcdrv_ftp_funcs->transfer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) 	} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) 		len = -ENXIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) 	mutex_unlock(&hmcdrv_ftp_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) 	return len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) EXPORT_SYMBOL(hmcdrv_ftp_do);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181)  * hmcdrv_ftp_probe() - probe for the HMC drive FTP service
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)  * Return: 0 if service is available, else an (negative) error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) int hmcdrv_ftp_probe(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) 	int rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) 	struct hmcdrv_ftp_cmdspec ftp = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) 		.id = HMCDRV_FTP_NOOP,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) 		.ofs = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) 		.fname = "",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) 		.len = PAGE_SIZE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) 	ftp.buf = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) 	if (!ftp.buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) 	rc = hmcdrv_ftp_startup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) 	if (rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) 		goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) 	rc = hmcdrv_ftp_do(&ftp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) 	hmcdrv_ftp_shutdown();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) 	switch (rc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) 	case -ENOENT: /* no such file/media or currently busy, */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) 	case -EBUSY:  /* but service seems to be available */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) 		rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) 	default: /* leave 'rc' as it is for [0, -EPERM, -E...] */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) 		if (rc > 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) 			rc = 0; /* clear length (success) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) 	} /* switch */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) 	free_page((unsigned long) ftp.buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) EXPORT_SYMBOL(hmcdrv_ftp_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226)  * hmcdrv_ftp_cmd() - Perform a HMC drive FTP, with data from user-space
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)  * @cmd: FTP command string "<cmd> <filename>"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)  * @offset: file position to read/write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230)  * @buf: user-space buffer for read/written directory/file
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)  * @len: size of @buf (read/dir) or number of bytes to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)  * This function must not be called before hmcdrv_ftp_startup() was called.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)  * Return: number of bytes read/written or a negative error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) ssize_t hmcdrv_ftp_cmd(char __kernel *cmd, loff_t offset,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) 		       char __user *buf, size_t len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) 	int order;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) 	struct hmcdrv_ftp_cmdspec ftp = {.len = len, .ofs = offset};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) 	ssize_t retlen = hmcdrv_ftp_parse(cmd, &ftp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) 	if (retlen)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) 		return retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) 	order = get_order(ftp.len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) 	ftp.buf = (void *) __get_free_pages(GFP_KERNEL | GFP_DMA, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) 	if (!ftp.buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) 	switch (ftp.id) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 	case HMCDRV_FTP_DIR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) 	case HMCDRV_FTP_NLIST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) 	case HMCDRV_FTP_GET:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) 		retlen = hmcdrv_ftp_do(&ftp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) 		if ((retlen >= 0) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) 		    copy_to_user(buf, ftp.buf, retlen))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262) 			retlen = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) 	case HMCDRV_FTP_PUT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) 	case HMCDRV_FTP_APPEND:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) 		if (!copy_from_user(ftp.buf, buf, ftp.len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) 			retlen = hmcdrv_ftp_do(&ftp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) 			retlen = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) 	case HMCDRV_FTP_DELETE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) 		retlen = hmcdrv_ftp_do(&ftp);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) 	default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) 		retlen = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) 	free_pages((unsigned long) ftp.buf, order);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) 	return retlen;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287)  * hmcdrv_ftp_startup() - startup of HMC drive FTP functionality for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288)  * dedicated (owner) instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290)  * Return: 0 on success, else an (negative) error code
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) int hmcdrv_ftp_startup(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) 	static const struct hmcdrv_ftp_ops hmcdrv_ftp_zvm = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) 		.startup = diag_ftp_startup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) 		.shutdown = diag_ftp_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) 		.transfer = diag_ftp_cmd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) 	static const struct hmcdrv_ftp_ops hmcdrv_ftp_lpar = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) 		.startup = sclp_ftp_startup,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) 		.shutdown = sclp_ftp_shutdown,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) 		.transfer = sclp_ftp_cmd
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) 	};
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) 	int rc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) 	mutex_lock(&hmcdrv_ftp_mutex); /* block transfers while start-up */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) 	if (hmcdrv_ftp_refcnt == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) 		if (MACHINE_IS_VM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) 			hmcdrv_ftp_funcs = &hmcdrv_ftp_zvm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) 		else if (MACHINE_IS_LPAR || MACHINE_IS_KVM)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) 			hmcdrv_ftp_funcs = &hmcdrv_ftp_lpar;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) 		else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) 			rc = -EOPNOTSUPP;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) 		if (hmcdrv_ftp_funcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) 			rc = hmcdrv_ftp_funcs->startup();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) 	if (!rc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) 		++hmcdrv_ftp_refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) 	mutex_unlock(&hmcdrv_ftp_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) 	return rc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) EXPORT_SYMBOL(hmcdrv_ftp_startup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331)  * hmcdrv_ftp_shutdown() - shutdown of HMC drive FTP functionality for a
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332)  * dedicated (owner) instance
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) void hmcdrv_ftp_shutdown(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) 	mutex_lock(&hmcdrv_ftp_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) 	--hmcdrv_ftp_refcnt;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) 	if ((hmcdrv_ftp_refcnt == 0) && hmcdrv_ftp_funcs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) 		hmcdrv_ftp_funcs->shutdown();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) 	mutex_unlock(&hmcdrv_ftp_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) EXPORT_SYMBOL(hmcdrv_ftp_shutdown);