diag_ftp.c 6.1 KB

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