vfio_ccw_cp.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * channel program interfaces
  4. *
  5. * Copyright IBM Corp. 2017
  6. *
  7. * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
  8. * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
  9. */
  10. #include <linux/mm.h>
  11. #include <linux/slab.h>
  12. #include <linux/iommu.h>
  13. #include <linux/vfio.h>
  14. #include <asm/idals.h>
  15. #include "vfio_ccw_cp.h"
  16. /*
  17. * Max length for ccw chain.
  18. * XXX: Limit to 256, need to check more?
  19. */
  20. #define CCWCHAIN_LEN_MAX 256
  21. struct pfn_array {
  22. /* Starting guest physical I/O address. */
  23. unsigned long pa_iova;
  24. /* Array that stores PFNs of the pages need to pin. */
  25. unsigned long *pa_iova_pfn;
  26. /* Array that receives PFNs of the pages pinned. */
  27. unsigned long *pa_pfn;
  28. /* Number of pages pinned from @pa_iova. */
  29. int pa_nr;
  30. };
  31. struct pfn_array_table {
  32. struct pfn_array *pat_pa;
  33. int pat_nr;
  34. };
  35. struct ccwchain {
  36. struct list_head next;
  37. struct ccw1 *ch_ccw;
  38. /* Guest physical address of the current chain. */
  39. u64 ch_iova;
  40. /* Count of the valid ccws in chain. */
  41. int ch_len;
  42. /* Pinned PAGEs for the original data. */
  43. struct pfn_array_table *ch_pat;
  44. };
  45. /*
  46. * pfn_array_alloc_pin() - alloc memory for PFNs, then pin user pages in memory
  47. * @pa: pfn_array on which to perform the operation
  48. * @mdev: the mediated device to perform pin/unpin operations
  49. * @iova: target guest physical address
  50. * @len: number of bytes that should be pinned from @iova
  51. *
  52. * Attempt to allocate memory for PFNs, and pin user pages in memory.
  53. *
  54. * Usage of pfn_array:
  55. * We expect (pa_nr == 0) and (pa_iova_pfn == NULL), any field in
  56. * this structure will be filled in by this function.
  57. *
  58. * Returns:
  59. * Number of pages pinned on success.
  60. * If @pa->pa_nr is not 0, or @pa->pa_iova_pfn is not NULL initially,
  61. * returns -EINVAL.
  62. * If no pages were pinned, returns -errno.
  63. */
  64. static int pfn_array_alloc_pin(struct pfn_array *pa, struct device *mdev,
  65. u64 iova, unsigned int len)
  66. {
  67. int i, ret = 0;
  68. if (!len)
  69. return 0;
  70. if (pa->pa_nr || pa->pa_iova_pfn)
  71. return -EINVAL;
  72. pa->pa_iova = iova;
  73. pa->pa_nr = ((iova & ~PAGE_MASK) + len + (PAGE_SIZE - 1)) >> PAGE_SHIFT;
  74. if (!pa->pa_nr)
  75. return -EINVAL;
  76. pa->pa_iova_pfn = kcalloc(pa->pa_nr,
  77. sizeof(*pa->pa_iova_pfn) +
  78. sizeof(*pa->pa_pfn),
  79. GFP_KERNEL);
  80. if (unlikely(!pa->pa_iova_pfn)) {
  81. pa->pa_nr = 0;
  82. return -ENOMEM;
  83. }
  84. pa->pa_pfn = pa->pa_iova_pfn + pa->pa_nr;
  85. pa->pa_iova_pfn[0] = pa->pa_iova >> PAGE_SHIFT;
  86. for (i = 1; i < pa->pa_nr; i++)
  87. pa->pa_iova_pfn[i] = pa->pa_iova_pfn[i - 1] + 1;
  88. ret = vfio_pin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr,
  89. IOMMU_READ | IOMMU_WRITE, pa->pa_pfn);
  90. if (ret < 0) {
  91. goto err_out;
  92. } else if (ret > 0 && ret != pa->pa_nr) {
  93. vfio_unpin_pages(mdev, pa->pa_iova_pfn, ret);
  94. ret = -EINVAL;
  95. goto err_out;
  96. }
  97. return ret;
  98. err_out:
  99. pa->pa_nr = 0;
  100. kfree(pa->pa_iova_pfn);
  101. pa->pa_iova_pfn = NULL;
  102. return ret;
  103. }
  104. /* Unpin the pages before releasing the memory. */
  105. static void pfn_array_unpin_free(struct pfn_array *pa, struct device *mdev)
  106. {
  107. vfio_unpin_pages(mdev, pa->pa_iova_pfn, pa->pa_nr);
  108. pa->pa_nr = 0;
  109. kfree(pa->pa_iova_pfn);
  110. }
  111. static int pfn_array_table_init(struct pfn_array_table *pat, int nr)
  112. {
  113. pat->pat_pa = kcalloc(nr, sizeof(*pat->pat_pa), GFP_KERNEL);
  114. if (unlikely(ZERO_OR_NULL_PTR(pat->pat_pa))) {
  115. pat->pat_nr = 0;
  116. return -ENOMEM;
  117. }
  118. pat->pat_nr = nr;
  119. return 0;
  120. }
  121. static void pfn_array_table_unpin_free(struct pfn_array_table *pat,
  122. struct device *mdev)
  123. {
  124. int i;
  125. for (i = 0; i < pat->pat_nr; i++)
  126. pfn_array_unpin_free(pat->pat_pa + i, mdev);
  127. if (pat->pat_nr) {
  128. kfree(pat->pat_pa);
  129. pat->pat_pa = NULL;
  130. pat->pat_nr = 0;
  131. }
  132. }
  133. static bool pfn_array_table_iova_pinned(struct pfn_array_table *pat,
  134. unsigned long iova)
  135. {
  136. struct pfn_array *pa = pat->pat_pa;
  137. unsigned long iova_pfn = iova >> PAGE_SHIFT;
  138. int i, j;
  139. for (i = 0; i < pat->pat_nr; i++, pa++)
  140. for (j = 0; j < pa->pa_nr; j++)
  141. if (pa->pa_iova_pfn[j] == iova_pfn)
  142. return true;
  143. return false;
  144. }
  145. /* Create the list idal words for a pfn_array_table. */
  146. static inline void pfn_array_table_idal_create_words(
  147. struct pfn_array_table *pat,
  148. unsigned long *idaws)
  149. {
  150. struct pfn_array *pa;
  151. int i, j, k;
  152. /*
  153. * Idal words (execept the first one) rely on the memory being 4k
  154. * aligned. If a user virtual address is 4K aligned, then it's
  155. * corresponding kernel physical address will also be 4K aligned. Thus
  156. * there will be no problem here to simply use the phys to create an
  157. * idaw.
  158. */
  159. k = 0;
  160. for (i = 0; i < pat->pat_nr; i++) {
  161. pa = pat->pat_pa + i;
  162. for (j = 0; j < pa->pa_nr; j++) {
  163. idaws[k] = pa->pa_pfn[j] << PAGE_SHIFT;
  164. if (k == 0)
  165. idaws[k] += pa->pa_iova & (PAGE_SIZE - 1);
  166. k++;
  167. }
  168. }
  169. }
  170. /*
  171. * Within the domain (@mdev), copy @n bytes from a guest physical
  172. * address (@iova) to a host physical address (@to).
  173. */
  174. static long copy_from_iova(struct device *mdev,
  175. void *to, u64 iova,
  176. unsigned long n)
  177. {
  178. struct pfn_array pa = {0};
  179. u64 from;
  180. int i, ret;
  181. unsigned long l, m;
  182. ret = pfn_array_alloc_pin(&pa, mdev, iova, n);
  183. if (ret <= 0)
  184. return ret;
  185. l = n;
  186. for (i = 0; i < pa.pa_nr; i++) {
  187. from = pa.pa_pfn[i] << PAGE_SHIFT;
  188. m = PAGE_SIZE;
  189. if (i == 0) {
  190. from += iova & (PAGE_SIZE - 1);
  191. m -= iova & (PAGE_SIZE - 1);
  192. }
  193. m = min(l, m);
  194. memcpy(to + (n - l), (void *)from, m);
  195. l -= m;
  196. if (l == 0)
  197. break;
  198. }
  199. pfn_array_unpin_free(&pa, mdev);
  200. return l;
  201. }
  202. static long copy_ccw_from_iova(struct channel_program *cp,
  203. struct ccw1 *to, u64 iova,
  204. unsigned long len)
  205. {
  206. struct ccw0 ccw0;
  207. struct ccw1 *pccw1;
  208. int ret;
  209. int i;
  210. ret = copy_from_iova(cp->mdev, to, iova, len * sizeof(struct ccw1));
  211. if (ret)
  212. return ret;
  213. if (!cp->orb.cmd.fmt) {
  214. pccw1 = to;
  215. for (i = 0; i < len; i++) {
  216. ccw0 = *(struct ccw0 *)pccw1;
  217. if ((pccw1->cmd_code & 0x0f) == CCW_CMD_TIC) {
  218. pccw1->cmd_code = CCW_CMD_TIC;
  219. pccw1->flags = 0;
  220. pccw1->count = 0;
  221. } else {
  222. pccw1->cmd_code = ccw0.cmd_code;
  223. pccw1->flags = ccw0.flags;
  224. pccw1->count = ccw0.count;
  225. }
  226. pccw1->cda = ccw0.cda;
  227. pccw1++;
  228. }
  229. }
  230. return ret;
  231. }
  232. /*
  233. * Helpers to operate ccwchain.
  234. */
  235. #define ccw_is_test(_ccw) (((_ccw)->cmd_code & 0x0F) == 0)
  236. #define ccw_is_noop(_ccw) ((_ccw)->cmd_code == CCW_CMD_NOOP)
  237. #define ccw_is_tic(_ccw) ((_ccw)->cmd_code == CCW_CMD_TIC)
  238. #define ccw_is_idal(_ccw) ((_ccw)->flags & CCW_FLAG_IDA)
  239. #define ccw_is_chain(_ccw) ((_ccw)->flags & (CCW_FLAG_CC | CCW_FLAG_DC))
  240. static struct ccwchain *ccwchain_alloc(struct channel_program *cp, int len)
  241. {
  242. struct ccwchain *chain;
  243. void *data;
  244. size_t size;
  245. /* Make ccw address aligned to 8. */
  246. size = ((sizeof(*chain) + 7L) & -8L) +
  247. sizeof(*chain->ch_ccw) * len +
  248. sizeof(*chain->ch_pat) * len;
  249. chain = kzalloc(size, GFP_DMA | GFP_KERNEL);
  250. if (!chain)
  251. return NULL;
  252. data = (u8 *)chain + ((sizeof(*chain) + 7L) & -8L);
  253. chain->ch_ccw = (struct ccw1 *)data;
  254. data = (u8 *)(chain->ch_ccw) + sizeof(*chain->ch_ccw) * len;
  255. chain->ch_pat = (struct pfn_array_table *)data;
  256. chain->ch_len = len;
  257. list_add_tail(&chain->next, &cp->ccwchain_list);
  258. return chain;
  259. }
  260. static void ccwchain_free(struct ccwchain *chain)
  261. {
  262. list_del(&chain->next);
  263. kfree(chain);
  264. }
  265. /* Free resource for a ccw that allocated memory for its cda. */
  266. static void ccwchain_cda_free(struct ccwchain *chain, int idx)
  267. {
  268. struct ccw1 *ccw = chain->ch_ccw + idx;
  269. if (ccw_is_test(ccw) || ccw_is_noop(ccw) || ccw_is_tic(ccw))
  270. return;
  271. if (!ccw->count)
  272. return;
  273. kfree((void *)(u64)ccw->cda);
  274. }
  275. /* Unpin the pages then free the memory resources. */
  276. static void cp_unpin_free(struct channel_program *cp)
  277. {
  278. struct ccwchain *chain, *temp;
  279. int i;
  280. list_for_each_entry_safe(chain, temp, &cp->ccwchain_list, next) {
  281. for (i = 0; i < chain->ch_len; i++) {
  282. pfn_array_table_unpin_free(chain->ch_pat + i,
  283. cp->mdev);
  284. ccwchain_cda_free(chain, i);
  285. }
  286. ccwchain_free(chain);
  287. }
  288. }
  289. /**
  290. * ccwchain_calc_length - calculate the length of the ccw chain.
  291. * @iova: guest physical address of the target ccw chain
  292. * @cp: channel_program on which to perform the operation
  293. *
  294. * This is the chain length not considering any TICs.
  295. * You need to do a new round for each TIC target.
  296. *
  297. * The program is also validated for absence of not yet supported
  298. * indirect data addressing scenarios.
  299. *
  300. * Returns: the length of the ccw chain or -errno.
  301. */
  302. static int ccwchain_calc_length(u64 iova, struct channel_program *cp)
  303. {
  304. struct ccw1 *ccw, *p;
  305. int cnt;
  306. /*
  307. * Copy current chain from guest to host kernel.
  308. * Currently the chain length is limited to CCWCHAIN_LEN_MAX (256).
  309. * So copying 2K is enough (safe).
  310. */
  311. p = ccw = kcalloc(CCWCHAIN_LEN_MAX, sizeof(*ccw), GFP_KERNEL);
  312. if (!ccw)
  313. return -ENOMEM;
  314. cnt = copy_ccw_from_iova(cp, ccw, iova, CCWCHAIN_LEN_MAX);
  315. if (cnt) {
  316. kfree(ccw);
  317. return cnt;
  318. }
  319. cnt = 0;
  320. do {
  321. cnt++;
  322. /*
  323. * As we don't want to fail direct addressing even if the
  324. * orb specified one of the unsupported formats, we defer
  325. * checking for IDAWs in unsupported formats to here.
  326. */
  327. if ((!cp->orb.cmd.c64 || cp->orb.cmd.i2k) && ccw_is_idal(ccw)) {
  328. kfree(p);
  329. return -EOPNOTSUPP;
  330. }
  331. if ((!ccw_is_chain(ccw)) && (!ccw_is_tic(ccw)))
  332. break;
  333. ccw++;
  334. } while (cnt < CCWCHAIN_LEN_MAX + 1);
  335. if (cnt == CCWCHAIN_LEN_MAX + 1)
  336. cnt = -EINVAL;
  337. kfree(p);
  338. return cnt;
  339. }
  340. static int tic_target_chain_exists(struct ccw1 *tic, struct channel_program *cp)
  341. {
  342. struct ccwchain *chain;
  343. u32 ccw_head, ccw_tail;
  344. list_for_each_entry(chain, &cp->ccwchain_list, next) {
  345. ccw_head = chain->ch_iova;
  346. ccw_tail = ccw_head + (chain->ch_len - 1) * sizeof(struct ccw1);
  347. if ((ccw_head <= tic->cda) && (tic->cda <= ccw_tail))
  348. return 1;
  349. }
  350. return 0;
  351. }
  352. static int ccwchain_loop_tic(struct ccwchain *chain,
  353. struct channel_program *cp);
  354. static int ccwchain_handle_tic(struct ccw1 *tic, struct channel_program *cp)
  355. {
  356. struct ccwchain *chain;
  357. int len, ret;
  358. /* May transfer to an existing chain. */
  359. if (tic_target_chain_exists(tic, cp))
  360. return 0;
  361. /* Get chain length. */
  362. len = ccwchain_calc_length(tic->cda, cp);
  363. if (len < 0)
  364. return len;
  365. /* Need alloc a new chain for this one. */
  366. chain = ccwchain_alloc(cp, len);
  367. if (!chain)
  368. return -ENOMEM;
  369. chain->ch_iova = tic->cda;
  370. /* Copy the new chain from user. */
  371. ret = copy_ccw_from_iova(cp, chain->ch_ccw, tic->cda, len);
  372. if (ret) {
  373. ccwchain_free(chain);
  374. return ret;
  375. }
  376. /* Loop for tics on this new chain. */
  377. return ccwchain_loop_tic(chain, cp);
  378. }
  379. /* Loop for TICs. */
  380. static int ccwchain_loop_tic(struct ccwchain *chain, struct channel_program *cp)
  381. {
  382. struct ccw1 *tic;
  383. int i, ret;
  384. for (i = 0; i < chain->ch_len; i++) {
  385. tic = chain->ch_ccw + i;
  386. if (!ccw_is_tic(tic))
  387. continue;
  388. ret = ccwchain_handle_tic(tic, cp);
  389. if (ret)
  390. return ret;
  391. }
  392. return 0;
  393. }
  394. static int ccwchain_fetch_tic(struct ccwchain *chain,
  395. int idx,
  396. struct channel_program *cp)
  397. {
  398. struct ccw1 *ccw = chain->ch_ccw + idx;
  399. struct ccwchain *iter;
  400. u32 ccw_head, ccw_tail;
  401. list_for_each_entry(iter, &cp->ccwchain_list, next) {
  402. ccw_head = iter->ch_iova;
  403. ccw_tail = ccw_head + (iter->ch_len - 1) * sizeof(struct ccw1);
  404. if ((ccw_head <= ccw->cda) && (ccw->cda <= ccw_tail)) {
  405. ccw->cda = (__u32) (addr_t) (((char *)iter->ch_ccw) +
  406. (ccw->cda - ccw_head));
  407. return 0;
  408. }
  409. }
  410. return -EFAULT;
  411. }
  412. static int ccwchain_fetch_direct(struct ccwchain *chain,
  413. int idx,
  414. struct channel_program *cp)
  415. {
  416. struct ccw1 *ccw;
  417. struct pfn_array_table *pat;
  418. unsigned long *idaws;
  419. int ret;
  420. ccw = chain->ch_ccw + idx;
  421. if (!ccw->count) {
  422. /*
  423. * We just want the translation result of any direct ccw
  424. * to be an IDA ccw, so let's add the IDA flag for it.
  425. * Although the flag will be ignored by firmware.
  426. */
  427. ccw->flags |= CCW_FLAG_IDA;
  428. return 0;
  429. }
  430. /*
  431. * Pin data page(s) in memory.
  432. * The number of pages actually is the count of the idaws which will be
  433. * needed when translating a direct ccw to a idal ccw.
  434. */
  435. pat = chain->ch_pat + idx;
  436. ret = pfn_array_table_init(pat, 1);
  437. if (ret)
  438. goto out_init;
  439. ret = pfn_array_alloc_pin(pat->pat_pa, cp->mdev, ccw->cda, ccw->count);
  440. if (ret < 0)
  441. goto out_unpin;
  442. /* Translate this direct ccw to a idal ccw. */
  443. idaws = kcalloc(ret, sizeof(*idaws), GFP_DMA | GFP_KERNEL);
  444. if (!idaws) {
  445. ret = -ENOMEM;
  446. goto out_unpin;
  447. }
  448. ccw->cda = (__u32) virt_to_phys(idaws);
  449. ccw->flags |= CCW_FLAG_IDA;
  450. pfn_array_table_idal_create_words(pat, idaws);
  451. return 0;
  452. out_unpin:
  453. pfn_array_table_unpin_free(pat, cp->mdev);
  454. out_init:
  455. ccw->cda = 0;
  456. return ret;
  457. }
  458. static int ccwchain_fetch_idal(struct ccwchain *chain,
  459. int idx,
  460. struct channel_program *cp)
  461. {
  462. struct ccw1 *ccw;
  463. struct pfn_array_table *pat;
  464. unsigned long *idaws;
  465. u64 idaw_iova;
  466. unsigned int idaw_nr, idaw_len;
  467. int i, ret;
  468. ccw = chain->ch_ccw + idx;
  469. if (!ccw->count)
  470. return 0;
  471. /* Calculate size of idaws. */
  472. ret = copy_from_iova(cp->mdev, &idaw_iova, ccw->cda, sizeof(idaw_iova));
  473. if (ret)
  474. return ret;
  475. idaw_nr = idal_nr_words((void *)(idaw_iova), ccw->count);
  476. idaw_len = idaw_nr * sizeof(*idaws);
  477. /* Pin data page(s) in memory. */
  478. pat = chain->ch_pat + idx;
  479. ret = pfn_array_table_init(pat, idaw_nr);
  480. if (ret)
  481. goto out_init;
  482. /* Translate idal ccw to use new allocated idaws. */
  483. idaws = kzalloc(idaw_len, GFP_DMA | GFP_KERNEL);
  484. if (!idaws) {
  485. ret = -ENOMEM;
  486. goto out_unpin;
  487. }
  488. ret = copy_from_iova(cp->mdev, idaws, ccw->cda, idaw_len);
  489. if (ret)
  490. goto out_free_idaws;
  491. ccw->cda = virt_to_phys(idaws);
  492. for (i = 0; i < idaw_nr; i++) {
  493. idaw_iova = *(idaws + i);
  494. ret = pfn_array_alloc_pin(pat->pat_pa + i, cp->mdev,
  495. idaw_iova, 1);
  496. if (ret < 0)
  497. goto out_free_idaws;
  498. }
  499. pfn_array_table_idal_create_words(pat, idaws);
  500. return 0;
  501. out_free_idaws:
  502. kfree(idaws);
  503. out_unpin:
  504. pfn_array_table_unpin_free(pat, cp->mdev);
  505. out_init:
  506. ccw->cda = 0;
  507. return ret;
  508. }
  509. /*
  510. * Fetch one ccw.
  511. * To reduce memory copy, we'll pin the cda page in memory,
  512. * and to get rid of the cda 2G limitiaion of ccw1, we'll translate
  513. * direct ccws to idal ccws.
  514. */
  515. static int ccwchain_fetch_one(struct ccwchain *chain,
  516. int idx,
  517. struct channel_program *cp)
  518. {
  519. struct ccw1 *ccw = chain->ch_ccw + idx;
  520. if (ccw_is_test(ccw) || ccw_is_noop(ccw))
  521. return 0;
  522. if (ccw_is_tic(ccw))
  523. return ccwchain_fetch_tic(chain, idx, cp);
  524. if (ccw_is_idal(ccw))
  525. return ccwchain_fetch_idal(chain, idx, cp);
  526. return ccwchain_fetch_direct(chain, idx, cp);
  527. }
  528. /**
  529. * cp_init() - allocate ccwchains for a channel program.
  530. * @cp: channel_program on which to perform the operation
  531. * @mdev: the mediated device to perform pin/unpin operations
  532. * @orb: control block for the channel program from the guest
  533. *
  534. * This creates one or more ccwchain(s), and copies the raw data of
  535. * the target channel program from @orb->cmd.iova to the new ccwchain(s).
  536. *
  537. * Limitations:
  538. * 1. Supports only prefetch enabled mode.
  539. * 2. Supports idal(c64) ccw chaining.
  540. * 3. Supports 4k idaw.
  541. *
  542. * Returns:
  543. * %0 on success and a negative error value on failure.
  544. */
  545. int cp_init(struct channel_program *cp, struct device *mdev, union orb *orb)
  546. {
  547. u64 iova = orb->cmd.cpa;
  548. struct ccwchain *chain;
  549. int len, ret;
  550. /*
  551. * XXX:
  552. * Only support prefetch enable mode now.
  553. */
  554. if (!orb->cmd.pfch)
  555. return -EOPNOTSUPP;
  556. INIT_LIST_HEAD(&cp->ccwchain_list);
  557. memcpy(&cp->orb, orb, sizeof(*orb));
  558. cp->mdev = mdev;
  559. /* Get chain length. */
  560. len = ccwchain_calc_length(iova, cp);
  561. if (len < 0)
  562. return len;
  563. /* Alloc mem for the head chain. */
  564. chain = ccwchain_alloc(cp, len);
  565. if (!chain)
  566. return -ENOMEM;
  567. chain->ch_iova = iova;
  568. /* Copy the head chain from guest. */
  569. ret = copy_ccw_from_iova(cp, chain->ch_ccw, iova, len);
  570. if (ret) {
  571. ccwchain_free(chain);
  572. return ret;
  573. }
  574. /* Now loop for its TICs. */
  575. ret = ccwchain_loop_tic(chain, cp);
  576. if (ret)
  577. cp_unpin_free(cp);
  578. /* It is safe to force: if not set but idals used
  579. * ccwchain_calc_length returns an error.
  580. */
  581. cp->orb.cmd.c64 = 1;
  582. return ret;
  583. }
  584. /**
  585. * cp_free() - free resources for channel program.
  586. * @cp: channel_program on which to perform the operation
  587. *
  588. * This unpins the memory pages and frees the memory space occupied by
  589. * @cp, which must have been returned by a previous call to cp_init().
  590. * Otherwise, undefined behavior occurs.
  591. */
  592. void cp_free(struct channel_program *cp)
  593. {
  594. cp_unpin_free(cp);
  595. }
  596. /**
  597. * cp_prefetch() - translate a guest physical address channel program to
  598. * a real-device runnable channel program.
  599. * @cp: channel_program on which to perform the operation
  600. *
  601. * This function translates the guest-physical-address channel program
  602. * and stores the result to ccwchain list. @cp must have been
  603. * initialized by a previous call with cp_init(). Otherwise, undefined
  604. * behavior occurs.
  605. * For each chain composing the channel program:
  606. * - On entry ch_len holds the count of CCWs to be translated.
  607. * - On exit ch_len is adjusted to the count of successfully translated CCWs.
  608. * This allows cp_free to find in ch_len the count of CCWs to free in a chain.
  609. *
  610. * The S/390 CCW Translation APIS (prefixed by 'cp_') are introduced
  611. * as helpers to do ccw chain translation inside the kernel. Basically
  612. * they accept a channel program issued by a virtual machine, and
  613. * translate the channel program to a real-device runnable channel
  614. * program.
  615. *
  616. * These APIs will copy the ccws into kernel-space buffers, and update
  617. * the guest phsical addresses with their corresponding host physical
  618. * addresses. Then channel I/O device drivers could issue the
  619. * translated channel program to real devices to perform an I/O
  620. * operation.
  621. *
  622. * These interfaces are designed to support translation only for
  623. * channel programs, which are generated and formatted by a
  624. * guest. Thus this will make it possible for things like VFIO to
  625. * leverage the interfaces to passthrough a channel I/O mediated
  626. * device in QEMU.
  627. *
  628. * We support direct ccw chaining by translating them to idal ccws.
  629. *
  630. * Returns:
  631. * %0 on success and a negative error value on failure.
  632. */
  633. int cp_prefetch(struct channel_program *cp)
  634. {
  635. struct ccwchain *chain;
  636. int len, idx, ret;
  637. list_for_each_entry(chain, &cp->ccwchain_list, next) {
  638. len = chain->ch_len;
  639. for (idx = 0; idx < len; idx++) {
  640. ret = ccwchain_fetch_one(chain, idx, cp);
  641. if (ret)
  642. goto out_err;
  643. }
  644. }
  645. return 0;
  646. out_err:
  647. /* Only cleanup the chain elements that were actually translated. */
  648. chain->ch_len = idx;
  649. list_for_each_entry_continue(chain, &cp->ccwchain_list, next) {
  650. chain->ch_len = 0;
  651. }
  652. return ret;
  653. }
  654. /**
  655. * cp_get_orb() - get the orb of the channel program
  656. * @cp: channel_program on which to perform the operation
  657. * @intparm: new intparm for the returned orb
  658. * @lpm: candidate value of the logical-path mask for the returned orb
  659. *
  660. * This function returns the address of the updated orb of the channel
  661. * program. Channel I/O device drivers could use this orb to issue a
  662. * ssch.
  663. */
  664. union orb *cp_get_orb(struct channel_program *cp, u32 intparm, u8 lpm)
  665. {
  666. union orb *orb;
  667. struct ccwchain *chain;
  668. struct ccw1 *cpa;
  669. orb = &cp->orb;
  670. orb->cmd.intparm = intparm;
  671. orb->cmd.fmt = 1;
  672. orb->cmd.key = PAGE_DEFAULT_KEY >> 4;
  673. if (orb->cmd.lpm == 0)
  674. orb->cmd.lpm = lpm;
  675. chain = list_first_entry(&cp->ccwchain_list, struct ccwchain, next);
  676. cpa = chain->ch_ccw;
  677. orb->cmd.cpa = (__u32) __pa(cpa);
  678. return orb;
  679. }
  680. /**
  681. * cp_update_scsw() - update scsw for a channel program.
  682. * @cp: channel_program on which to perform the operation
  683. * @scsw: I/O results of the channel program and also the target to be
  684. * updated
  685. *
  686. * @scsw contains the I/O results of the channel program that pointed
  687. * to by @cp. However what @scsw->cpa stores is a host physical
  688. * address, which is meaningless for the guest, which is waiting for
  689. * the I/O results.
  690. *
  691. * This function updates @scsw->cpa to its coressponding guest physical
  692. * address.
  693. */
  694. void cp_update_scsw(struct channel_program *cp, union scsw *scsw)
  695. {
  696. struct ccwchain *chain;
  697. u32 cpa = scsw->cmd.cpa;
  698. u32 ccw_head, ccw_tail;
  699. /*
  700. * LATER:
  701. * For now, only update the cmd.cpa part. We may need to deal with
  702. * other portions of the schib as well, even if we don't return them
  703. * in the ioctl directly. Path status changes etc.
  704. */
  705. list_for_each_entry(chain, &cp->ccwchain_list, next) {
  706. ccw_head = (u32)(u64)chain->ch_ccw;
  707. ccw_tail = (u32)(u64)(chain->ch_ccw + chain->ch_len - 1);
  708. if ((ccw_head <= cpa) && (cpa <= ccw_tail)) {
  709. /*
  710. * (cpa - ccw_head) is the offset value of the host
  711. * physical ccw to its chain head.
  712. * Adding this value to the guest physical ccw chain
  713. * head gets us the guest cpa.
  714. */
  715. cpa = chain->ch_iova + (cpa - ccw_head);
  716. break;
  717. }
  718. }
  719. scsw->cmd.cpa = cpa;
  720. }
  721. /**
  722. * cp_iova_pinned() - check if an iova is pinned for a ccw chain.
  723. * @cp: channel_program on which to perform the operation
  724. * @iova: the iova to check
  725. *
  726. * If the @iova is currently pinned for the ccw chain, return true;
  727. * else return false.
  728. */
  729. bool cp_iova_pinned(struct channel_program *cp, u64 iova)
  730. {
  731. struct ccwchain *chain;
  732. int i;
  733. list_for_each_entry(chain, &cp->ccwchain_list, next) {
  734. for (i = 0; i < chain->ch_len; i++)
  735. if (pfn_array_table_iova_pinned(chain->ch_pat + i,
  736. iova))
  737. return true;
  738. }
  739. return false;
  740. }