sclp_sdias.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * SCLP "store data in absolute storage"
  4. *
  5. * Copyright IBM Corp. 2003, 2013
  6. * Author(s): Michael Holzheu
  7. */
  8. #define KMSG_COMPONENT "sclp_sdias"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/completion.h>
  11. #include <linux/sched.h>
  12. #include <asm/sclp.h>
  13. #include <asm/debug.h>
  14. #include <asm/ipl.h>
  15. #include "sclp_sdias.h"
  16. #include "sclp.h"
  17. #include "sclp_rw.h"
  18. #define TRACE(x...) debug_sprintf_event(sdias_dbf, 1, x)
  19. #define SDIAS_RETRIES 300
  20. static struct debug_info *sdias_dbf;
  21. static struct sclp_register sclp_sdias_register = {
  22. .send_mask = EVTYP_SDIAS_MASK,
  23. };
  24. static struct sdias_sccb *sclp_sdias_sccb;
  25. static struct sdias_evbuf sdias_evbuf;
  26. static DECLARE_COMPLETION(evbuf_accepted);
  27. static DECLARE_COMPLETION(evbuf_done);
  28. static DEFINE_MUTEX(sdias_mutex);
  29. /*
  30. * Called by SCLP base when read event data has been completed (async mode only)
  31. */
  32. static void sclp_sdias_receiver_fn(struct evbuf_header *evbuf)
  33. {
  34. memcpy(&sdias_evbuf, evbuf,
  35. min_t(unsigned long, sizeof(sdias_evbuf), evbuf->length));
  36. complete(&evbuf_done);
  37. TRACE("sclp_sdias_receiver_fn done\n");
  38. }
  39. /*
  40. * Called by SCLP base when sdias event has been accepted
  41. */
  42. static void sdias_callback(struct sclp_req *request, void *data)
  43. {
  44. complete(&evbuf_accepted);
  45. TRACE("callback done\n");
  46. }
  47. static int sdias_sclp_send(struct sclp_req *req)
  48. {
  49. struct sdias_sccb *sccb = sclp_sdias_sccb;
  50. int retries;
  51. int rc;
  52. for (retries = SDIAS_RETRIES; retries; retries--) {
  53. TRACE("add request\n");
  54. rc = sclp_add_request(req);
  55. if (rc) {
  56. /* not initiated, wait some time and retry */
  57. set_current_state(TASK_INTERRUPTIBLE);
  58. TRACE("add request failed: rc = %i\n",rc);
  59. schedule_timeout(msecs_to_jiffies(500));
  60. continue;
  61. }
  62. /* initiated, wait for completion of service call */
  63. wait_for_completion(&evbuf_accepted);
  64. if (req->status == SCLP_REQ_FAILED) {
  65. TRACE("sclp request failed\n");
  66. continue;
  67. }
  68. /* if not accepted, retry */
  69. if (!(sccb->evbuf.hdr.flags & 0x80)) {
  70. TRACE("sclp request failed: flags=%x\n",
  71. sccb->evbuf.hdr.flags);
  72. continue;
  73. }
  74. /*
  75. * for the sync interface the response is in the initial sccb
  76. */
  77. if (!sclp_sdias_register.receiver_fn) {
  78. memcpy(&sdias_evbuf, &sccb->evbuf, sizeof(sdias_evbuf));
  79. TRACE("sync request done\n");
  80. return 0;
  81. }
  82. /* otherwise we wait for completion */
  83. wait_for_completion(&evbuf_done);
  84. TRACE("request done\n");
  85. return 0;
  86. }
  87. return -EIO;
  88. }
  89. /*
  90. * Get number of blocks (4K) available in the HSA
  91. */
  92. int sclp_sdias_blk_count(void)
  93. {
  94. struct sdias_sccb *sccb = sclp_sdias_sccb;
  95. struct sclp_req request;
  96. int rc;
  97. mutex_lock(&sdias_mutex);
  98. memset(sccb, 0, sizeof(*sccb));
  99. memset(&request, 0, sizeof(request));
  100. sccb->hdr.length = sizeof(*sccb);
  101. sccb->evbuf.hdr.length = sizeof(struct sdias_evbuf);
  102. sccb->evbuf.hdr.type = EVTYP_SDIAS;
  103. sccb->evbuf.event_qual = SDIAS_EQ_SIZE;
  104. sccb->evbuf.data_id = SDIAS_DI_FCP_DUMP;
  105. sccb->evbuf.event_id = 4712;
  106. sccb->evbuf.dbs = 1;
  107. request.sccb = sccb;
  108. request.command = SCLP_CMDW_WRITE_EVENT_DATA;
  109. request.status = SCLP_REQ_FILLED;
  110. request.callback = sdias_callback;
  111. rc = sdias_sclp_send(&request);
  112. if (rc) {
  113. pr_err("sclp_send failed for get_nr_blocks\n");
  114. goto out;
  115. }
  116. if (sccb->hdr.response_code != 0x0020) {
  117. TRACE("send failed: %x\n", sccb->hdr.response_code);
  118. rc = -EIO;
  119. goto out;
  120. }
  121. switch (sdias_evbuf.event_status) {
  122. case 0:
  123. rc = sdias_evbuf.blk_cnt;
  124. break;
  125. default:
  126. pr_err("SCLP error: %x\n", sdias_evbuf.event_status);
  127. rc = -EIO;
  128. goto out;
  129. }
  130. TRACE("%i blocks\n", rc);
  131. out:
  132. mutex_unlock(&sdias_mutex);
  133. return rc;
  134. }
  135. /*
  136. * Copy from HSA to absolute storage (not reentrant):
  137. *
  138. * @dest : Address of buffer where data should be copied
  139. * @start_blk: Start Block (beginning with 1)
  140. * @nr_blks : Number of 4K blocks to copy
  141. *
  142. * Return Value: 0 : Requested 'number' of blocks of data copied
  143. * <0: ERROR - negative event status
  144. */
  145. int sclp_sdias_copy(void *dest, int start_blk, int nr_blks)
  146. {
  147. struct sdias_sccb *sccb = sclp_sdias_sccb;
  148. struct sclp_req request;
  149. int rc;
  150. mutex_lock(&sdias_mutex);
  151. memset(sccb, 0, sizeof(*sccb));
  152. memset(&request, 0, sizeof(request));
  153. sccb->hdr.length = sizeof(*sccb);
  154. sccb->evbuf.hdr.length = sizeof(struct sdias_evbuf);
  155. sccb->evbuf.hdr.type = EVTYP_SDIAS;
  156. sccb->evbuf.hdr.flags = 0;
  157. sccb->evbuf.event_qual = SDIAS_EQ_STORE_DATA;
  158. sccb->evbuf.data_id = SDIAS_DI_FCP_DUMP;
  159. sccb->evbuf.event_id = 4712;
  160. sccb->evbuf.asa_size = SDIAS_ASA_SIZE_64;
  161. sccb->evbuf.event_status = 0;
  162. sccb->evbuf.blk_cnt = nr_blks;
  163. sccb->evbuf.asa = __pa(dest);
  164. sccb->evbuf.fbn = start_blk;
  165. sccb->evbuf.lbn = 0;
  166. sccb->evbuf.dbs = 1;
  167. request.sccb = sccb;
  168. request.command = SCLP_CMDW_WRITE_EVENT_DATA;
  169. request.status = SCLP_REQ_FILLED;
  170. request.callback = sdias_callback;
  171. rc = sdias_sclp_send(&request);
  172. if (rc) {
  173. pr_err("sclp_send failed: %x\n", rc);
  174. goto out;
  175. }
  176. if (sccb->hdr.response_code != 0x0020) {
  177. TRACE("copy failed: %x\n", sccb->hdr.response_code);
  178. rc = -EIO;
  179. goto out;
  180. }
  181. switch (sdias_evbuf.event_status) {
  182. case SDIAS_EVSTATE_ALL_STORED:
  183. TRACE("all stored\n");
  184. break;
  185. case SDIAS_EVSTATE_PART_STORED:
  186. TRACE("part stored: %i\n", sdias_evbuf.blk_cnt);
  187. break;
  188. case SDIAS_EVSTATE_NO_DATA:
  189. TRACE("no data\n");
  190. fallthrough;
  191. default:
  192. pr_err("Error from SCLP while copying hsa. Event status = %x\n",
  193. sdias_evbuf.event_status);
  194. rc = -EIO;
  195. }
  196. out:
  197. mutex_unlock(&sdias_mutex);
  198. return rc;
  199. }
  200. static int __init sclp_sdias_register_check(void)
  201. {
  202. int rc;
  203. rc = sclp_register(&sclp_sdias_register);
  204. if (rc)
  205. return rc;
  206. if (sclp_sdias_blk_count() == 0) {
  207. sclp_unregister(&sclp_sdias_register);
  208. return -ENODEV;
  209. }
  210. return 0;
  211. }
  212. static int __init sclp_sdias_init_sync(void)
  213. {
  214. TRACE("Try synchronous mode\n");
  215. sclp_sdias_register.receive_mask = 0;
  216. sclp_sdias_register.receiver_fn = NULL;
  217. return sclp_sdias_register_check();
  218. }
  219. static int __init sclp_sdias_init_async(void)
  220. {
  221. TRACE("Try asynchronous mode\n");
  222. sclp_sdias_register.receive_mask = EVTYP_SDIAS_MASK;
  223. sclp_sdias_register.receiver_fn = sclp_sdias_receiver_fn;
  224. return sclp_sdias_register_check();
  225. }
  226. int __init sclp_sdias_init(void)
  227. {
  228. if (!is_ipl_type_dump())
  229. return 0;
  230. sclp_sdias_sccb = (void *) __get_free_page(GFP_KERNEL | GFP_DMA);
  231. BUG_ON(!sclp_sdias_sccb);
  232. sdias_dbf = debug_register("dump_sdias", 4, 1, 4 * sizeof(long));
  233. debug_register_view(sdias_dbf, &debug_sprintf_view);
  234. debug_set_level(sdias_dbf, 6);
  235. if (sclp_sdias_init_sync() == 0)
  236. goto out;
  237. if (sclp_sdias_init_async() == 0)
  238. goto out;
  239. TRACE("init failed\n");
  240. free_page((unsigned long) sclp_sdias_sccb);
  241. return -ENODEV;
  242. out:
  243. TRACE("init done\n");
  244. return 0;
  245. }