pci_clp.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. zdev->pft = response->pft;
  102. zdev->vfn = response->vfn;
  103. zdev->uid = response->uid;
  104. memcpy(zdev->pfip, response->pfip, sizeof(zdev->pfip));
  105. if (response->util_str_avail) {
  106. memcpy(zdev->util_str, response->util_str,
  107. sizeof(zdev->util_str));
  108. }
  109. return 0;
  110. }
  111. static int clp_query_pci_fn(struct zpci_dev *zdev, u32 fh)
  112. {
  113. struct clp_req_rsp_query_pci *rrb;
  114. int rc;
  115. rrb = clp_alloc_block(GFP_KERNEL);
  116. if (!rrb)
  117. return -ENOMEM;
  118. memset(rrb, 0, sizeof(*rrb));
  119. rrb->request.hdr.len = sizeof(rrb->request);
  120. rrb->request.hdr.cmd = CLP_QUERY_PCI_FN;
  121. rrb->response.hdr.len = sizeof(rrb->response);
  122. rrb->request.fh = fh;
  123. rc = clp_instr(rrb);
  124. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
  125. rc = clp_store_query_pci_fn(zdev, &rrb->response);
  126. if (rc)
  127. goto out;
  128. if (rrb->response.pfgid)
  129. rc = clp_query_pci_fngrp(zdev, rrb->response.pfgid);
  130. } else {
  131. zpci_err("Q PCI FN:\n");
  132. zpci_err_clp(rrb->response.hdr.rsp, rc);
  133. rc = -EIO;
  134. }
  135. out:
  136. clp_free_block(rrb);
  137. return rc;
  138. }
  139. int clp_add_pci_device(u32 fid, u32 fh, int configured)
  140. {
  141. struct zpci_dev *zdev;
  142. int rc;
  143. zpci_dbg(3, "add fid:%x, fh:%x, c:%d\n", fid, fh, configured);
  144. zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
  145. if (!zdev)
  146. return -ENOMEM;
  147. zdev->fh = fh;
  148. zdev->fid = fid;
  149. /* Query function properties and update zdev */
  150. rc = clp_query_pci_fn(zdev, fh);
  151. if (rc)
  152. goto error;
  153. if (configured)
  154. zdev->state = ZPCI_FN_STATE_CONFIGURED;
  155. else
  156. zdev->state = ZPCI_FN_STATE_STANDBY;
  157. rc = zpci_create_device(zdev);
  158. if (rc)
  159. goto error;
  160. return 0;
  161. error:
  162. kfree(zdev);
  163. return rc;
  164. }
  165. /*
  166. * Enable/Disable a given PCI function defined by its function handle.
  167. */
  168. static int clp_set_pci_fn(u32 *fh, u8 nr_dma_as, u8 command)
  169. {
  170. struct clp_req_rsp_set_pci *rrb;
  171. int rc, retries = 100;
  172. rrb = clp_alloc_block(GFP_KERNEL);
  173. if (!rrb)
  174. return -ENOMEM;
  175. do {
  176. memset(rrb, 0, sizeof(*rrb));
  177. rrb->request.hdr.len = sizeof(rrb->request);
  178. rrb->request.hdr.cmd = CLP_SET_PCI_FN;
  179. rrb->response.hdr.len = sizeof(rrb->response);
  180. rrb->request.fh = *fh;
  181. rrb->request.oc = command;
  182. rrb->request.ndas = nr_dma_as;
  183. rc = clp_instr(rrb);
  184. if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) {
  185. retries--;
  186. if (retries < 0)
  187. break;
  188. msleep(20);
  189. }
  190. } while (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY);
  191. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
  192. *fh = rrb->response.fh;
  193. else {
  194. zpci_err("Set PCI FN:\n");
  195. zpci_err_clp(rrb->response.hdr.rsp, rc);
  196. rc = -EIO;
  197. }
  198. clp_free_block(rrb);
  199. return rc;
  200. }
  201. int clp_enable_fh(struct zpci_dev *zdev, u8 nr_dma_as)
  202. {
  203. u32 fh = zdev->fh;
  204. int rc;
  205. rc = clp_set_pci_fn(&fh, nr_dma_as, CLP_SET_ENABLE_PCI_FN);
  206. if (!rc)
  207. /* Success -> store enabled handle in zdev */
  208. zdev->fh = fh;
  209. zpci_dbg(3, "ena fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
  210. return rc;
  211. }
  212. int clp_disable_fh(struct zpci_dev *zdev)
  213. {
  214. u32 fh = zdev->fh;
  215. int rc;
  216. if (!zdev_enabled(zdev))
  217. return 0;
  218. rc = clp_set_pci_fn(&fh, 0, CLP_SET_DISABLE_PCI_FN);
  219. if (!rc)
  220. /* Success -> store disabled handle in zdev */
  221. zdev->fh = fh;
  222. zpci_dbg(3, "dis fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
  223. return rc;
  224. }
  225. static int clp_list_pci(struct clp_req_rsp_list_pci *rrb,
  226. void (*cb)(struct clp_fh_list_entry *entry))
  227. {
  228. u64 resume_token = 0;
  229. int entries, i, rc;
  230. do {
  231. memset(rrb, 0, sizeof(*rrb));
  232. rrb->request.hdr.len = sizeof(rrb->request);
  233. rrb->request.hdr.cmd = CLP_LIST_PCI;
  234. /* store as many entries as possible */
  235. rrb->response.hdr.len = CLP_BLK_SIZE - LIST_PCI_HDR_LEN;
  236. rrb->request.resume_token = resume_token;
  237. /* Get PCI function handle list */
  238. rc = clp_instr(rrb);
  239. if (rc || rrb->response.hdr.rsp != CLP_RC_OK) {
  240. zpci_err("List PCI FN:\n");
  241. zpci_err_clp(rrb->response.hdr.rsp, rc);
  242. rc = -EIO;
  243. goto out;
  244. }
  245. WARN_ON_ONCE(rrb->response.entry_size !=
  246. sizeof(struct clp_fh_list_entry));
  247. entries = (rrb->response.hdr.len - LIST_PCI_HDR_LEN) /
  248. rrb->response.entry_size;
  249. resume_token = rrb->response.resume_token;
  250. for (i = 0; i < entries; i++)
  251. cb(&rrb->response.fh_list[i]);
  252. } while (resume_token);
  253. out:
  254. return rc;
  255. }
  256. static void __clp_add(struct clp_fh_list_entry *entry)
  257. {
  258. if (!entry->vendor_id)
  259. return;
  260. clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
  261. }
  262. static void __clp_rescan(struct clp_fh_list_entry *entry)
  263. {
  264. struct zpci_dev *zdev;
  265. if (!entry->vendor_id)
  266. return;
  267. zdev = get_zdev_by_fid(entry->fid);
  268. if (!zdev) {
  269. clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
  270. return;
  271. }
  272. if (!entry->config_state) {
  273. /*
  274. * The handle is already disabled, that means no iota/irq freeing via
  275. * the firmware interfaces anymore. Need to free resources manually
  276. * (DMA memory, debug, sysfs)...
  277. */
  278. zpci_stop_device(zdev);
  279. }
  280. }
  281. static void __clp_update(struct clp_fh_list_entry *entry)
  282. {
  283. struct zpci_dev *zdev;
  284. if (!entry->vendor_id)
  285. return;
  286. zdev = get_zdev_by_fid(entry->fid);
  287. if (!zdev)
  288. return;
  289. zdev->fh = entry->fh;
  290. }
  291. int clp_scan_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_add);
  299. clp_free_block(rrb);
  300. return rc;
  301. }
  302. int clp_rescan_pci_devices(void)
  303. {
  304. struct clp_req_rsp_list_pci *rrb;
  305. int rc;
  306. rrb = clp_alloc_block(GFP_KERNEL);
  307. if (!rrb)
  308. return -ENOMEM;
  309. rc = clp_list_pci(rrb, __clp_rescan);
  310. clp_free_block(rrb);
  311. return rc;
  312. }
  313. int clp_rescan_pci_devices_simple(void)
  314. {
  315. struct clp_req_rsp_list_pci *rrb;
  316. int rc;
  317. rrb = clp_alloc_block(GFP_NOWAIT);
  318. if (!rrb)
  319. return -ENOMEM;
  320. rc = clp_list_pci(rrb, __clp_update);
  321. clp_free_block(rrb);
  322. return rc;
  323. }