pci_clp.c 7.8 KB

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