pci_clp.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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_debug.h>
  15. #include <asm/pci_clp.h>
  16. /*
  17. * Call Logical Processor
  18. * Retry logic is handled by the caller.
  19. */
  20. static inline u8 clp_instr(void *data)
  21. {
  22. struct { u8 _[CLP_BLK_SIZE]; } *req = data;
  23. u64 ignored;
  24. u8 cc;
  25. asm volatile (
  26. " .insn rrf,0xb9a00000,%[ign],%[req],0x0,0x2\n"
  27. " ipm %[cc]\n"
  28. " srl %[cc],28\n"
  29. : [cc] "=d" (cc), [ign] "=d" (ignored), "+m" (*req)
  30. : [req] "a" (req)
  31. : "cc");
  32. return cc;
  33. }
  34. static void *clp_alloc_block(gfp_t gfp_mask)
  35. {
  36. return (void *) __get_free_pages(gfp_mask, get_order(CLP_BLK_SIZE));
  37. }
  38. static void clp_free_block(void *ptr)
  39. {
  40. free_pages((unsigned long) ptr, get_order(CLP_BLK_SIZE));
  41. }
  42. static void clp_store_query_pci_fngrp(struct zpci_dev *zdev,
  43. struct clp_rsp_query_pci_grp *response)
  44. {
  45. zdev->tlb_refresh = response->refresh;
  46. zdev->dma_mask = response->dasm;
  47. zdev->msi_addr = response->msia;
  48. zdev->fmb_update = response->mui;
  49. pr_debug("Supported number of MSI vectors: %u\n", response->noi);
  50. switch (response->version) {
  51. case 1:
  52. zdev->max_bus_speed = PCIE_SPEED_5_0GT;
  53. break;
  54. default:
  55. zdev->max_bus_speed = PCI_SPEED_UNKNOWN;
  56. break;
  57. }
  58. }
  59. static int clp_query_pci_fngrp(struct zpci_dev *zdev, u8 pfgid)
  60. {
  61. struct clp_req_rsp_query_pci_grp *rrb;
  62. int rc;
  63. rrb = clp_alloc_block(GFP_KERNEL);
  64. if (!rrb)
  65. return -ENOMEM;
  66. memset(rrb, 0, sizeof(*rrb));
  67. rrb->request.hdr.len = sizeof(rrb->request);
  68. rrb->request.hdr.cmd = CLP_QUERY_PCI_FNGRP;
  69. rrb->response.hdr.len = sizeof(rrb->response);
  70. rrb->request.pfgid = pfgid;
  71. rc = clp_instr(rrb);
  72. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
  73. clp_store_query_pci_fngrp(zdev, &rrb->response);
  74. else {
  75. pr_err("Query PCI FNGRP failed with response: %x cc: %d\n",
  76. rrb->response.hdr.rsp, rc);
  77. rc = -EIO;
  78. }
  79. clp_free_block(rrb);
  80. return rc;
  81. }
  82. static int clp_store_query_pci_fn(struct zpci_dev *zdev,
  83. struct clp_rsp_query_pci *response)
  84. {
  85. int i;
  86. for (i = 0; i < PCI_BAR_COUNT; i++) {
  87. zdev->bars[i].val = le32_to_cpu(response->bar[i]);
  88. zdev->bars[i].size = response->bar_size[i];
  89. }
  90. zdev->start_dma = response->sdma;
  91. zdev->end_dma = response->edma;
  92. zdev->pchid = response->pchid;
  93. zdev->pfgid = response->pfgid;
  94. return 0;
  95. }
  96. static int clp_query_pci_fn(struct zpci_dev *zdev, u32 fh)
  97. {
  98. struct clp_req_rsp_query_pci *rrb;
  99. int rc;
  100. rrb = clp_alloc_block(GFP_KERNEL);
  101. if (!rrb)
  102. return -ENOMEM;
  103. memset(rrb, 0, sizeof(*rrb));
  104. rrb->request.hdr.len = sizeof(rrb->request);
  105. rrb->request.hdr.cmd = CLP_QUERY_PCI_FN;
  106. rrb->response.hdr.len = sizeof(rrb->response);
  107. rrb->request.fh = fh;
  108. rc = clp_instr(rrb);
  109. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
  110. rc = clp_store_query_pci_fn(zdev, &rrb->response);
  111. if (rc)
  112. goto out;
  113. if (rrb->response.pfgid)
  114. rc = clp_query_pci_fngrp(zdev, rrb->response.pfgid);
  115. } else {
  116. pr_err("Query PCI failed with response: %x cc: %d\n",
  117. rrb->response.hdr.rsp, rc);
  118. rc = -EIO;
  119. }
  120. out:
  121. clp_free_block(rrb);
  122. return rc;
  123. }
  124. int clp_add_pci_device(u32 fid, u32 fh, int configured)
  125. {
  126. struct zpci_dev *zdev;
  127. int rc;
  128. zpci_dbg(3, "add fid:%x, fh:%x, c:%d\n", fid, fh, configured);
  129. zdev = zpci_alloc_device();
  130. if (IS_ERR(zdev))
  131. return PTR_ERR(zdev);
  132. zdev->fh = fh;
  133. zdev->fid = fid;
  134. /* Query function properties and update zdev */
  135. rc = clp_query_pci_fn(zdev, fh);
  136. if (rc)
  137. goto error;
  138. if (configured)
  139. zdev->state = ZPCI_FN_STATE_CONFIGURED;
  140. else
  141. zdev->state = ZPCI_FN_STATE_STANDBY;
  142. rc = zpci_create_device(zdev);
  143. if (rc)
  144. goto error;
  145. return 0;
  146. error:
  147. zpci_free_device(zdev);
  148. return rc;
  149. }
  150. /*
  151. * Enable/Disable a given PCI function defined by its function handle.
  152. */
  153. static int clp_set_pci_fn(u32 *fh, u8 nr_dma_as, u8 command)
  154. {
  155. struct clp_req_rsp_set_pci *rrb;
  156. int rc, retries = 100;
  157. rrb = clp_alloc_block(GFP_KERNEL);
  158. if (!rrb)
  159. return -ENOMEM;
  160. do {
  161. memset(rrb, 0, sizeof(*rrb));
  162. rrb->request.hdr.len = sizeof(rrb->request);
  163. rrb->request.hdr.cmd = CLP_SET_PCI_FN;
  164. rrb->response.hdr.len = sizeof(rrb->response);
  165. rrb->request.fh = *fh;
  166. rrb->request.oc = command;
  167. rrb->request.ndas = nr_dma_as;
  168. rc = clp_instr(rrb);
  169. if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) {
  170. retries--;
  171. if (retries < 0)
  172. break;
  173. msleep(20);
  174. }
  175. } while (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY);
  176. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
  177. *fh = rrb->response.fh;
  178. else {
  179. zpci_dbg(0, "SPF fh:%x, cc:%d, resp:%x\n", *fh, rc,
  180. rrb->response.hdr.rsp);
  181. rc = -EIO;
  182. }
  183. clp_free_block(rrb);
  184. return rc;
  185. }
  186. int clp_enable_fh(struct zpci_dev *zdev, u8 nr_dma_as)
  187. {
  188. u32 fh = zdev->fh;
  189. int rc;
  190. rc = clp_set_pci_fn(&fh, nr_dma_as, CLP_SET_ENABLE_PCI_FN);
  191. if (!rc)
  192. /* Success -> store enabled handle in zdev */
  193. zdev->fh = fh;
  194. zpci_dbg(3, "ena fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
  195. return rc;
  196. }
  197. int clp_disable_fh(struct zpci_dev *zdev)
  198. {
  199. u32 fh = zdev->fh;
  200. int rc;
  201. if (!zdev_enabled(zdev))
  202. return 0;
  203. rc = clp_set_pci_fn(&fh, 0, CLP_SET_DISABLE_PCI_FN);
  204. if (!rc)
  205. /* Success -> store disabled handle in zdev */
  206. zdev->fh = fh;
  207. zpci_dbg(3, "dis fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
  208. return rc;
  209. }
  210. static int clp_list_pci(struct clp_req_rsp_list_pci *rrb,
  211. void (*cb)(struct clp_fh_list_entry *entry))
  212. {
  213. u64 resume_token = 0;
  214. int entries, i, rc;
  215. do {
  216. memset(rrb, 0, sizeof(*rrb));
  217. rrb->request.hdr.len = sizeof(rrb->request);
  218. rrb->request.hdr.cmd = CLP_LIST_PCI;
  219. /* store as many entries as possible */
  220. rrb->response.hdr.len = CLP_BLK_SIZE - LIST_PCI_HDR_LEN;
  221. rrb->request.resume_token = resume_token;
  222. /* Get PCI function handle list */
  223. rc = clp_instr(rrb);
  224. if (rc || rrb->response.hdr.rsp != CLP_RC_OK) {
  225. pr_err("List PCI failed with response: 0x%x cc: %d\n",
  226. rrb->response.hdr.rsp, rc);
  227. rc = -EIO;
  228. goto out;
  229. }
  230. WARN_ON_ONCE(rrb->response.entry_size !=
  231. sizeof(struct clp_fh_list_entry));
  232. entries = (rrb->response.hdr.len - LIST_PCI_HDR_LEN) /
  233. rrb->response.entry_size;
  234. pr_info("Detected number of PCI functions: %u\n", entries);
  235. /* Store the returned resume token as input for the next call */
  236. resume_token = rrb->response.resume_token;
  237. for (i = 0; i < entries; i++)
  238. cb(&rrb->response.fh_list[i]);
  239. } while (resume_token);
  240. pr_debug("Maximum number of supported PCI functions: %u\n",
  241. rrb->response.max_fn);
  242. out:
  243. return rc;
  244. }
  245. static void __clp_add(struct clp_fh_list_entry *entry)
  246. {
  247. if (!entry->vendor_id)
  248. return;
  249. clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
  250. }
  251. static void __clp_rescan(struct clp_fh_list_entry *entry)
  252. {
  253. struct zpci_dev *zdev;
  254. if (!entry->vendor_id)
  255. return;
  256. zdev = get_zdev_by_fid(entry->fid);
  257. if (!zdev) {
  258. clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
  259. return;
  260. }
  261. if (!entry->config_state) {
  262. /*
  263. * The handle is already disabled, that means no iota/irq freeing via
  264. * the firmware interfaces anymore. Need to free resources manually
  265. * (DMA memory, debug, sysfs)...
  266. */
  267. zpci_stop_device(zdev);
  268. }
  269. }
  270. static void __clp_update(struct clp_fh_list_entry *entry)
  271. {
  272. struct zpci_dev *zdev;
  273. if (!entry->vendor_id)
  274. return;
  275. zdev = get_zdev_by_fid(entry->fid);
  276. if (!zdev)
  277. return;
  278. zdev->fh = entry->fh;
  279. }
  280. int clp_scan_pci_devices(void)
  281. {
  282. struct clp_req_rsp_list_pci *rrb;
  283. int rc;
  284. rrb = clp_alloc_block(GFP_KERNEL);
  285. if (!rrb)
  286. return -ENOMEM;
  287. rc = clp_list_pci(rrb, __clp_add);
  288. clp_free_block(rrb);
  289. return rc;
  290. }
  291. int clp_rescan_pci_devices(void)
  292. {
  293. struct clp_req_rsp_list_pci *rrb;
  294. int rc;
  295. rrb = clp_alloc_block(GFP_KERNEL);
  296. if (!rrb)
  297. return -ENOMEM;
  298. rc = clp_list_pci(rrb, __clp_rescan);
  299. clp_free_block(rrb);
  300. return rc;
  301. }
  302. int clp_rescan_pci_devices_simple(void)
  303. {
  304. struct clp_req_rsp_list_pci *rrb;
  305. int rc;
  306. rrb = clp_alloc_block(GFP_NOWAIT);
  307. if (!rrb)
  308. return -ENOMEM;
  309. rc = clp_list_pci(rrb, __clp_update);
  310. clp_free_block(rrb);
  311. return rc;
  312. }