vfio_ccw_cp.c 20 KB

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