diag_ftp.c 6.2 KB

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