pci_clp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright IBM Corp. 2012
  4. *
  5. * Author(s):
  6. * Jan Glauber <jang@linux.vnet.ibm.com>
  7. */
  8. #define KMSG_COMPONENT "zpci"
  9. #define pr_fmt(fmt) KMSG_COMPONENT ": " fmt
  10. #include <linux/compat.h>
  11. #include <linux/kernel.h>
  12. #include <linux/miscdevice.h>
  13. #include <linux/slab.h>
  14. #include <linux/err.h>
  15. #include <linux/delay.h>
  16. #include <linux/pci.h>
  17. #include <linux/uaccess.h>
  18. #include <asm/pci_debug.h>
  19. #include <asm/pci_clp.h>
  20. #include <asm/clp.h>
  21. #include <uapi/asm/clp.h>
  22. bool zpci_unique_uid;
  23. static void update_uid_checking(bool new)
  24. {
  25. if (zpci_unique_uid != new)
  26. zpci_dbg(1, "uid checking:%d\n", new);
  27. zpci_unique_uid = new;
  28. }
  29. static inline void zpci_err_clp(unsigned int rsp, int rc)
  30. {
  31. struct {
  32. unsigned int rsp;
  33. int rc;
  34. } __packed data = {rsp, rc};
  35. zpci_err_hex(&data, sizeof(data));
  36. }
  37. /*
  38. * Call Logical Processor with c=1, lps=0 and command 1
  39. * to get the bit mask of installed logical processors
  40. */
  41. static inline int clp_get_ilp(unsigned long *ilp)
  42. {
  43. unsigned long mask;
  44. int cc = 3;
  45. asm volatile (
  46. " .insn rrf,0xb9a00000,%[mask],%[cmd],8,0\n"
  47. "0: ipm %[cc]\n"
  48. " srl %[cc],28\n"
  49. "1:\n"
  50. EX_TABLE(0b, 1b)
  51. : [cc] "+d" (cc), [mask] "=d" (mask) : [cmd] "a" (1)
  52. : "cc");
  53. *ilp = mask;
  54. return cc;
  55. }
  56. /*
  57. * Call Logical Processor with c=0, the give constant lps and an lpcb request.
  58. */
  59. static inline int clp_req(void *data, unsigned int lps)
  60. {
  61. struct { u8 _[CLP_BLK_SIZE]; } *req = data;
  62. u64 ignored;
  63. int cc = 3;
  64. asm volatile (
  65. " .insn rrf,0xb9a00000,%[ign],%[req],0,%[lps]\n"
  66. "0: ipm %[cc]\n"
  67. " srl %[cc],28\n"
  68. "1:\n"
  69. EX_TABLE(0b, 1b)
  70. : [cc] "+d" (cc), [ign] "=d" (ignored), "+m" (*req)
  71. : [req] "a" (req), [lps] "i" (lps)
  72. : "cc");
  73. return cc;
  74. }
  75. static void *clp_alloc_block(gfp_t gfp_mask)
  76. {
  77. return (void *) __get_free_pages(gfp_mask, get_order(CLP_BLK_SIZE));
  78. }
  79. static void clp_free_block(void *ptr)
  80. {
  81. free_pages((unsigned long) ptr, get_order(CLP_BLK_SIZE));
  82. }
  83. static void clp_store_query_pci_fngrp(struct zpci_dev *zdev,
  84. struct clp_rsp_query_pci_grp *response)
  85. {
  86. zdev->tlb_refresh = response->refresh;
  87. zdev->dma_mask = response->dasm;
  88. zdev->msi_addr = response->msia;
  89. zdev->max_msi = response->noi;
  90. zdev->fmb_update = response->mui;
  91. switch (response->version) {
  92. case 1:
  93. zdev->max_bus_speed = PCIE_SPEED_5_0GT;
  94. break;
  95. default:
  96. zdev->max_bus_speed = PCI_SPEED_UNKNOWN;
  97. break;
  98. }
  99. }
  100. static int clp_query_pci_fngrp(struct zpci_dev *zdev, u8 pfgid)
  101. {
  102. struct clp_req_rsp_query_pci_grp *rrb;
  103. int rc;
  104. rrb = clp_alloc_block(GFP_KERNEL);
  105. if (!rrb)
  106. return -ENOMEM;
  107. memset(rrb, 0, sizeof(*rrb));
  108. rrb->request.hdr.len = sizeof(rrb->request);
  109. rrb->request.hdr.cmd = CLP_QUERY_PCI_FNGRP;
  110. rrb->response.hdr.len = sizeof(rrb->response);
  111. rrb->request.pfgid = pfgid;
  112. rc = clp_req(rrb, CLP_LPS_PCI);
  113. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
  114. clp_store_query_pci_fngrp(zdev, &rrb->response);
  115. else {
  116. zpci_err("Q PCI FGRP:\n");
  117. zpci_err_clp(rrb->response.hdr.rsp, rc);
  118. rc = -EIO;
  119. }
  120. clp_free_block(rrb);
  121. return rc;
  122. }
  123. static int clp_store_query_pci_fn(struct zpci_dev *zdev,
  124. struct clp_rsp_query_pci *response)
  125. {
  126. int i;
  127. for (i = 0; i < PCI_BAR_COUNT; i++) {
  128. zdev->bars[i].val = le32_to_cpu(response->bar[i]);
  129. zdev->bars[i].size = response->bar_size[i];
  130. }
  131. zdev->start_dma = response->sdma;
  132. zdev->end_dma = response->edma;
  133. zdev->pchid = response->pchid;
  134. zdev->pfgid = response->pfgid;
  135. zdev->pft = response->pft;
  136. zdev->vfn = response->vfn;
  137. zdev->uid = response->uid;
  138. zdev->fmb_length = sizeof(u32) * response->fmb_len;
  139. memcpy(zdev->pfip, response->pfip, sizeof(zdev->pfip));
  140. if (response->util_str_avail) {
  141. memcpy(zdev->util_str, response->util_str,
  142. sizeof(zdev->util_str));
  143. }
  144. return 0;
  145. }
  146. static int clp_query_pci_fn(struct zpci_dev *zdev, u32 fh)
  147. {
  148. struct clp_req_rsp_query_pci *rrb;
  149. int rc;
  150. rrb = clp_alloc_block(GFP_KERNEL);
  151. if (!rrb)
  152. return -ENOMEM;
  153. memset(rrb, 0, sizeof(*rrb));
  154. rrb->request.hdr.len = sizeof(rrb->request);
  155. rrb->request.hdr.cmd = CLP_QUERY_PCI_FN;
  156. rrb->response.hdr.len = sizeof(rrb->response);
  157. rrb->request.fh = fh;
  158. rc = clp_req(rrb, CLP_LPS_PCI);
  159. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK) {
  160. rc = clp_store_query_pci_fn(zdev, &rrb->response);
  161. if (rc)
  162. goto out;
  163. rc = clp_query_pci_fngrp(zdev, rrb->response.pfgid);
  164. } else {
  165. zpci_err("Q PCI FN:\n");
  166. zpci_err_clp(rrb->response.hdr.rsp, rc);
  167. rc = -EIO;
  168. }
  169. out:
  170. clp_free_block(rrb);
  171. return rc;
  172. }
  173. int clp_add_pci_device(u32 fid, u32 fh, int configured)
  174. {
  175. struct zpci_dev *zdev;
  176. int rc = -ENOMEM;
  177. zpci_dbg(3, "add fid:%x, fh:%x, c:%d\n", fid, fh, configured);
  178. zdev = kzalloc(sizeof(*zdev), GFP_KERNEL);
  179. if (!zdev)
  180. goto error;
  181. zdev->fh = fh;
  182. zdev->fid = fid;
  183. /* Query function properties and update zdev */
  184. rc = clp_query_pci_fn(zdev, fh);
  185. if (rc)
  186. goto error;
  187. if (configured)
  188. zdev->state = ZPCI_FN_STATE_CONFIGURED;
  189. else
  190. zdev->state = ZPCI_FN_STATE_STANDBY;
  191. rc = zpci_create_device(zdev);
  192. if (rc)
  193. goto error;
  194. return 0;
  195. error:
  196. zpci_dbg(0, "add fid:%x, rc:%d\n", fid, rc);
  197. kfree(zdev);
  198. return rc;
  199. }
  200. /*
  201. * Enable/Disable a given PCI function defined by its function handle.
  202. */
  203. static int clp_set_pci_fn(u32 *fh, u8 nr_dma_as, u8 command)
  204. {
  205. struct clp_req_rsp_set_pci *rrb;
  206. int rc, retries = 100;
  207. rrb = clp_alloc_block(GFP_KERNEL);
  208. if (!rrb)
  209. return -ENOMEM;
  210. do {
  211. memset(rrb, 0, sizeof(*rrb));
  212. rrb->request.hdr.len = sizeof(rrb->request);
  213. rrb->request.hdr.cmd = CLP_SET_PCI_FN;
  214. rrb->response.hdr.len = sizeof(rrb->response);
  215. rrb->request.fh = *fh;
  216. rrb->request.oc = command;
  217. rrb->request.ndas = nr_dma_as;
  218. rc = clp_req(rrb, CLP_LPS_PCI);
  219. if (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY) {
  220. retries--;
  221. if (retries < 0)
  222. break;
  223. msleep(20);
  224. }
  225. } while (rrb->response.hdr.rsp == CLP_RC_SETPCIFN_BUSY);
  226. if (!rc && rrb->response.hdr.rsp == CLP_RC_OK)
  227. *fh = rrb->response.fh;
  228. else {
  229. zpci_err("Set PCI FN:\n");
  230. zpci_err_clp(rrb->response.hdr.rsp, rc);
  231. rc = -EIO;
  232. }
  233. clp_free_block(rrb);
  234. return rc;
  235. }
  236. int clp_enable_fh(struct zpci_dev *zdev, u8 nr_dma_as)
  237. {
  238. u32 fh = zdev->fh;
  239. int rc;
  240. rc = clp_set_pci_fn(&fh, nr_dma_as, CLP_SET_ENABLE_PCI_FN);
  241. if (!rc)
  242. /* Success -> store enabled handle in zdev */
  243. zdev->fh = fh;
  244. zpci_dbg(3, "ena fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
  245. return rc;
  246. }
  247. int clp_disable_fh(struct zpci_dev *zdev)
  248. {
  249. u32 fh = zdev->fh;
  250. int rc;
  251. if (!zdev_enabled(zdev))
  252. return 0;
  253. rc = clp_set_pci_fn(&fh, 0, CLP_SET_DISABLE_PCI_FN);
  254. if (!rc)
  255. /* Success -> store disabled handle in zdev */
  256. zdev->fh = fh;
  257. zpci_dbg(3, "dis fid:%x, fh:%x, rc:%d\n", zdev->fid, zdev->fh, rc);
  258. return rc;
  259. }
  260. static int clp_list_pci(struct clp_req_rsp_list_pci *rrb, void *data,
  261. void (*cb)(struct clp_fh_list_entry *, void *))
  262. {
  263. u64 resume_token = 0;
  264. int entries, i, rc;
  265. do {
  266. memset(rrb, 0, sizeof(*rrb));
  267. rrb->request.hdr.len = sizeof(rrb->request);
  268. rrb->request.hdr.cmd = CLP_LIST_PCI;
  269. /* store as many entries as possible */
  270. rrb->response.hdr.len = CLP_BLK_SIZE - LIST_PCI_HDR_LEN;
  271. rrb->request.resume_token = resume_token;
  272. /* Get PCI function handle list */
  273. rc = clp_req(rrb, CLP_LPS_PCI);
  274. if (rc || rrb->response.hdr.rsp != CLP_RC_OK) {
  275. zpci_err("List PCI FN:\n");
  276. zpci_err_clp(rrb->response.hdr.rsp, rc);
  277. rc = -EIO;
  278. goto out;
  279. }
  280. update_uid_checking(rrb->response.uid_checking);
  281. WARN_ON_ONCE(rrb->response.entry_size !=
  282. sizeof(struct clp_fh_list_entry));
  283. entries = (rrb->response.hdr.len - LIST_PCI_HDR_LEN) /
  284. rrb->response.entry_size;
  285. resume_token = rrb->response.resume_token;
  286. for (i = 0; i < entries; i++)
  287. cb(&rrb->response.fh_list[i], data);
  288. } while (resume_token);
  289. out:
  290. return rc;
  291. }
  292. static void __clp_add(struct clp_fh_list_entry *entry, void *data)
  293. {
  294. struct zpci_dev *zdev;
  295. if (!entry->vendor_id)
  296. return;
  297. zdev = get_zdev_by_fid(entry->fid);
  298. if (!zdev)
  299. clp_add_pci_device(entry->fid, entry->fh, entry->config_state);
  300. }
  301. static void __clp_update(struct clp_fh_list_entry *entry, void *data)
  302. {
  303. struct zpci_dev *zdev;
  304. if (!entry->vendor_id)
  305. return;
  306. zdev = get_zdev_by_fid(entry->fid);
  307. if (!zdev)
  308. return;
  309. zdev->fh = entry->fh;
  310. }
  311. int clp_scan_pci_devices(void)
  312. {
  313. struct clp_req_rsp_list_pci *rrb;
  314. int rc;
  315. rrb = clp_alloc_block(GFP_KERNEL);
  316. if (!rrb)
  317. return -ENOMEM;
  318. rc = clp_list_pci(rrb, NULL, __clp_add);
  319. clp_free_block(rrb);
  320. return rc;
  321. }
  322. int clp_rescan_pci_devices(void)
  323. {
  324. struct clp_req_rsp_list_pci *rrb;
  325. int rc;
  326. zpci_remove_reserved_devices();
  327. rrb = clp_alloc_block(GFP_KERNEL);
  328. if (!rrb)
  329. return -ENOMEM;
  330. rc = clp_list_pci(rrb, NULL, __clp_add);
  331. clp_free_block(rrb);
  332. return rc;
  333. }
  334. int clp_rescan_pci_devices_simple(void)
  335. {
  336. struct clp_req_rsp_list_pci *rrb;
  337. int rc;
  338. rrb = clp_alloc_block(GFP_NOWAIT);
  339. if (!rrb)
  340. return -ENOMEM;
  341. rc = clp_list_pci(rrb, NULL, __clp_update);
  342. clp_free_block(rrb);
  343. return rc;
  344. }
  345. struct clp_state_data {
  346. u32 fid;
  347. enum zpci_state state;
  348. };
  349. static void __clp_get_state(struct clp_fh_list_entry *entry, void *data)
  350. {
  351. struct clp_state_data *sd = data;
  352. if (entry->fid != sd->fid)
  353. return;
  354. sd->state = entry->config_state;
  355. }
  356. int clp_get_state(u32 fid, enum zpci_state *state)
  357. {
  358. struct clp_req_rsp_list_pci *rrb;
  359. struct clp_state_data sd = {fid, ZPCI_FN_STATE_RESERVED};
  360. int rc;
  361. rrb = clp_alloc_block(GFP_ATOMIC);
  362. if (!rrb)
  363. return -ENOMEM;
  364. rc = clp_list_pci(rrb, &sd, __clp_get_state);
  365. if (!rc)
  366. *state = sd.state;
  367. clp_free_block(rrb);
  368. return rc;
  369. }
  370. static int clp_base_slpc(struct clp_req *req, struct clp_req_rsp_slpc *lpcb)
  371. {
  372. unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
  373. if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
  374. lpcb->response.hdr.len > limit)
  375. return -EINVAL;
  376. return clp_req(lpcb, CLP_LPS_BASE) ? -EOPNOTSUPP : 0;
  377. }
  378. static int clp_base_command(struct clp_req *req, struct clp_req_hdr *lpcb)
  379. {
  380. switch (lpcb->cmd) {
  381. case 0x0001: /* store logical-processor characteristics */
  382. return clp_base_slpc(req, (void *) lpcb);
  383. default:
  384. return -EINVAL;
  385. }
  386. }
  387. static int clp_pci_slpc(struct clp_req *req, struct clp_req_rsp_slpc *lpcb)
  388. {
  389. unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
  390. if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
  391. lpcb->response.hdr.len > limit)
  392. return -EINVAL;
  393. return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
  394. }
  395. static int clp_pci_list(struct clp_req *req, struct clp_req_rsp_list_pci *lpcb)
  396. {
  397. unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
  398. if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
  399. lpcb->response.hdr.len > limit)
  400. return -EINVAL;
  401. if (lpcb->request.reserved2 != 0)
  402. return -EINVAL;
  403. return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
  404. }
  405. static int clp_pci_query(struct clp_req *req,
  406. struct clp_req_rsp_query_pci *lpcb)
  407. {
  408. unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
  409. if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
  410. lpcb->response.hdr.len > limit)
  411. return -EINVAL;
  412. if (lpcb->request.reserved2 != 0 || lpcb->request.reserved3 != 0)
  413. return -EINVAL;
  414. return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
  415. }
  416. static int clp_pci_query_grp(struct clp_req *req,
  417. struct clp_req_rsp_query_pci_grp *lpcb)
  418. {
  419. unsigned long limit = PAGE_SIZE - sizeof(lpcb->request);
  420. if (lpcb->request.hdr.len != sizeof(lpcb->request) ||
  421. lpcb->response.hdr.len > limit)
  422. return -EINVAL;
  423. if (lpcb->request.reserved2 != 0 || lpcb->request.reserved3 != 0 ||
  424. lpcb->request.reserved4 != 0)
  425. return -EINVAL;
  426. return clp_req(lpcb, CLP_LPS_PCI) ? -EOPNOTSUPP : 0;
  427. }
  428. static int clp_pci_command(struct clp_req *req, struct clp_req_hdr *lpcb)
  429. {
  430. switch (lpcb->cmd) {
  431. case 0x0001: /* store logical-processor characteristics */
  432. return clp_pci_slpc(req, (void *) lpcb);
  433. case 0x0002: /* list PCI functions */
  434. return clp_pci_list(req, (void *) lpcb);
  435. case 0x0003: /* query PCI function */
  436. return clp_pci_query(req, (void *) lpcb);
  437. case 0x0004: /* query PCI function group */
  438. return clp_pci_query_grp(req, (void *) lpcb);
  439. default:
  440. return -EINVAL;
  441. }
  442. }
  443. static int clp_normal_command(struct clp_req *req)
  444. {
  445. struct clp_req_hdr *lpcb;
  446. void __user *uptr;
  447. int rc;
  448. rc = -EINVAL;
  449. if (req->lps != 0 && req->lps != 2)
  450. goto out;
  451. rc = -ENOMEM;
  452. lpcb = clp_alloc_block(GFP_KERNEL);
  453. if (!lpcb)
  454. goto out;
  455. rc = -EFAULT;
  456. uptr = (void __force __user *)(unsigned long) req->data_p;
  457. if (copy_from_user(lpcb, uptr, PAGE_SIZE) != 0)
  458. goto out_free;
  459. rc = -EINVAL;
  460. if (lpcb->fmt != 0 || lpcb->reserved1 != 0 || lpcb->reserved2 != 0)
  461. goto out_free;
  462. switch (req->lps) {
  463. case 0:
  464. rc = clp_base_command(req, lpcb);
  465. break;
  466. case 2:
  467. rc = clp_pci_command(req, lpcb);
  468. break;
  469. }
  470. if (rc)
  471. goto out_free;
  472. rc = -EFAULT;
  473. if (copy_to_user(uptr, lpcb, PAGE_SIZE) != 0)
  474. goto out_free;
  475. rc = 0;
  476. out_free:
  477. clp_free_block(lpcb);
  478. out:
  479. return rc;
  480. }
  481. static int clp_immediate_command(struct clp_req *req)
  482. {
  483. void __user *uptr;
  484. unsigned long ilp;
  485. int exists;
  486. if (req->cmd > 1 || clp_get_ilp(&ilp) != 0)
  487. return -EINVAL;
  488. uptr = (void __force __user *)(unsigned long) req->data_p;
  489. if (req->cmd == 0) {
  490. /* Command code 0: test for a specific processor */
  491. exists = test_bit_inv(req->lps, &ilp);
  492. return put_user(exists, (int __user *) uptr);
  493. }
  494. /* Command code 1: return bit mask of installed processors */
  495. return put_user(ilp, (unsigned long __user *) uptr);
  496. }
  497. static long clp_misc_ioctl(struct file *filp, unsigned int cmd,
  498. unsigned long arg)
  499. {
  500. struct clp_req req;
  501. void __user *argp;
  502. if (cmd != CLP_SYNC)
  503. return -EINVAL;
  504. argp = is_compat_task() ? compat_ptr(arg) : (void __user *) arg;
  505. if (copy_from_user(&req, argp, sizeof(req)))
  506. return -EFAULT;
  507. if (req.r != 0)
  508. return -EINVAL;
  509. return req.c ? clp_immediate_command(&req) : clp_normal_command(&req);
  510. }
  511. static int clp_misc_release(struct inode *inode, struct file *filp)
  512. {
  513. return 0;
  514. }
  515. static const struct file_operations clp_misc_fops = {
  516. .owner = THIS_MODULE,
  517. .open = nonseekable_open,
  518. .release = clp_misc_release,
  519. .unlocked_ioctl = clp_misc_ioctl,
  520. .compat_ioctl = clp_misc_ioctl,
  521. .llseek = no_llseek,
  522. };
  523. static struct miscdevice clp_misc_device = {
  524. .minor = MISC_DYNAMIC_MINOR,
  525. .name = "clp",
  526. .fops = &clp_misc_fops,
  527. };
  528. static int __init clp_misc_init(void)
  529. {
  530. return misc_register(&clp_misc_device);
  531. }
  532. device_initcall(clp_misc_init);