diag_ftp.c 6.2 KB

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