sclp_pci.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PCI I/O adapter configuration related functions.
  4. *
  5. * Copyright IBM Corp. 2016
  6. */
  7. #define KMSG_COMPONENT "sclp_cmd"
  8. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  9. #include <linux/completion.h>
  10. #include <linux/export.h>
  11. #include <linux/mutex.h>
  12. #include <linux/errno.h>
  13. #include <linux/slab.h>
  14. #include <linux/init.h>
  15. #include <linux/err.h>
  16. #include <asm/sclp.h>
  17. #include "sclp.h"
  18. #define SCLP_CMDW_CONFIGURE_PCI 0x001a0001
  19. #define SCLP_CMDW_DECONFIGURE_PCI 0x001b0001
  20. #define SCLP_ATYPE_PCI 2
  21. #define SCLP_ERRNOTIFY_AQ_RESET 0
  22. #define SCLP_ERRNOTIFY_AQ_REPAIR 1
  23. #define SCLP_ERRNOTIFY_AQ_INFO_LOG 2
  24. static DEFINE_MUTEX(sclp_pci_mutex);
  25. static struct sclp_register sclp_pci_event = {
  26. .send_mask = EVTYP_ERRNOTIFY_MASK,
  27. };
  28. struct err_notify_evbuf {
  29. struct evbuf_header header;
  30. u8 action;
  31. u8 atype;
  32. u32 fh;
  33. u32 fid;
  34. u8 data[];
  35. } __packed;
  36. struct err_notify_sccb {
  37. struct sccb_header header;
  38. struct err_notify_evbuf evbuf;
  39. } __packed;
  40. struct pci_cfg_sccb {
  41. struct sccb_header header;
  42. u8 atype; /* adapter type */
  43. u8 reserved1;
  44. u16 reserved2;
  45. u32 aid; /* adapter identifier */
  46. } __packed;
  47. static int do_pci_configure(sclp_cmdw_t cmd, u32 fid)
  48. {
  49. struct pci_cfg_sccb *sccb;
  50. int rc;
  51. if (!SCLP_HAS_PCI_RECONFIG)
  52. return -EOPNOTSUPP;
  53. sccb = (struct pci_cfg_sccb *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  54. if (!sccb)
  55. return -ENOMEM;
  56. sccb->header.length = PAGE_SIZE;
  57. sccb->atype = SCLP_ATYPE_PCI;
  58. sccb->aid = fid;
  59. rc = sclp_sync_request(cmd, sccb);
  60. if (rc)
  61. goto out;
  62. switch (sccb->header.response_code) {
  63. case 0x0020:
  64. case 0x0120:
  65. break;
  66. default:
  67. pr_warn("configure PCI I/O adapter failed: cmd=0x%08x response=0x%04x\n",
  68. cmd, sccb->header.response_code);
  69. rc = -EIO;
  70. break;
  71. }
  72. out:
  73. free_page((unsigned long) sccb);
  74. return rc;
  75. }
  76. int sclp_pci_configure(u32 fid)
  77. {
  78. return do_pci_configure(SCLP_CMDW_CONFIGURE_PCI, fid);
  79. }
  80. EXPORT_SYMBOL(sclp_pci_configure);
  81. int sclp_pci_deconfigure(u32 fid)
  82. {
  83. return do_pci_configure(SCLP_CMDW_DECONFIGURE_PCI, fid);
  84. }
  85. EXPORT_SYMBOL(sclp_pci_deconfigure);
  86. static void sclp_pci_callback(struct sclp_req *req, void *data)
  87. {
  88. struct completion *completion = data;
  89. complete(completion);
  90. }
  91. static int sclp_pci_check_report(struct zpci_report_error_header *report)
  92. {
  93. if (report->version != 1)
  94. return -EINVAL;
  95. switch (report->action) {
  96. case SCLP_ERRNOTIFY_AQ_RESET:
  97. case SCLP_ERRNOTIFY_AQ_REPAIR:
  98. case SCLP_ERRNOTIFY_AQ_INFO_LOG:
  99. break;
  100. default:
  101. return -EINVAL;
  102. }
  103. if (report->length > (PAGE_SIZE - sizeof(struct err_notify_sccb)))
  104. return -EINVAL;
  105. return 0;
  106. }
  107. int sclp_pci_report(struct zpci_report_error_header *report, u32 fh, u32 fid)
  108. {
  109. DECLARE_COMPLETION_ONSTACK(completion);
  110. struct err_notify_sccb *sccb;
  111. struct sclp_req req;
  112. int ret;
  113. ret = sclp_pci_check_report(report);
  114. if (ret)
  115. return ret;
  116. mutex_lock(&sclp_pci_mutex);
  117. ret = sclp_register(&sclp_pci_event);
  118. if (ret)
  119. goto out_unlock;
  120. if (!(sclp_pci_event.sclp_receive_mask & EVTYP_ERRNOTIFY_MASK)) {
  121. ret = -EOPNOTSUPP;
  122. goto out_unregister;
  123. }
  124. sccb = (void *) get_zeroed_page(GFP_KERNEL | GFP_DMA);
  125. if (!sccb) {
  126. ret = -ENOMEM;
  127. goto out_unregister;
  128. }
  129. memset(&req, 0, sizeof(req));
  130. req.callback_data = &completion;
  131. req.callback = sclp_pci_callback;
  132. req.command = SCLP_CMDW_WRITE_EVENT_DATA;
  133. req.status = SCLP_REQ_FILLED;
  134. req.sccb = sccb;
  135. sccb->evbuf.header.length = sizeof(sccb->evbuf) + report->length;
  136. sccb->evbuf.header.type = EVTYP_ERRNOTIFY;
  137. sccb->header.length = sizeof(sccb->header) + sccb->evbuf.header.length;
  138. sccb->evbuf.action = report->action;
  139. sccb->evbuf.atype = SCLP_ATYPE_PCI;
  140. sccb->evbuf.fh = fh;
  141. sccb->evbuf.fid = fid;
  142. memcpy(sccb->evbuf.data, report->data, report->length);
  143. ret = sclp_add_request(&req);
  144. if (ret)
  145. goto out_free_req;
  146. wait_for_completion(&completion);
  147. if (req.status != SCLP_REQ_DONE) {
  148. pr_warn("request failed (status=0x%02x)\n",
  149. req.status);
  150. ret = -EIO;
  151. goto out_free_req;
  152. }
  153. if (sccb->header.response_code != 0x0020) {
  154. pr_warn("request failed with response code 0x%x\n",
  155. sccb->header.response_code);
  156. ret = -EIO;
  157. }
  158. out_free_req:
  159. free_page((unsigned long) sccb);
  160. out_unregister:
  161. sclp_unregister(&sclp_pci_event);
  162. out_unlock:
  163. mutex_unlock(&sclp_pci_mutex);
  164. return ret;
  165. }