pci_clp.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * Copyright IBM Corp. 2012
  3. *
  4. * Author(s):
  5. * Jan Glauber <jang@linux.vnet.ibm.com>
  6. */
  7. #define KMSG_COMPONENT "zpci"
  8. #define pr_fmt(fmt) KMSG_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->max_msi = response->noi;
  57. zdev->fmb_update = response->mui;
  58. switch (response->version) {
  59. case 1:
  60. zdev->max_bus_speed = PCIE_SPEED_5_0GT;
  61. break;
  62. default:
  63. zdev->max_bus_speed = PCI_SPEED_UNKNOWN;
  64. break;
  65. }
  66. }
  67. static int clp_query_pci_fngrp(struct zpci_dev *zdev, u8 pfgid)
  68. {
  69. struct clp_req_rsp_query_pci_grp *rrb;
  70. int rc;
  71. rrb = clp_alloc_block(GFP_KERNEL);
  72. if (!rrb)
  73. return -ENOMEM;
  74. memset(rrb, 0, sizeof(*rrb));
  75. rrb->request.hdr.len = sizeof(rrb->request);
  76. rrb->request.hdr.cmd = CLP_QUERY_PCI_FNGRP;
  77. rrb->response.hdr.len = sizeof(rrb->response);
  78. rrb->request.pfgid = pfgid;
  79. rc = clp_instr(rrb);
  80. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
  81. clp_store_query_pci_fngrp(zdev, &rrb->response);
  82. else {
  83. zpci_err("Q PCI FGRP:\n");
  84. zpci_err_clp(rrb->response.hdr.rsp, rc);
  85. rc = -EIO;
  86. }
  87. clp_free_block(rrb);
  88. return rc;
  89. }
  90. static int clp_store_query_pci_fn(struct zpci_dev *zdev,
  91. struct clp_rsp_query_pci *response)
  92. {
  93. int i;
  94. for (i = 0; i < PCI_BAR_COUNT; i++) {
  95. zdev->bars[i].val = le32_to_cpu(response->bar[i]);
  96. zdev->bars[i].size = response->bar_size[i];
  97. }
  98. zdev->start_dma = response->sdma;
  99. zdev->end_dma = response->edma;
  100. zdev->pchid = response->pchid;
  101. zdev->pfgid = response->pfgid;
  102. zdev->pft = response->pft;
  103. zdev->vfn = response->vfn;
  104. zdev->uid = response->uid;
  105. memcpy(zdev->pfip, response->pfip, sizeof(zdev->pfip));
  106. if (response->util_str_avail) {
  107. memcpy(zdev->util_str, response->util_str,
  108. sizeof(zdev->util_str));
  109. }
  110. return 0;
  111. }
  112. static int clp_query_pci_fn(struct zpci_dev *zdev, u32 fh)
  113. {
  114. struct clp_req_rsp_query_pci *rrb;
  115. int rc;
  116. rrb = clp_alloc_block(GFP_KERNEL);
  117. if (!rrb)
  118. return -ENOMEM;
  119. memset(rrb, 0, sizeof(*rrb));
  120. rrb->request.hdr.len = sizeof(rrb->request);
  121. rrb->request.hdr.cmd = CLP_QUERY_PCI_FN;
  122. rrb->response.hdr.len = sizeof(rrb->response);
  123. rrb->request.fh = fh;
  124. rc = clp_instr(rrb);
  125. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
  126. rc = clp_store_query_pci_fn(zdev, &rrb->response);
  127. if (rc)
  128. goto out;
  129. if (rrb->response.pfgid)
  130. rc = clp_query_pci_fngrp(zdev, rrb->response.pfgid);
  131. } else {
  132. zpci_err("Q PCI FN:\n");
  133. zpci_err_clp(rrb->response.hdr.rsp, rc);
  134. rc = -EIO;
  135. }
  136. out:
  137. clp_free_block(rrb);
  138. return rc;
  139. }
  140. int clp_add_pci_device(u32 fid, u32 fh, int configured)
  141. {
  142. struct zpci_dev *zdev;
  143. int rc;
  144. zpci_dbg(3, "add fid:%x, fh:%x, c:%d\n", fid, fh, configured);
  145. zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
  146. if (!zdev)
  147. return -ENOMEM;
  148. zdev->fh = fh;
  149. zdev->fid = fid;
  150. /* Query function properties and update zdev */
  151. rc = clp_query_pci_fn(zdev, fh);
  152. if (rc)
  153. goto error;
  154. if (configured)
  155. zdev->state = ZPCI_FN_STATE_CONFIGURED;
  156. else
  157. zdev->state = ZPCI_FN_STATE_STANDBY;
  158. rc = zpci_create_device(zdev);
  159. if (rc)
  160. goto error;
  161. return 0;
  162. error:
  163. kfree(zdev);
  164. return rc;
  165. }
  166. /*
  167. * Enable/Disable a given PCI function defined by its function handle.
  168. */
  169. static int clp_set_pci_fn(u32 *fh, u8 nr_dma_as, u8 command)
  170. {
  171. struct clp_req_rsp_set_pci *rrb;
  172. int rc, retries = 100;
  173. rrb = clp_alloc_block(GFP_KERNEL);
  174. if (!rrb)
  175. return -ENOMEM;
  176. do {
  177. memset(rrb, 0, sizeof(*rrb));
  178. rrb->request.hdr.len = sizeof(rrb->request);
  179. rrb->request.hdr.cmd = CLP_SET_PCI_FN;
  180. rrb->response.hdr.len = sizeof(rrb->response);
  181. rrb->request.fh = *fh;
  182. rrb->request.oc = command;
  183. rrb->request.ndas = nr_dma_as;
  184. rc = clp_instr(rrb);
  185. if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) {
  186. retries--;
  187. if (retries < 0)
  188. break;
  189. msleep(20);
  190. }
  191. } while (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY);
  192. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
  193. *fh = rrb->response.fh;
  194. else {
  195. zpci_err("Set PCI FN:\n");
  196. zpci_err_clp(rrb->response.hdr.rsp, rc);
  197. rc = -EIO;
  198. }
  199. clp_free_block(rrb);
  200. return rc;
  201. }
  202. int clp_enable_fh(struct zpci_dev *zdev, u8 nr_dma_as)
  203. {
  204. u32 fh = zdev->fh;
  205. int rc;
  206. rc = clp_set_pci_fn(&fh, nr_dma_as, CLP_SET_ENABLE_PCI_FN);
  207. if (!rc)
  208. /* Success -> store enabled handle in zdev */
  209. zdev->fh = fh;
  210. zpci_dbg(3, "ena fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
  211. return rc;
  212. }
  213. int clp_disable_fh(struct zpci_dev *zdev)
  214. {
  215. u32 fh = zdev->fh;
  216. int rc;
  217. if (!zdev_enabled(zdev))
  218. return 0;
  219. rc = clp_set_pci_fn(&fh, 0, CLP_SET_DISABLE_PCI_FN);
  220. if (!rc)
  221. /* Success -> store disabled handle in zdev */
  222. zdev->fh = fh;
  223. zpci_dbg(3, "dis fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
  224. return rc;
  225. }
  226. static int clp_list_pci(struct clp_req_rsp_list_pci *rrb,
  227. void (*cb)(struct clp_fh_list_entry *entry))
  228. {
  229. u64 resume_token = 0;
  230. int entries, i, rc;
  231. do {
  232. memset(rrb, 0, sizeof(*rrb));
  233. rrb->request.hdr.len = sizeof(rrb->request);
  234. rrb->request.hdr.cmd = CLP_LIST_PCI;
  235. /* store as many entries as possible */
  236. rrb->response.hdr.len = CLP_BLK_SIZE - LIST_PCI_HDR_LEN;
  237. rrb->request.resume_token = resume_token;
  238. /* Get PCI function handle list */
  239. rc = clp_instr(rrb);
  240. if (rc || rrb->response.hdr.rsp != CLP_RC_OK) {
  241. zpci_err("List PCI FN:\n");
  242. zpci_err_clp(rrb->response.hdr.rsp, rc);
  243. rc = -EIO;
  244. goto out;
  245. }
  246. WARN_ON_ONCE(rrb->response.entry_size !=
  247. sizeof(struct clp_fh_list_entry));
  248. entries = (rrb->response.hdr.len - LIST_PCI_HDR_LEN) /
  249. rrb->response.entry_size;
  250. resume_token = rrb->response.resume_token;
  251. for (i = 0; i < entries; i++)
  252. cb(&rrb->response.fh_list[i]);
  253. } while (resume_token);
  254. out:
  255. return rc;
  256. }
  257. static void __clp_add(struct clp_fh_list_entry *entry)
  258. {
  259. if (!entry->vendor_id)
  260. return;
  261. clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
  262. }
  263. static void __clp_rescan(struct clp_fh_list_entry *entry)
  264. {
  265. struct zpci_dev *zdev;
  266. if (!entry->vendor_id)
  267. return;
  268. zdev = get_zdev_by_fid(entry->fid);
  269. if (!zdev) {
  270. clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
  271. return;
  272. }
  273. if (!entry->config_state) {
  274. /*
  275. * The handle is already disabled, that means no iota/irq freeing via
  276. * the firmware interfaces anymore. Need to free resources manually
  277. * (DMA memory, debug, sysfs)...
  278. */
  279. zpci_stop_device(zdev);
  280. }
  281. }
  282. static void __clp_update(struct clp_fh_list_entry *entry)
  283. {
  284. struct zpci_dev *zdev;
  285. if (!entry->vendor_id)
  286. return;
  287. zdev = get_zdev_by_fid(entry->fid);
  288. if (!zdev)
  289. return;
  290. zdev->fh = entry->fh;
  291. }
  292. int clp_scan_pci_devices(void)
  293. {
  294. struct clp_req_rsp_list_pci *rrb;
  295. int rc;
  296. rrb = clp_alloc_block(GFP_KERNEL);
  297. if (!rrb)
  298. return -ENOMEM;
  299. rc = clp_list_pci(rrb, __clp_add);
  300. clp_free_block(rrb);
  301. return rc;
  302. }
  303. int clp_rescan_pci_devices(void)
  304. {
  305. struct clp_req_rsp_list_pci *rrb;
  306. int rc;
  307. rrb = clp_alloc_block(GFP_KERNEL);
  308. if (!rrb)
  309. return -ENOMEM;
  310. rc = clp_list_pci(rrb, __clp_rescan);
  311. clp_free_block(rrb);
  312. return rc;
  313. }
  314. int clp_rescan_pci_devices_simple(void)
  315. {
  316. struct clp_req_rsp_list_pci *rrb;
  317. int rc;
  318. rrb = clp_alloc_block(GFP_NOWAIT);
  319. if (!rrb)
  320. return -ENOMEM;
  321. rc = clp_list_pci(rrb, __clp_update);
  322. clp_free_block(rrb);
  323. return rc;
  324. }