pci_clp.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /*
  2. * Copyright IBM Corp. 2012
  3. *
  4. * Author(s):
  5. * Jan Glauber <jang@linux.vnet.ibm.com>
  6. */
  7. #define COMPONENT "zPCI"
  8. #define pr_fmt(fmt) COMPONENT ": " fmt
  9. #include <linux/kernel.h>
  10. #include <linux/slab.h>
  11. #include <linux/err.h>
  12. #include <linux/delay.h>
  13. #include <linux/pci.h>
  14. #include <asm/pci_clp.h>
  15. /*
  16. * Call Logical Processor
  17. * Retry logic is handled by the caller.
  18. */
  19. static inline u8 clp_instr(void *req)
  20. {
  21. u64 ilpm;
  22. u8 cc;
  23. asm volatile (
  24. " .insn rrf,0xb9a00000,%[ilpm],%[req],0x0,0x2\n"
  25. " ipm %[cc]\n"
  26. " srl %[cc],28\n"
  27. : [cc] "=d" (cc), [ilpm] "=d" (ilpm)
  28. : [req] "a" (req)
  29. : "cc", "memory");
  30. return cc;
  31. }
  32. static void *clp_alloc_block(void)
  33. {
  34. struct page *page = alloc_pages(GFP_KERNEL, get_order(CLP_BLK_SIZE));
  35. return (page) ? page_address(page) : NULL;
  36. }
  37. static void clp_free_block(void *ptr)
  38. {
  39. free_pages((unsigned long) ptr, get_order(CLP_BLK_SIZE));
  40. }
  41. static void clp_store_query_pci_fngrp(struct zpci_dev *zdev,
  42. struct clp_rsp_query_pci_grp *response)
  43. {
  44. switch (response->version) {
  45. case 1:
  46. zdev->max_bus_speed = PCIE_SPEED_5_0GT;
  47. break;
  48. default:
  49. zdev->max_bus_speed = PCI_SPEED_UNKNOWN;
  50. break;
  51. }
  52. }
  53. static int clp_query_pci_fngrp(struct zpci_dev *zdev, u8 pfgid)
  54. {
  55. struct clp_req_rsp_query_pci_grp *rrb;
  56. int rc;
  57. rrb = clp_alloc_block();
  58. if (!rrb)
  59. return -ENOMEM;
  60. memset(rrb, 0, sizeof(*rrb));
  61. rrb->request.hdr.len = sizeof(rrb->request);
  62. rrb->request.hdr.cmd = CLP_QUERY_PCI_FNGRP;
  63. rrb->response.hdr.len = sizeof(rrb->response);
  64. rrb->request.pfgid = pfgid;
  65. rc = clp_instr(rrb);
  66. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
  67. clp_store_query_pci_fngrp(zdev, &rrb->response);
  68. else {
  69. pr_err("Query PCI FNGRP failed with response: %x cc: %d\n",
  70. rrb->response.hdr.rsp, rc);
  71. rc = -EIO;
  72. }
  73. clp_free_block(rrb);
  74. return rc;
  75. }
  76. static int clp_store_query_pci_fn(struct zpci_dev *zdev,
  77. struct clp_rsp_query_pci *response)
  78. {
  79. int i;
  80. for (i = 0; i < PCI_BAR_COUNT; i++) {
  81. zdev->bars[i].val = le32_to_cpu(response->bar[i]);
  82. zdev->bars[i].size = response->bar_size[i];
  83. }
  84. zdev->pchid = response->pchid;
  85. zdev->pfgid = response->pfgid;
  86. return 0;
  87. }
  88. static int clp_query_pci_fn(struct zpci_dev *zdev, u32 fh)
  89. {
  90. struct clp_req_rsp_query_pci *rrb;
  91. int rc;
  92. rrb = clp_alloc_block();
  93. if (!rrb)
  94. return -ENOMEM;
  95. memset(rrb, 0, sizeof(*rrb));
  96. rrb->request.hdr.len = sizeof(rrb->request);
  97. rrb->request.hdr.cmd = CLP_QUERY_PCI_FN;
  98. rrb->response.hdr.len = sizeof(rrb->response);
  99. rrb->request.fh = fh;
  100. rc = clp_instr(rrb);
  101. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
  102. rc = clp_store_query_pci_fn(zdev, &rrb->response);
  103. if (rc)
  104. goto out;
  105. if (rrb->response.pfgid)
  106. rc = clp_query_pci_fngrp(zdev, rrb->response.pfgid);
  107. } else {
  108. pr_err("Query PCI failed with response: %x cc: %d\n",
  109. rrb->response.hdr.rsp, rc);
  110. rc = -EIO;
  111. }
  112. out:
  113. clp_free_block(rrb);
  114. return rc;
  115. }
  116. int clp_add_pci_device(u32 fid, u32 fh, int configured)
  117. {
  118. struct zpci_dev *zdev;
  119. int rc;
  120. zdev = zpci_alloc_device();
  121. if (IS_ERR(zdev))
  122. return PTR_ERR(zdev);
  123. zdev->fh = fh;
  124. zdev->fid = fid;
  125. /* Query function properties and update zdev */
  126. rc = clp_query_pci_fn(zdev, fh);
  127. if (rc)
  128. goto error;
  129. if (configured)
  130. zdev->state = ZPCI_FN_STATE_CONFIGURED;
  131. else
  132. zdev->state = ZPCI_FN_STATE_STANDBY;
  133. rc = zpci_create_device(zdev);
  134. if (rc)
  135. goto error;
  136. return 0;
  137. error:
  138. zpci_free_device(zdev);
  139. return rc;
  140. }
  141. /*
  142. * Enable/Disable a given PCI function defined by its function handle.
  143. */
  144. static int clp_set_pci_fn(u32 *fh, u8 nr_dma_as, u8 command)
  145. {
  146. struct clp_req_rsp_set_pci *rrb;
  147. int rc, retries = 1000;
  148. rrb = clp_alloc_block();
  149. if (!rrb)
  150. return -ENOMEM;
  151. do {
  152. memset(rrb, 0, sizeof(*rrb));
  153. rrb->request.hdr.len = sizeof(rrb->request);
  154. rrb->request.hdr.cmd = CLP_SET_PCI_FN;
  155. rrb->response.hdr.len = sizeof(rrb->response);
  156. rrb->request.fh = *fh;
  157. rrb->request.oc = command;
  158. rrb->request.ndas = nr_dma_as;
  159. rc = clp_instr(rrb);
  160. if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) {
  161. retries--;
  162. if (retries < 0)
  163. break;
  164. msleep(1);
  165. }
  166. } while (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY);
  167. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
  168. *fh = rrb->response.fh;
  169. else {
  170. pr_err("Set PCI FN failed with response: %x cc: %d\n",
  171. rrb->response.hdr.rsp, rc);
  172. rc = -EIO;
  173. }
  174. clp_free_block(rrb);
  175. return rc;
  176. }
  177. int clp_enable_fh(struct zpci_dev *zdev, u8 nr_dma_as)
  178. {
  179. u32 fh = zdev->fh;
  180. int rc;
  181. rc = clp_set_pci_fn(&fh, nr_dma_as, CLP_SET_ENABLE_PCI_FN);
  182. if (!rc)
  183. /* Success -> store enabled handle in zdev */
  184. zdev->fh = fh;
  185. return rc;
  186. }
  187. int clp_disable_fh(struct zpci_dev *zdev)
  188. {
  189. u32 fh = zdev->fh;
  190. int rc;
  191. if (!zdev_enabled(zdev))
  192. return 0;
  193. dev_info(&zdev->pdev->dev, "disabling fn handle: 0x%x\n", fh);
  194. rc = clp_set_pci_fn(&fh, 0, CLP_SET_DISABLE_PCI_FN);
  195. if (!rc)
  196. /* Success -> store disabled handle in zdev */
  197. zdev->fh = fh;
  198. else
  199. dev_err(&zdev->pdev->dev,
  200. "Failed to disable fn handle: 0x%x\n", fh);
  201. return rc;
  202. }
  203. static void clp_check_pcifn_entry(struct clp_fh_list_entry *entry)
  204. {
  205. int present, rc;
  206. if (!entry->vendor_id)
  207. return;
  208. /* TODO: be a little bit more scalable */
  209. present = zpci_fid_present(entry->fid);
  210. if (present)
  211. pr_debug("%s: device %x already present\n", __func__, entry->fid);
  212. /* skip already used functions */
  213. if (present && entry->config_state)
  214. return;
  215. /* aev 306: function moved to stand-by state */
  216. if (present && !entry->config_state) {
  217. /*
  218. * The handle is already disabled, that means no iota/irq freeing via
  219. * the firmware interfaces anymore. Need to free resources manually
  220. * (DMA memory, debug, sysfs)...
  221. */
  222. zpci_stop_device(get_zdev_by_fid(entry->fid));
  223. return;
  224. }
  225. rc = clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
  226. if (rc)
  227. pr_err("Failed to add fid: 0x%x\n", entry->fid);
  228. }
  229. int clp_find_pci_devices(void)
  230. {
  231. struct clp_req_rsp_list_pci *rrb;
  232. u64 resume_token = 0;
  233. int entries, i, rc;
  234. rrb = clp_alloc_block();
  235. if (!rrb)
  236. return -ENOMEM;
  237. do {
  238. memset(rrb, 0, sizeof(*rrb));
  239. rrb->request.hdr.len = sizeof(rrb->request);
  240. rrb->request.hdr.cmd = CLP_LIST_PCI;
  241. /* store as many entries as possible */
  242. rrb->response.hdr.len = CLP_BLK_SIZE - LIST_PCI_HDR_LEN;
  243. rrb->request.resume_token = resume_token;
  244. /* Get PCI function handle list */
  245. rc = clp_instr(rrb);
  246. if (rc || rrb->response.hdr.rsp != CLP_RC_OK) {
  247. pr_err("List PCI failed with response: 0x%x cc: %d\n",
  248. rrb->response.hdr.rsp, rc);
  249. rc = -EIO;
  250. goto out;
  251. }
  252. WARN_ON_ONCE(rrb->response.entry_size !=
  253. sizeof(struct clp_fh_list_entry));
  254. entries = (rrb->response.hdr.len - LIST_PCI_HDR_LEN) /
  255. rrb->response.entry_size;
  256. pr_info("Detected number of PCI functions: %u\n", entries);
  257. /* Store the returned resume token as input for the next call */
  258. resume_token = rrb->response.resume_token;
  259. for (i = 0; i < entries; i++)
  260. clp_check_pcifn_entry(&rrb->response.fh_list[i]);
  261. } while (resume_token);
  262. pr_debug("Maximum number of supported PCI functions: %u\n",
  263. rrb->response.max_fn);
  264. out:
  265. clp_free_block(rrb);
  266. return rc;
  267. }