oradax.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
  4. */
  5. /*
  6. * Oracle Data Analytics Accelerator (DAX)
  7. *
  8. * DAX is a coprocessor which resides on the SPARC M7 (DAX1) and M8
  9. * (DAX2) processor chips, and has direct access to the CPU's L3
  10. * caches as well as physical memory. It can perform several
  11. * operations on data streams with various input and output formats.
  12. * The driver provides a transport mechanism only and has limited
  13. * knowledge of the various opcodes and data formats. A user space
  14. * library provides high level services and translates these into low
  15. * level commands which are then passed into the driver and
  16. * subsequently the hypervisor and the coprocessor. The library is
  17. * the recommended way for applications to use the coprocessor, and
  18. * the driver interface is not intended for general use.
  19. *
  20. * See Documentation/sparc/oradax/oracle-dax.rst for more details.
  21. */
  22. #include <linux/uaccess.h>
  23. #include <linux/module.h>
  24. #include <linux/delay.h>
  25. #include <linux/cdev.h>
  26. #include <linux/slab.h>
  27. #include <linux/mm.h>
  28. #include <asm/hypervisor.h>
  29. #include <asm/mdesc.h>
  30. #include <asm/oradax.h>
  31. MODULE_LICENSE("GPL");
  32. MODULE_DESCRIPTION("Driver for Oracle Data Analytics Accelerator");
  33. #define DAX_DBG_FLG_BASIC 0x01
  34. #define DAX_DBG_FLG_STAT 0x02
  35. #define DAX_DBG_FLG_INFO 0x04
  36. #define DAX_DBG_FLG_ALL 0xff
  37. #define dax_err(fmt, ...) pr_err("%s: " fmt "\n", __func__, ##__VA_ARGS__)
  38. #define dax_info(fmt, ...) pr_info("%s: " fmt "\n", __func__, ##__VA_ARGS__)
  39. #define dax_dbg(fmt, ...) do { \
  40. if (dax_debug & DAX_DBG_FLG_BASIC)\
  41. dax_info(fmt, ##__VA_ARGS__); \
  42. } while (0)
  43. #define dax_stat_dbg(fmt, ...) do { \
  44. if (dax_debug & DAX_DBG_FLG_STAT) \
  45. dax_info(fmt, ##__VA_ARGS__); \
  46. } while (0)
  47. #define dax_info_dbg(fmt, ...) do { \
  48. if (dax_debug & DAX_DBG_FLG_INFO) \
  49. dax_info(fmt, ##__VA_ARGS__); \
  50. } while (0)
  51. #define DAX1_MINOR 1
  52. #define DAX1_MAJOR 1
  53. #define DAX2_MINOR 0
  54. #define DAX2_MAJOR 2
  55. #define DAX1_STR "ORCL,sun4v-dax"
  56. #define DAX2_STR "ORCL,sun4v-dax2"
  57. #define DAX_CA_ELEMS (DAX_MMAP_LEN / sizeof(struct dax_cca))
  58. #define DAX_CCB_USEC 100
  59. #define DAX_CCB_RETRIES 10000
  60. /* stream types */
  61. enum {
  62. OUT,
  63. PRI,
  64. SEC,
  65. TBL,
  66. NUM_STREAM_TYPES
  67. };
  68. /* completion status */
  69. #define CCA_STAT_NOT_COMPLETED 0
  70. #define CCA_STAT_COMPLETED 1
  71. #define CCA_STAT_FAILED 2
  72. #define CCA_STAT_KILLED 3
  73. #define CCA_STAT_NOT_RUN 4
  74. #define CCA_STAT_PIPE_OUT 5
  75. #define CCA_STAT_PIPE_SRC 6
  76. #define CCA_STAT_PIPE_DST 7
  77. /* completion err */
  78. #define CCA_ERR_SUCCESS 0x0 /* no error */
  79. #define CCA_ERR_OVERFLOW 0x1 /* buffer overflow */
  80. #define CCA_ERR_DECODE 0x2 /* CCB decode error */
  81. #define CCA_ERR_PAGE_OVERFLOW 0x3 /* page overflow */
  82. #define CCA_ERR_KILLED 0x7 /* command was killed */
  83. #define CCA_ERR_TIMEOUT 0x8 /* Timeout */
  84. #define CCA_ERR_ADI 0x9 /* ADI error */
  85. #define CCA_ERR_DATA_FMT 0xA /* data format error */
  86. #define CCA_ERR_OTHER_NO_RETRY 0xE /* Other error, do not retry */
  87. #define CCA_ERR_OTHER_RETRY 0xF /* Other error, retry */
  88. #define CCA_ERR_PARTIAL_SYMBOL 0x80 /* QP partial symbol warning */
  89. /* CCB address types */
  90. #define DAX_ADDR_TYPE_NONE 0
  91. #define DAX_ADDR_TYPE_VA_ALT 1 /* secondary context */
  92. #define DAX_ADDR_TYPE_RA 2 /* real address */
  93. #define DAX_ADDR_TYPE_VA 3 /* virtual address */
  94. /* dax_header_t opcode */
  95. #define DAX_OP_SYNC_NOP 0x0
  96. #define DAX_OP_EXTRACT 0x1
  97. #define DAX_OP_SCAN_VALUE 0x2
  98. #define DAX_OP_SCAN_RANGE 0x3
  99. #define DAX_OP_TRANSLATE 0x4
  100. #define DAX_OP_SELECT 0x5
  101. #define DAX_OP_INVERT 0x10 /* OR with translate, scan opcodes */
  102. struct dax_header {
  103. u32 ccb_version:4; /* 31:28 CCB Version */
  104. /* 27:24 Sync Flags */
  105. u32 pipe:1; /* Pipeline */
  106. u32 longccb:1; /* Longccb. Set for scan with lu2, lu3, lu4. */
  107. u32 cond:1; /* Conditional */
  108. u32 serial:1; /* Serial */
  109. u32 opcode:8; /* 23:16 Opcode */
  110. /* 15:0 Address Type. */
  111. u32 reserved:3; /* 15:13 reserved */
  112. u32 table_addr_type:2; /* 12:11 Huffman Table Address Type */
  113. u32 out_addr_type:3; /* 10:8 Destination Address Type */
  114. u32 sec_addr_type:3; /* 7:5 Secondary Source Address Type */
  115. u32 pri_addr_type:3; /* 4:2 Primary Source Address Type */
  116. u32 cca_addr_type:2; /* 1:0 Completion Address Type */
  117. };
  118. struct dax_control {
  119. u32 pri_fmt:4; /* 31:28 Primary Input Format */
  120. u32 pri_elem_size:5; /* 27:23 Primary Input Element Size(less1) */
  121. u32 pri_offset:3; /* 22:20 Primary Input Starting Offset */
  122. u32 sec_encoding:1; /* 19 Secondary Input Encoding */
  123. /* (must be 0 for Select) */
  124. u32 sec_offset:3; /* 18:16 Secondary Input Starting Offset */
  125. u32 sec_elem_size:2; /* 15:14 Secondary Input Element Size */
  126. /* (must be 0 for Select) */
  127. u32 out_fmt:2; /* 13:12 Output Format */
  128. u32 out_elem_size:2; /* 11:10 Output Element Size */
  129. u32 misc:10; /* 9:0 Opcode specific info */
  130. };
  131. struct dax_data_access {
  132. u64 flow_ctrl:2; /* 63:62 Flow Control Type */
  133. u64 pipe_target:2; /* 61:60 Pipeline Target */
  134. u64 out_buf_size:20; /* 59:40 Output Buffer Size */
  135. /* (cachelines less 1) */
  136. u64 unused1:8; /* 39:32 Reserved, Set to 0 */
  137. u64 out_alloc:5; /* 31:27 Output Allocation */
  138. u64 unused2:1; /* 26 Reserved */
  139. u64 pri_len_fmt:2; /* 25:24 Input Length Format */
  140. u64 pri_len:24; /* 23:0 Input Element/Byte/Bit Count */
  141. /* (less 1) */
  142. };
  143. struct dax_ccb {
  144. struct dax_header hdr; /* CCB Header */
  145. struct dax_control ctrl;/* Control Word */
  146. void *ca; /* Completion Address */
  147. void *pri; /* Primary Input Address */
  148. struct dax_data_access dac; /* Data Access Control */
  149. void *sec; /* Secondary Input Address */
  150. u64 dword5; /* depends on opcode */
  151. void *out; /* Output Address */
  152. void *tbl; /* Table Address or bitmap */
  153. };
  154. struct dax_cca {
  155. u8 status; /* user may mwait on this address */
  156. u8 err; /* user visible error notification */
  157. u8 rsvd[2]; /* reserved */
  158. u32 n_remaining; /* for QP partial symbol warning */
  159. u32 output_sz; /* output in bytes */
  160. u32 rsvd2; /* reserved */
  161. u64 run_cycles; /* run time in OCND2 cycles */
  162. u64 run_stats; /* nothing reported in version 1.0 */
  163. u32 n_processed; /* number input elements */
  164. u32 rsvd3[5]; /* reserved */
  165. u64 retval; /* command return value */
  166. u64 rsvd4[8]; /* reserved */
  167. };
  168. /* per thread CCB context */
  169. struct dax_ctx {
  170. struct dax_ccb *ccb_buf;
  171. u64 ccb_buf_ra; /* cached RA of ccb_buf */
  172. struct dax_cca *ca_buf;
  173. u64 ca_buf_ra; /* cached RA of ca_buf */
  174. struct page *pages[DAX_CA_ELEMS][NUM_STREAM_TYPES];
  175. /* array of locked pages */
  176. struct task_struct *owner; /* thread that owns ctx */
  177. struct task_struct *client; /* requesting thread */
  178. union ccb_result result;
  179. u32 ccb_count;
  180. u32 fail_count;
  181. };
  182. /* driver public entry points */
  183. static int dax_open(struct inode *inode, struct file *file);
  184. static ssize_t dax_read(struct file *filp, char __user *buf,
  185. size_t count, loff_t *ppos);
  186. static ssize_t dax_write(struct file *filp, const char __user *buf,
  187. size_t count, loff_t *ppos);
  188. static int dax_devmap(struct file *f, struct vm_area_struct *vma);
  189. static int dax_close(struct inode *i, struct file *f);
  190. static const struct file_operations dax_fops = {
  191. .owner = THIS_MODULE,
  192. .open = dax_open,
  193. .read = dax_read,
  194. .write = dax_write,
  195. .mmap = dax_devmap,
  196. .release = dax_close,
  197. };
  198. static int dax_ccb_exec(struct dax_ctx *ctx, const char __user *buf,
  199. size_t count, loff_t *ppos);
  200. static int dax_ccb_info(u64 ca, struct ccb_info_result *info);
  201. static int dax_ccb_kill(u64 ca, u16 *kill_res);
  202. static struct cdev c_dev;
  203. static struct class *cl;
  204. static dev_t first;
  205. static int max_ccb_version;
  206. static int dax_debug;
  207. module_param(dax_debug, int, 0644);
  208. MODULE_PARM_DESC(dax_debug, "Debug flags");
  209. static int __init dax_attach(void)
  210. {
  211. unsigned long dummy, hv_rv, major, minor, minor_requested, max_ccbs;
  212. struct mdesc_handle *hp = mdesc_grab();
  213. char *prop, *dax_name;
  214. bool found = false;
  215. int len, ret = 0;
  216. u64 pn;
  217. if (hp == NULL) {
  218. dax_err("Unable to grab mdesc");
  219. return -ENODEV;
  220. }
  221. mdesc_for_each_node_by_name(hp, pn, "virtual-device") {
  222. prop = (char *)mdesc_get_property(hp, pn, "name", &len);
  223. if (prop == NULL)
  224. continue;
  225. if (strncmp(prop, "dax", strlen("dax")))
  226. continue;
  227. dax_dbg("Found node 0x%llx = %s", pn, prop);
  228. prop = (char *)mdesc_get_property(hp, pn, "compatible", &len);
  229. if (prop == NULL)
  230. continue;
  231. dax_dbg("Found node 0x%llx = %s", pn, prop);
  232. found = true;
  233. break;
  234. }
  235. if (!found) {
  236. dax_err("No DAX device found");
  237. ret = -ENODEV;
  238. goto done;
  239. }
  240. if (strncmp(prop, DAX2_STR, strlen(DAX2_STR)) == 0) {
  241. dax_name = DAX_NAME "2";
  242. major = DAX2_MAJOR;
  243. minor_requested = DAX2_MINOR;
  244. max_ccb_version = 1;
  245. dax_dbg("MD indicates DAX2 coprocessor");
  246. } else if (strncmp(prop, DAX1_STR, strlen(DAX1_STR)) == 0) {
  247. dax_name = DAX_NAME "1";
  248. major = DAX1_MAJOR;
  249. minor_requested = DAX1_MINOR;
  250. max_ccb_version = 0;
  251. dax_dbg("MD indicates DAX1 coprocessor");
  252. } else {
  253. dax_err("Unknown dax type: %s", prop);
  254. ret = -ENODEV;
  255. goto done;
  256. }
  257. minor = minor_requested;
  258. dax_dbg("Registering DAX HV api with major %ld minor %ld", major,
  259. minor);
  260. if (sun4v_hvapi_register(HV_GRP_DAX, major, &minor)) {
  261. dax_err("hvapi_register failed");
  262. ret = -ENODEV;
  263. goto done;
  264. } else {
  265. dax_dbg("Max minor supported by HV = %ld (major %ld)", minor,
  266. major);
  267. minor = min(minor, minor_requested);
  268. dax_dbg("registered DAX major %ld minor %ld", major, minor);
  269. }
  270. /* submit a zero length ccb array to query coprocessor queue size */
  271. hv_rv = sun4v_ccb_submit(0, 0, HV_CCB_QUERY_CMD, 0, &max_ccbs, &dummy);
  272. if (hv_rv != 0) {
  273. dax_err("get_hwqueue_size failed with status=%ld and max_ccbs=%ld",
  274. hv_rv, max_ccbs);
  275. ret = -ENODEV;
  276. goto done;
  277. }
  278. if (max_ccbs != DAX_MAX_CCBS) {
  279. dax_err("HV reports unsupported max_ccbs=%ld", max_ccbs);
  280. ret = -ENODEV;
  281. goto done;
  282. }
  283. if (alloc_chrdev_region(&first, 0, 1, DAX_NAME) < 0) {
  284. dax_err("alloc_chrdev_region failed");
  285. ret = -ENXIO;
  286. goto done;
  287. }
  288. cl = class_create(THIS_MODULE, DAX_NAME);
  289. if (IS_ERR(cl)) {
  290. dax_err("class_create failed");
  291. ret = PTR_ERR(cl);
  292. goto class_error;
  293. }
  294. if (device_create(cl, NULL, first, NULL, dax_name) == NULL) {
  295. dax_err("device_create failed");
  296. ret = -ENXIO;
  297. goto device_error;
  298. }
  299. cdev_init(&c_dev, &dax_fops);
  300. if (cdev_add(&c_dev, first, 1) == -1) {
  301. dax_err("cdev_add failed");
  302. ret = -ENXIO;
  303. goto cdev_error;
  304. }
  305. pr_info("Attached DAX module\n");
  306. goto done;
  307. cdev_error:
  308. device_destroy(cl, first);
  309. device_error:
  310. class_destroy(cl);
  311. class_error:
  312. unregister_chrdev_region(first, 1);
  313. done:
  314. mdesc_release(hp);
  315. return ret;
  316. }
  317. module_init(dax_attach);
  318. static void __exit dax_detach(void)
  319. {
  320. pr_info("Cleaning up DAX module\n");
  321. cdev_del(&c_dev);
  322. device_destroy(cl, first);
  323. class_destroy(cl);
  324. unregister_chrdev_region(first, 1);
  325. }
  326. module_exit(dax_detach);
  327. /* map completion area */
  328. static int dax_devmap(struct file *f, struct vm_area_struct *vma)
  329. {
  330. struct dax_ctx *ctx = (struct dax_ctx *)f->private_data;
  331. size_t len = vma->vm_end - vma->vm_start;
  332. dax_dbg("len=0x%lx, flags=0x%lx", len, vma->vm_flags);
  333. if (ctx->owner != current) {
  334. dax_dbg("devmap called from wrong thread");
  335. return -EINVAL;
  336. }
  337. if (len != DAX_MMAP_LEN) {
  338. dax_dbg("len(%lu) != DAX_MMAP_LEN(%d)", len, DAX_MMAP_LEN);
  339. return -EINVAL;
  340. }
  341. /* completion area is mapped read-only for user */
  342. if (vma->vm_flags & VM_WRITE)
  343. return -EPERM;
  344. vm_flags_clear(vma, VM_MAYWRITE);
  345. if (remap_pfn_range(vma, vma->vm_start, ctx->ca_buf_ra >> PAGE_SHIFT,
  346. len, vma->vm_page_prot))
  347. return -EAGAIN;
  348. dax_dbg("mmapped completion area at uva 0x%lx", vma->vm_start);
  349. return 0;
  350. }
  351. /* Unlock user pages. Called during dequeue or device close */
  352. static void dax_unlock_pages(struct dax_ctx *ctx, int ccb_index, int nelem)
  353. {
  354. int i, j;
  355. for (i = ccb_index; i < ccb_index + nelem; i++) {
  356. for (j = 0; j < NUM_STREAM_TYPES; j++) {
  357. struct page *p = ctx->pages[i][j];
  358. if (p) {
  359. dax_dbg("freeing page %p", p);
  360. unpin_user_pages_dirty_lock(&p, 1, j == OUT);
  361. ctx->pages[i][j] = NULL;
  362. }
  363. }
  364. }
  365. }
  366. static int dax_lock_page(void *va, struct page **p)
  367. {
  368. int ret;
  369. dax_dbg("uva %p", va);
  370. ret = pin_user_pages_fast((unsigned long)va, 1, FOLL_WRITE, p);
  371. if (ret == 1) {
  372. dax_dbg("locked page %p, for VA %p", *p, va);
  373. return 0;
  374. }
  375. dax_dbg("pin_user_pages failed, va=%p, ret=%d", va, ret);
  376. return -1;
  377. }
  378. static int dax_lock_pages(struct dax_ctx *ctx, int idx,
  379. int nelem, u64 *err_va)
  380. {
  381. int i;
  382. for (i = 0; i < nelem; i++) {
  383. struct dax_ccb *ccbp = &ctx->ccb_buf[i];
  384. /*
  385. * For each address in the CCB whose type is virtual,
  386. * lock the page and change the type to virtual alternate
  387. * context. On error, return the offending address in
  388. * err_va.
  389. */
  390. if (ccbp->hdr.out_addr_type == DAX_ADDR_TYPE_VA) {
  391. dax_dbg("output");
  392. if (dax_lock_page(ccbp->out,
  393. &ctx->pages[i + idx][OUT]) != 0) {
  394. *err_va = (u64)ccbp->out;
  395. goto error;
  396. }
  397. ccbp->hdr.out_addr_type = DAX_ADDR_TYPE_VA_ALT;
  398. }
  399. if (ccbp->hdr.pri_addr_type == DAX_ADDR_TYPE_VA) {
  400. dax_dbg("input");
  401. if (dax_lock_page(ccbp->pri,
  402. &ctx->pages[i + idx][PRI]) != 0) {
  403. *err_va = (u64)ccbp->pri;
  404. goto error;
  405. }
  406. ccbp->hdr.pri_addr_type = DAX_ADDR_TYPE_VA_ALT;
  407. }
  408. if (ccbp->hdr.sec_addr_type == DAX_ADDR_TYPE_VA) {
  409. dax_dbg("sec input");
  410. if (dax_lock_page(ccbp->sec,
  411. &ctx->pages[i + idx][SEC]) != 0) {
  412. *err_va = (u64)ccbp->sec;
  413. goto error;
  414. }
  415. ccbp->hdr.sec_addr_type = DAX_ADDR_TYPE_VA_ALT;
  416. }
  417. if (ccbp->hdr.table_addr_type == DAX_ADDR_TYPE_VA) {
  418. dax_dbg("tbl");
  419. if (dax_lock_page(ccbp->tbl,
  420. &ctx->pages[i + idx][TBL]) != 0) {
  421. *err_va = (u64)ccbp->tbl;
  422. goto error;
  423. }
  424. ccbp->hdr.table_addr_type = DAX_ADDR_TYPE_VA_ALT;
  425. }
  426. /* skip over 2nd 64 bytes of long CCB */
  427. if (ccbp->hdr.longccb)
  428. i++;
  429. }
  430. return DAX_SUBMIT_OK;
  431. error:
  432. dax_unlock_pages(ctx, idx, nelem);
  433. return DAX_SUBMIT_ERR_NOACCESS;
  434. }
  435. static void dax_ccb_wait(struct dax_ctx *ctx, int idx)
  436. {
  437. int ret, nretries;
  438. u16 kill_res;
  439. dax_dbg("idx=%d", idx);
  440. for (nretries = 0; nretries < DAX_CCB_RETRIES; nretries++) {
  441. if (ctx->ca_buf[idx].status == CCA_STAT_NOT_COMPLETED)
  442. udelay(DAX_CCB_USEC);
  443. else
  444. return;
  445. }
  446. dax_dbg("ctx (%p): CCB[%d] timed out, wait usec=%d, retries=%d. Killing ccb",
  447. (void *)ctx, idx, DAX_CCB_USEC, DAX_CCB_RETRIES);
  448. ret = dax_ccb_kill(ctx->ca_buf_ra + idx * sizeof(struct dax_cca),
  449. &kill_res);
  450. dax_dbg("Kill CCB[%d] %s", idx, ret ? "failed" : "succeeded");
  451. }
  452. static int dax_close(struct inode *ino, struct file *f)
  453. {
  454. struct dax_ctx *ctx = (struct dax_ctx *)f->private_data;
  455. int i;
  456. f->private_data = NULL;
  457. for (i = 0; i < DAX_CA_ELEMS; i++) {
  458. if (ctx->ca_buf[i].status == CCA_STAT_NOT_COMPLETED) {
  459. dax_dbg("CCB[%d] not completed", i);
  460. dax_ccb_wait(ctx, i);
  461. }
  462. dax_unlock_pages(ctx, i, 1);
  463. }
  464. kfree(ctx->ccb_buf);
  465. kfree(ctx->ca_buf);
  466. dax_stat_dbg("CCBs: %d good, %d bad", ctx->ccb_count, ctx->fail_count);
  467. kfree(ctx);
  468. return 0;
  469. }
  470. static ssize_t dax_read(struct file *f, char __user *buf,
  471. size_t count, loff_t *ppos)
  472. {
  473. struct dax_ctx *ctx = f->private_data;
  474. if (ctx->client != current)
  475. return -EUSERS;
  476. ctx->client = NULL;
  477. if (count != sizeof(union ccb_result))
  478. return -EINVAL;
  479. if (copy_to_user(buf, &ctx->result, sizeof(union ccb_result)))
  480. return -EFAULT;
  481. return count;
  482. }
  483. static ssize_t dax_write(struct file *f, const char __user *buf,
  484. size_t count, loff_t *ppos)
  485. {
  486. struct dax_ctx *ctx = f->private_data;
  487. struct dax_command hdr;
  488. unsigned long ca;
  489. int i, idx, ret;
  490. if (ctx->client != NULL)
  491. return -EINVAL;
  492. if (count == 0 || count > DAX_MAX_CCBS * sizeof(struct dax_ccb))
  493. return -EINVAL;
  494. if (count % sizeof(struct dax_ccb) == 0)
  495. return dax_ccb_exec(ctx, buf, count, ppos); /* CCB EXEC */
  496. if (count != sizeof(struct dax_command))
  497. return -EINVAL;
  498. /* immediate command */
  499. if (ctx->owner != current)
  500. return -EUSERS;
  501. if (copy_from_user(&hdr, buf, sizeof(hdr)))
  502. return -EFAULT;
  503. ca = ctx->ca_buf_ra + hdr.ca_offset;
  504. switch (hdr.command) {
  505. case CCB_KILL:
  506. if (hdr.ca_offset >= DAX_MMAP_LEN) {
  507. dax_dbg("invalid ca_offset (%d) >= ca_buflen (%d)",
  508. hdr.ca_offset, DAX_MMAP_LEN);
  509. return -EINVAL;
  510. }
  511. ret = dax_ccb_kill(ca, &ctx->result.kill.action);
  512. if (ret != 0) {
  513. dax_dbg("dax_ccb_kill failed (ret=%d)", ret);
  514. return ret;
  515. }
  516. dax_info_dbg("killed (ca_offset %d)", hdr.ca_offset);
  517. idx = hdr.ca_offset / sizeof(struct dax_cca);
  518. ctx->ca_buf[idx].status = CCA_STAT_KILLED;
  519. ctx->ca_buf[idx].err = CCA_ERR_KILLED;
  520. ctx->client = current;
  521. return count;
  522. case CCB_INFO:
  523. if (hdr.ca_offset >= DAX_MMAP_LEN) {
  524. dax_dbg("invalid ca_offset (%d) >= ca_buflen (%d)",
  525. hdr.ca_offset, DAX_MMAP_LEN);
  526. return -EINVAL;
  527. }
  528. ret = dax_ccb_info(ca, &ctx->result.info);
  529. if (ret != 0) {
  530. dax_dbg("dax_ccb_info failed (ret=%d)", ret);
  531. return ret;
  532. }
  533. dax_info_dbg("info succeeded on ca_offset %d", hdr.ca_offset);
  534. ctx->client = current;
  535. return count;
  536. case CCB_DEQUEUE:
  537. for (i = 0; i < DAX_CA_ELEMS; i++) {
  538. if (ctx->ca_buf[i].status !=
  539. CCA_STAT_NOT_COMPLETED)
  540. dax_unlock_pages(ctx, i, 1);
  541. }
  542. return count;
  543. default:
  544. return -EINVAL;
  545. }
  546. }
  547. static int dax_open(struct inode *inode, struct file *f)
  548. {
  549. struct dax_ctx *ctx = NULL;
  550. int i;
  551. ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
  552. if (ctx == NULL)
  553. goto done;
  554. ctx->ccb_buf = kcalloc(DAX_MAX_CCBS, sizeof(struct dax_ccb),
  555. GFP_KERNEL);
  556. if (ctx->ccb_buf == NULL)
  557. goto done;
  558. ctx->ccb_buf_ra = virt_to_phys(ctx->ccb_buf);
  559. dax_dbg("ctx->ccb_buf=0x%p, ccb_buf_ra=0x%llx",
  560. (void *)ctx->ccb_buf, ctx->ccb_buf_ra);
  561. /* allocate CCB completion area buffer */
  562. ctx->ca_buf = kzalloc(DAX_MMAP_LEN, GFP_KERNEL);
  563. if (ctx->ca_buf == NULL)
  564. goto alloc_error;
  565. for (i = 0; i < DAX_CA_ELEMS; i++)
  566. ctx->ca_buf[i].status = CCA_STAT_COMPLETED;
  567. ctx->ca_buf_ra = virt_to_phys(ctx->ca_buf);
  568. dax_dbg("ctx=0x%p, ctx->ca_buf=0x%p, ca_buf_ra=0x%llx",
  569. (void *)ctx, (void *)ctx->ca_buf, ctx->ca_buf_ra);
  570. ctx->owner = current;
  571. f->private_data = ctx;
  572. return 0;
  573. alloc_error:
  574. kfree(ctx->ccb_buf);
  575. done:
  576. kfree(ctx);
  577. return -ENOMEM;
  578. }
  579. static char *dax_hv_errno(unsigned long hv_ret, int *ret)
  580. {
  581. switch (hv_ret) {
  582. case HV_EBADALIGN:
  583. *ret = -EFAULT;
  584. return "HV_EBADALIGN";
  585. case HV_ENORADDR:
  586. *ret = -EFAULT;
  587. return "HV_ENORADDR";
  588. case HV_EINVAL:
  589. *ret = -EINVAL;
  590. return "HV_EINVAL";
  591. case HV_EWOULDBLOCK:
  592. *ret = -EAGAIN;
  593. return "HV_EWOULDBLOCK";
  594. case HV_ENOACCESS:
  595. *ret = -EPERM;
  596. return "HV_ENOACCESS";
  597. default:
  598. break;
  599. }
  600. *ret = -EIO;
  601. return "UNKNOWN";
  602. }
  603. static int dax_ccb_kill(u64 ca, u16 *kill_res)
  604. {
  605. unsigned long hv_ret;
  606. int count, ret = 0;
  607. char *err_str;
  608. for (count = 0; count < DAX_CCB_RETRIES; count++) {
  609. dax_dbg("attempting kill on ca_ra 0x%llx", ca);
  610. hv_ret = sun4v_ccb_kill(ca, kill_res);
  611. if (hv_ret == HV_EOK) {
  612. dax_info_dbg("HV_EOK (ca_ra 0x%llx): %d", ca,
  613. *kill_res);
  614. } else {
  615. err_str = dax_hv_errno(hv_ret, &ret);
  616. dax_dbg("%s (ca_ra 0x%llx)", err_str, ca);
  617. }
  618. if (ret != -EAGAIN)
  619. return ret;
  620. dax_info_dbg("ccb_kill count = %d", count);
  621. udelay(DAX_CCB_USEC);
  622. }
  623. return -EAGAIN;
  624. }
  625. static int dax_ccb_info(u64 ca, struct ccb_info_result *info)
  626. {
  627. unsigned long hv_ret;
  628. char *err_str;
  629. int ret = 0;
  630. dax_dbg("attempting info on ca_ra 0x%llx", ca);
  631. hv_ret = sun4v_ccb_info(ca, info);
  632. if (hv_ret == HV_EOK) {
  633. dax_info_dbg("HV_EOK (ca_ra 0x%llx): %d", ca, info->state);
  634. if (info->state == DAX_CCB_ENQUEUED) {
  635. dax_info_dbg("dax_unit %d, queue_num %d, queue_pos %d",
  636. info->inst_num, info->q_num, info->q_pos);
  637. }
  638. } else {
  639. err_str = dax_hv_errno(hv_ret, &ret);
  640. dax_dbg("%s (ca_ra 0x%llx)", err_str, ca);
  641. }
  642. return ret;
  643. }
  644. static void dax_prt_ccbs(struct dax_ccb *ccb, int nelem)
  645. {
  646. int i, j;
  647. u64 *ccbp;
  648. dax_dbg("ccb buffer:");
  649. for (i = 0; i < nelem; i++) {
  650. ccbp = (u64 *)&ccb[i];
  651. dax_dbg(" %sccb[%d]", ccb[i].hdr.longccb ? "long " : "", i);
  652. for (j = 0; j < 8; j++)
  653. dax_dbg("\tccb[%d].dwords[%d]=0x%llx",
  654. i, j, *(ccbp + j));
  655. }
  656. }
  657. /*
  658. * Validates user CCB content. Also sets completion address and address types
  659. * for all addresses contained in CCB.
  660. */
  661. static int dax_preprocess_usr_ccbs(struct dax_ctx *ctx, int idx, int nelem)
  662. {
  663. int i;
  664. /*
  665. * The user is not allowed to specify real address types in
  666. * the CCB header. This must be enforced by the kernel before
  667. * submitting the CCBs to HV. The only allowed values for all
  668. * address fields are VA or IMM
  669. */
  670. for (i = 0; i < nelem; i++) {
  671. struct dax_ccb *ccbp = &ctx->ccb_buf[i];
  672. unsigned long ca_offset;
  673. if (ccbp->hdr.ccb_version > max_ccb_version)
  674. return DAX_SUBMIT_ERR_CCB_INVAL;
  675. switch (ccbp->hdr.opcode) {
  676. case DAX_OP_SYNC_NOP:
  677. case DAX_OP_EXTRACT:
  678. case DAX_OP_SCAN_VALUE:
  679. case DAX_OP_SCAN_RANGE:
  680. case DAX_OP_TRANSLATE:
  681. case DAX_OP_SCAN_VALUE | DAX_OP_INVERT:
  682. case DAX_OP_SCAN_RANGE | DAX_OP_INVERT:
  683. case DAX_OP_TRANSLATE | DAX_OP_INVERT:
  684. case DAX_OP_SELECT:
  685. break;
  686. default:
  687. return DAX_SUBMIT_ERR_CCB_INVAL;
  688. }
  689. if (ccbp->hdr.out_addr_type != DAX_ADDR_TYPE_VA &&
  690. ccbp->hdr.out_addr_type != DAX_ADDR_TYPE_NONE) {
  691. dax_dbg("invalid out_addr_type in user CCB[%d]", i);
  692. return DAX_SUBMIT_ERR_CCB_INVAL;
  693. }
  694. if (ccbp->hdr.pri_addr_type != DAX_ADDR_TYPE_VA &&
  695. ccbp->hdr.pri_addr_type != DAX_ADDR_TYPE_NONE) {
  696. dax_dbg("invalid pri_addr_type in user CCB[%d]", i);
  697. return DAX_SUBMIT_ERR_CCB_INVAL;
  698. }
  699. if (ccbp->hdr.sec_addr_type != DAX_ADDR_TYPE_VA &&
  700. ccbp->hdr.sec_addr_type != DAX_ADDR_TYPE_NONE) {
  701. dax_dbg("invalid sec_addr_type in user CCB[%d]", i);
  702. return DAX_SUBMIT_ERR_CCB_INVAL;
  703. }
  704. if (ccbp->hdr.table_addr_type != DAX_ADDR_TYPE_VA &&
  705. ccbp->hdr.table_addr_type != DAX_ADDR_TYPE_NONE) {
  706. dax_dbg("invalid table_addr_type in user CCB[%d]", i);
  707. return DAX_SUBMIT_ERR_CCB_INVAL;
  708. }
  709. /* set completion (real) address and address type */
  710. ccbp->hdr.cca_addr_type = DAX_ADDR_TYPE_RA;
  711. ca_offset = (idx + i) * sizeof(struct dax_cca);
  712. ccbp->ca = (void *)ctx->ca_buf_ra + ca_offset;
  713. memset(&ctx->ca_buf[idx + i], 0, sizeof(struct dax_cca));
  714. dax_dbg("ccb[%d]=%p, ca_offset=0x%lx, compl RA=0x%llx",
  715. i, ccbp, ca_offset, ctx->ca_buf_ra + ca_offset);
  716. /* skip over 2nd 64 bytes of long CCB */
  717. if (ccbp->hdr.longccb)
  718. i++;
  719. }
  720. return DAX_SUBMIT_OK;
  721. }
  722. static int dax_ccb_exec(struct dax_ctx *ctx, const char __user *buf,
  723. size_t count, loff_t *ppos)
  724. {
  725. unsigned long accepted_len, hv_rv;
  726. int i, idx, nccbs, naccepted;
  727. ctx->client = current;
  728. idx = *ppos;
  729. nccbs = count / sizeof(struct dax_ccb);
  730. if (ctx->owner != current) {
  731. dax_dbg("wrong thread");
  732. ctx->result.exec.status = DAX_SUBMIT_ERR_THR_INIT;
  733. return 0;
  734. }
  735. dax_dbg("args: ccb_buf_len=%ld, idx=%d", count, idx);
  736. /* for given index and length, verify ca_buf range exists */
  737. if (idx < 0 || idx > (DAX_CA_ELEMS - nccbs)) {
  738. ctx->result.exec.status = DAX_SUBMIT_ERR_NO_CA_AVAIL;
  739. return 0;
  740. }
  741. /*
  742. * Copy CCBs into kernel buffer to prevent modification by the
  743. * user in between validation and submission.
  744. */
  745. if (copy_from_user(ctx->ccb_buf, buf, count)) {
  746. dax_dbg("copyin of user CCB buffer failed");
  747. ctx->result.exec.status = DAX_SUBMIT_ERR_CCB_ARR_MMU_MISS;
  748. return 0;
  749. }
  750. /* check to see if ca_buf[idx] .. ca_buf[idx + nccbs] are available */
  751. for (i = idx; i < idx + nccbs; i++) {
  752. if (ctx->ca_buf[i].status == CCA_STAT_NOT_COMPLETED) {
  753. dax_dbg("CA range not available, dequeue needed");
  754. ctx->result.exec.status = DAX_SUBMIT_ERR_NO_CA_AVAIL;
  755. return 0;
  756. }
  757. }
  758. dax_unlock_pages(ctx, idx, nccbs);
  759. ctx->result.exec.status = dax_preprocess_usr_ccbs(ctx, idx, nccbs);
  760. if (ctx->result.exec.status != DAX_SUBMIT_OK)
  761. return 0;
  762. ctx->result.exec.status = dax_lock_pages(ctx, idx, nccbs,
  763. &ctx->result.exec.status_data);
  764. if (ctx->result.exec.status != DAX_SUBMIT_OK)
  765. return 0;
  766. if (dax_debug & DAX_DBG_FLG_BASIC)
  767. dax_prt_ccbs(ctx->ccb_buf, nccbs);
  768. hv_rv = sun4v_ccb_submit(ctx->ccb_buf_ra, count,
  769. HV_CCB_QUERY_CMD | HV_CCB_VA_SECONDARY, 0,
  770. &accepted_len, &ctx->result.exec.status_data);
  771. switch (hv_rv) {
  772. case HV_EOK:
  773. /*
  774. * Hcall succeeded with no errors but the accepted
  775. * length may be less than the requested length. The
  776. * only way the driver can resubmit the remainder is
  777. * to wait for completion of the submitted CCBs since
  778. * there is no way to guarantee the ordering semantics
  779. * required by the client applications. Therefore we
  780. * let the user library deal with resubmissions.
  781. */
  782. ctx->result.exec.status = DAX_SUBMIT_OK;
  783. break;
  784. case HV_EWOULDBLOCK:
  785. /*
  786. * This is a transient HV API error. The user library
  787. * can retry.
  788. */
  789. dax_dbg("hcall returned HV_EWOULDBLOCK");
  790. ctx->result.exec.status = DAX_SUBMIT_ERR_WOULDBLOCK;
  791. break;
  792. case HV_ENOMAP:
  793. /*
  794. * HV was unable to translate a VA. The VA it could
  795. * not translate is returned in the status_data param.
  796. */
  797. dax_dbg("hcall returned HV_ENOMAP");
  798. ctx->result.exec.status = DAX_SUBMIT_ERR_NOMAP;
  799. break;
  800. case HV_EINVAL:
  801. /*
  802. * This is the result of an invalid user CCB as HV is
  803. * validating some of the user CCB fields. Pass this
  804. * error back to the user. There is no supporting info
  805. * to isolate the invalid field.
  806. */
  807. dax_dbg("hcall returned HV_EINVAL");
  808. ctx->result.exec.status = DAX_SUBMIT_ERR_CCB_INVAL;
  809. break;
  810. case HV_ENOACCESS:
  811. /*
  812. * HV found a VA that did not have the appropriate
  813. * permissions (such as the w bit). The VA in question
  814. * is returned in status_data param.
  815. */
  816. dax_dbg("hcall returned HV_ENOACCESS");
  817. ctx->result.exec.status = DAX_SUBMIT_ERR_NOACCESS;
  818. break;
  819. case HV_EUNAVAILABLE:
  820. /*
  821. * The requested CCB operation could not be performed
  822. * at this time. Return the specific unavailable code
  823. * in the status_data field.
  824. */
  825. dax_dbg("hcall returned HV_EUNAVAILABLE");
  826. ctx->result.exec.status = DAX_SUBMIT_ERR_UNAVAIL;
  827. break;
  828. default:
  829. ctx->result.exec.status = DAX_SUBMIT_ERR_INTERNAL;
  830. dax_dbg("unknown hcall return value (%ld)", hv_rv);
  831. break;
  832. }
  833. /* unlock pages associated with the unaccepted CCBs */
  834. naccepted = accepted_len / sizeof(struct dax_ccb);
  835. dax_unlock_pages(ctx, idx + naccepted, nccbs - naccepted);
  836. /* mark unaccepted CCBs as not completed */
  837. for (i = idx + naccepted; i < idx + nccbs; i++)
  838. ctx->ca_buf[i].status = CCA_STAT_COMPLETED;
  839. ctx->ccb_count += naccepted;
  840. ctx->fail_count += nccbs - naccepted;
  841. dax_dbg("hcall rv=%ld, accepted_len=%ld, status_data=0x%llx, ret status=%d",
  842. hv_rv, accepted_len, ctx->result.exec.status_data,
  843. ctx->result.exec.status);
  844. if (count == accepted_len)
  845. ctx->client = NULL; /* no read needed to complete protocol */
  846. return accepted_len;
  847. }