card_utils.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062
  1. /**
  2. * IBM Accelerator Family 'GenWQE'
  3. *
  4. * (C) Copyright IBM Corp. 2013
  5. *
  6. * Author: Frank Haverkamp <haver@linux.vnet.ibm.com>
  7. * Author: Joerg-Stephan Vogt <jsvogt@de.ibm.com>
  8. * Author: Michael Jung <mijung@gmx.net>
  9. * Author: Michael Ruettger <michael@ibmra.de>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License (version 2 only)
  13. * as published by the Free Software Foundation.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. */
  20. /*
  21. * Miscelanous functionality used in the other GenWQE driver parts.
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/dma-mapping.h>
  25. #include <linux/sched.h>
  26. #include <linux/vmalloc.h>
  27. #include <linux/page-flags.h>
  28. #include <linux/scatterlist.h>
  29. #include <linux/hugetlb.h>
  30. #include <linux/iommu.h>
  31. #include <linux/delay.h>
  32. #include <linux/pci.h>
  33. #include <linux/dma-mapping.h>
  34. #include <linux/ctype.h>
  35. #include <linux/module.h>
  36. #include <linux/platform_device.h>
  37. #include <linux/delay.h>
  38. #include <asm/pgtable.h>
  39. #include "genwqe_driver.h"
  40. #include "card_base.h"
  41. #include "card_ddcb.h"
  42. /**
  43. * __genwqe_writeq() - Write 64-bit register
  44. * @cd: genwqe device descriptor
  45. * @byte_offs: byte offset within BAR
  46. * @val: 64-bit value
  47. *
  48. * Return: 0 if success; < 0 if error
  49. */
  50. int __genwqe_writeq(struct genwqe_dev *cd, u64 byte_offs, u64 val)
  51. {
  52. struct pci_dev *pci_dev = cd->pci_dev;
  53. if (cd->err_inject & GENWQE_INJECT_HARDWARE_FAILURE)
  54. return -EIO;
  55. if (cd->mmio == NULL)
  56. return -EIO;
  57. if (pci_channel_offline(pci_dev))
  58. return -EIO;
  59. __raw_writeq((__force u64)cpu_to_be64(val), cd->mmio + byte_offs);
  60. return 0;
  61. }
  62. /**
  63. * __genwqe_readq() - Read 64-bit register
  64. * @cd: genwqe device descriptor
  65. * @byte_offs: offset within BAR
  66. *
  67. * Return: value from register
  68. */
  69. u64 __genwqe_readq(struct genwqe_dev *cd, u64 byte_offs)
  70. {
  71. if (cd->err_inject & GENWQE_INJECT_HARDWARE_FAILURE)
  72. return 0xffffffffffffffffull;
  73. if ((cd->err_inject & GENWQE_INJECT_GFIR_FATAL) &&
  74. (byte_offs == IO_SLC_CFGREG_GFIR))
  75. return 0x000000000000ffffull;
  76. if ((cd->err_inject & GENWQE_INJECT_GFIR_INFO) &&
  77. (byte_offs == IO_SLC_CFGREG_GFIR))
  78. return 0x00000000ffff0000ull;
  79. if (cd->mmio == NULL)
  80. return 0xffffffffffffffffull;
  81. return be64_to_cpu((__force __be64)__raw_readq(cd->mmio + byte_offs));
  82. }
  83. /**
  84. * __genwqe_writel() - Write 32-bit register
  85. * @cd: genwqe device descriptor
  86. * @byte_offs: byte offset within BAR
  87. * @val: 32-bit value
  88. *
  89. * Return: 0 if success; < 0 if error
  90. */
  91. int __genwqe_writel(struct genwqe_dev *cd, u64 byte_offs, u32 val)
  92. {
  93. struct pci_dev *pci_dev = cd->pci_dev;
  94. if (cd->err_inject & GENWQE_INJECT_HARDWARE_FAILURE)
  95. return -EIO;
  96. if (cd->mmio == NULL)
  97. return -EIO;
  98. if (pci_channel_offline(pci_dev))
  99. return -EIO;
  100. __raw_writel((__force u32)cpu_to_be32(val), cd->mmio + byte_offs);
  101. return 0;
  102. }
  103. /**
  104. * __genwqe_readl() - Read 32-bit register
  105. * @cd: genwqe device descriptor
  106. * @byte_offs: offset within BAR
  107. *
  108. * Return: Value from register
  109. */
  110. u32 __genwqe_readl(struct genwqe_dev *cd, u64 byte_offs)
  111. {
  112. if (cd->err_inject & GENWQE_INJECT_HARDWARE_FAILURE)
  113. return 0xffffffff;
  114. if (cd->mmio == NULL)
  115. return 0xffffffff;
  116. return be32_to_cpu((__force __be32)__raw_readl(cd->mmio + byte_offs));
  117. }
  118. /**
  119. * genwqe_read_app_id() - Extract app_id
  120. *
  121. * app_unitcfg need to be filled with valid data first
  122. */
  123. int genwqe_read_app_id(struct genwqe_dev *cd, char *app_name, int len)
  124. {
  125. int i, j;
  126. u32 app_id = (u32)cd->app_unitcfg;
  127. memset(app_name, 0, len);
  128. for (i = 0, j = 0; j < min(len, 4); j++) {
  129. char ch = (char)((app_id >> (24 - j*8)) & 0xff);
  130. if (ch == ' ')
  131. continue;
  132. app_name[i++] = isprint(ch) ? ch : 'X';
  133. }
  134. return i;
  135. }
  136. /**
  137. * genwqe_init_crc32() - Prepare a lookup table for fast crc32 calculations
  138. *
  139. * Existing kernel functions seem to use a different polynom,
  140. * therefore we could not use them here.
  141. *
  142. * Genwqe's Polynomial = 0x20044009
  143. */
  144. #define CRC32_POLYNOMIAL 0x20044009
  145. static u32 crc32_tab[256]; /* crc32 lookup table */
  146. void genwqe_init_crc32(void)
  147. {
  148. int i, j;
  149. u32 crc;
  150. for (i = 0; i < 256; i++) {
  151. crc = i << 24;
  152. for (j = 0; j < 8; j++) {
  153. if (crc & 0x80000000)
  154. crc = (crc << 1) ^ CRC32_POLYNOMIAL;
  155. else
  156. crc = (crc << 1);
  157. }
  158. crc32_tab[i] = crc;
  159. }
  160. }
  161. /**
  162. * genwqe_crc32() - Generate 32-bit crc as required for DDCBs
  163. * @buff: pointer to data buffer
  164. * @len: length of data for calculation
  165. * @init: initial crc (0xffffffff at start)
  166. *
  167. * polynomial = x^32 * + x^29 + x^18 + x^14 + x^3 + 1 (0x20044009)
  168. * Example: 4 bytes 0x01 0x02 0x03 0x04 with init=0xffffffff should
  169. * result in a crc32 of 0xf33cb7d3.
  170. *
  171. * The existing kernel crc functions did not cover this polynom yet.
  172. *
  173. * Return: crc32 checksum.
  174. */
  175. u32 genwqe_crc32(u8 *buff, size_t len, u32 init)
  176. {
  177. int i;
  178. u32 crc;
  179. crc = init;
  180. while (len--) {
  181. i = ((crc >> 24) ^ *buff++) & 0xFF;
  182. crc = (crc << 8) ^ crc32_tab[i];
  183. }
  184. return crc;
  185. }
  186. void *__genwqe_alloc_consistent(struct genwqe_dev *cd, size_t size,
  187. dma_addr_t *dma_handle)
  188. {
  189. if (get_order(size) >= MAX_ORDER)
  190. return NULL;
  191. return dma_zalloc_coherent(&cd->pci_dev->dev, size, dma_handle,
  192. GFP_KERNEL);
  193. }
  194. void __genwqe_free_consistent(struct genwqe_dev *cd, size_t size,
  195. void *vaddr, dma_addr_t dma_handle)
  196. {
  197. if (vaddr == NULL)
  198. return;
  199. dma_free_coherent(&cd->pci_dev->dev, size, vaddr, dma_handle);
  200. }
  201. static void genwqe_unmap_pages(struct genwqe_dev *cd, dma_addr_t *dma_list,
  202. int num_pages)
  203. {
  204. int i;
  205. struct pci_dev *pci_dev = cd->pci_dev;
  206. for (i = 0; (i < num_pages) && (dma_list[i] != 0x0); i++) {
  207. pci_unmap_page(pci_dev, dma_list[i],
  208. PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
  209. dma_list[i] = 0x0;
  210. }
  211. }
  212. static int genwqe_map_pages(struct genwqe_dev *cd,
  213. struct page **page_list, int num_pages,
  214. dma_addr_t *dma_list)
  215. {
  216. int i;
  217. struct pci_dev *pci_dev = cd->pci_dev;
  218. /* establish DMA mapping for requested pages */
  219. for (i = 0; i < num_pages; i++) {
  220. dma_addr_t daddr;
  221. dma_list[i] = 0x0;
  222. daddr = pci_map_page(pci_dev, page_list[i],
  223. 0, /* map_offs */
  224. PAGE_SIZE,
  225. PCI_DMA_BIDIRECTIONAL); /* FIXME rd/rw */
  226. if (pci_dma_mapping_error(pci_dev, daddr)) {
  227. dev_err(&pci_dev->dev,
  228. "[%s] err: no dma addr daddr=%016llx!\n",
  229. __func__, (long long)daddr);
  230. goto err;
  231. }
  232. dma_list[i] = daddr;
  233. }
  234. return 0;
  235. err:
  236. genwqe_unmap_pages(cd, dma_list, num_pages);
  237. return -EIO;
  238. }
  239. static int genwqe_sgl_size(int num_pages)
  240. {
  241. int len, num_tlb = num_pages / 7;
  242. len = sizeof(struct sg_entry) * (num_pages+num_tlb + 1);
  243. return roundup(len, PAGE_SIZE);
  244. }
  245. /**
  246. * genwqe_alloc_sync_sgl() - Allocate memory for sgl and overlapping pages
  247. *
  248. * Allocates memory for sgl and overlapping pages. Pages which might
  249. * overlap other user-space memory blocks are being cached for DMAs,
  250. * such that we do not run into syncronization issues. Data is copied
  251. * from user-space into the cached pages.
  252. */
  253. int genwqe_alloc_sync_sgl(struct genwqe_dev *cd, struct genwqe_sgl *sgl,
  254. void __user *user_addr, size_t user_size, int write)
  255. {
  256. int ret = -ENOMEM;
  257. struct pci_dev *pci_dev = cd->pci_dev;
  258. sgl->fpage_offs = offset_in_page((unsigned long)user_addr);
  259. sgl->fpage_size = min_t(size_t, PAGE_SIZE-sgl->fpage_offs, user_size);
  260. sgl->nr_pages = DIV_ROUND_UP(sgl->fpage_offs + user_size, PAGE_SIZE);
  261. sgl->lpage_size = (user_size - sgl->fpage_size) % PAGE_SIZE;
  262. dev_dbg(&pci_dev->dev, "[%s] uaddr=%p usize=%8ld nr_pages=%ld fpage_offs=%lx fpage_size=%ld lpage_size=%ld\n",
  263. __func__, user_addr, user_size, sgl->nr_pages,
  264. sgl->fpage_offs, sgl->fpage_size, sgl->lpage_size);
  265. sgl->user_addr = user_addr;
  266. sgl->user_size = user_size;
  267. sgl->write = write;
  268. sgl->sgl_size = genwqe_sgl_size(sgl->nr_pages);
  269. if (get_order(sgl->sgl_size) > MAX_ORDER) {
  270. dev_err(&pci_dev->dev,
  271. "[%s] err: too much memory requested!\n", __func__);
  272. return ret;
  273. }
  274. sgl->sgl = __genwqe_alloc_consistent(cd, sgl->sgl_size,
  275. &sgl->sgl_dma_addr);
  276. if (sgl->sgl == NULL) {
  277. dev_err(&pci_dev->dev,
  278. "[%s] err: no memory available!\n", __func__);
  279. return ret;
  280. }
  281. /* Only use buffering on incomplete pages */
  282. if ((sgl->fpage_size != 0) && (sgl->fpage_size != PAGE_SIZE)) {
  283. sgl->fpage = __genwqe_alloc_consistent(cd, PAGE_SIZE,
  284. &sgl->fpage_dma_addr);
  285. if (sgl->fpage == NULL)
  286. goto err_out;
  287. /* Sync with user memory */
  288. if (copy_from_user(sgl->fpage + sgl->fpage_offs,
  289. user_addr, sgl->fpage_size)) {
  290. ret = -EFAULT;
  291. goto err_out;
  292. }
  293. }
  294. if (sgl->lpage_size != 0) {
  295. sgl->lpage = __genwqe_alloc_consistent(cd, PAGE_SIZE,
  296. &sgl->lpage_dma_addr);
  297. if (sgl->lpage == NULL)
  298. goto err_out1;
  299. /* Sync with user memory */
  300. if (copy_from_user(sgl->lpage, user_addr + user_size -
  301. sgl->lpage_size, sgl->lpage_size)) {
  302. ret = -EFAULT;
  303. goto err_out2;
  304. }
  305. }
  306. return 0;
  307. err_out2:
  308. __genwqe_free_consistent(cd, PAGE_SIZE, sgl->lpage,
  309. sgl->lpage_dma_addr);
  310. sgl->lpage = NULL;
  311. sgl->lpage_dma_addr = 0;
  312. err_out1:
  313. __genwqe_free_consistent(cd, PAGE_SIZE, sgl->fpage,
  314. sgl->fpage_dma_addr);
  315. sgl->fpage = NULL;
  316. sgl->fpage_dma_addr = 0;
  317. err_out:
  318. __genwqe_free_consistent(cd, sgl->sgl_size, sgl->sgl,
  319. sgl->sgl_dma_addr);
  320. sgl->sgl = NULL;
  321. sgl->sgl_dma_addr = 0;
  322. sgl->sgl_size = 0;
  323. return ret;
  324. }
  325. int genwqe_setup_sgl(struct genwqe_dev *cd, struct genwqe_sgl *sgl,
  326. dma_addr_t *dma_list)
  327. {
  328. int i = 0, j = 0, p;
  329. unsigned long dma_offs, map_offs;
  330. dma_addr_t prev_daddr = 0;
  331. struct sg_entry *s, *last_s = NULL;
  332. size_t size = sgl->user_size;
  333. dma_offs = 128; /* next block if needed/dma_offset */
  334. map_offs = sgl->fpage_offs; /* offset in first page */
  335. s = &sgl->sgl[0]; /* first set of 8 entries */
  336. p = 0; /* page */
  337. while (p < sgl->nr_pages) {
  338. dma_addr_t daddr;
  339. unsigned int size_to_map;
  340. /* always write the chaining entry, cleanup is done later */
  341. j = 0;
  342. s[j].target_addr = cpu_to_be64(sgl->sgl_dma_addr + dma_offs);
  343. s[j].len = cpu_to_be32(128);
  344. s[j].flags = cpu_to_be32(SG_CHAINED);
  345. j++;
  346. while (j < 8) {
  347. /* DMA mapping for requested page, offs, size */
  348. size_to_map = min(size, PAGE_SIZE - map_offs);
  349. if ((p == 0) && (sgl->fpage != NULL)) {
  350. daddr = sgl->fpage_dma_addr + map_offs;
  351. } else if ((p == sgl->nr_pages - 1) &&
  352. (sgl->lpage != NULL)) {
  353. daddr = sgl->lpage_dma_addr;
  354. } else {
  355. daddr = dma_list[p] + map_offs;
  356. }
  357. size -= size_to_map;
  358. map_offs = 0;
  359. if (prev_daddr == daddr) {
  360. u32 prev_len = be32_to_cpu(last_s->len);
  361. /* pr_info("daddr combining: "
  362. "%016llx/%08x -> %016llx\n",
  363. prev_daddr, prev_len, daddr); */
  364. last_s->len = cpu_to_be32(prev_len +
  365. size_to_map);
  366. p++; /* process next page */
  367. if (p == sgl->nr_pages)
  368. goto fixup; /* nothing to do */
  369. prev_daddr = daddr + size_to_map;
  370. continue;
  371. }
  372. /* start new entry */
  373. s[j].target_addr = cpu_to_be64(daddr);
  374. s[j].len = cpu_to_be32(size_to_map);
  375. s[j].flags = cpu_to_be32(SG_DATA);
  376. prev_daddr = daddr + size_to_map;
  377. last_s = &s[j];
  378. j++;
  379. p++; /* process next page */
  380. if (p == sgl->nr_pages)
  381. goto fixup; /* nothing to do */
  382. }
  383. dma_offs += 128;
  384. s += 8; /* continue 8 elements further */
  385. }
  386. fixup:
  387. if (j == 1) { /* combining happened on last entry! */
  388. s -= 8; /* full shift needed on previous sgl block */
  389. j = 7; /* shift all elements */
  390. }
  391. for (i = 0; i < j; i++) /* move elements 1 up */
  392. s[i] = s[i + 1];
  393. s[i].target_addr = cpu_to_be64(0);
  394. s[i].len = cpu_to_be32(0);
  395. s[i].flags = cpu_to_be32(SG_END_LIST);
  396. return 0;
  397. }
  398. /**
  399. * genwqe_free_sync_sgl() - Free memory for sgl and overlapping pages
  400. *
  401. * After the DMA transfer has been completed we free the memory for
  402. * the sgl and the cached pages. Data is being transferred from cached
  403. * pages into user-space buffers.
  404. */
  405. int genwqe_free_sync_sgl(struct genwqe_dev *cd, struct genwqe_sgl *sgl)
  406. {
  407. int rc = 0;
  408. size_t offset;
  409. unsigned long res;
  410. struct pci_dev *pci_dev = cd->pci_dev;
  411. if (sgl->fpage) {
  412. if (sgl->write) {
  413. res = copy_to_user(sgl->user_addr,
  414. sgl->fpage + sgl->fpage_offs, sgl->fpage_size);
  415. if (res) {
  416. dev_err(&pci_dev->dev,
  417. "[%s] err: copying fpage! (res=%lu)\n",
  418. __func__, res);
  419. rc = -EFAULT;
  420. }
  421. }
  422. __genwqe_free_consistent(cd, PAGE_SIZE, sgl->fpage,
  423. sgl->fpage_dma_addr);
  424. sgl->fpage = NULL;
  425. sgl->fpage_dma_addr = 0;
  426. }
  427. if (sgl->lpage) {
  428. if (sgl->write) {
  429. offset = sgl->user_size - sgl->lpage_size;
  430. res = copy_to_user(sgl->user_addr + offset, sgl->lpage,
  431. sgl->lpage_size);
  432. if (res) {
  433. dev_err(&pci_dev->dev,
  434. "[%s] err: copying lpage! (res=%lu)\n",
  435. __func__, res);
  436. rc = -EFAULT;
  437. }
  438. }
  439. __genwqe_free_consistent(cd, PAGE_SIZE, sgl->lpage,
  440. sgl->lpage_dma_addr);
  441. sgl->lpage = NULL;
  442. sgl->lpage_dma_addr = 0;
  443. }
  444. __genwqe_free_consistent(cd, sgl->sgl_size, sgl->sgl,
  445. sgl->sgl_dma_addr);
  446. sgl->sgl = NULL;
  447. sgl->sgl_dma_addr = 0x0;
  448. sgl->sgl_size = 0;
  449. return rc;
  450. }
  451. /**
  452. * genwqe_free_user_pages() - Give pinned pages back
  453. *
  454. * Documentation of get_user_pages is in mm/gup.c:
  455. *
  456. * If the page is written to, set_page_dirty (or set_page_dirty_lock,
  457. * as appropriate) must be called after the page is finished with, and
  458. * before put_page is called.
  459. */
  460. static int genwqe_free_user_pages(struct page **page_list,
  461. unsigned int nr_pages, int dirty)
  462. {
  463. unsigned int i;
  464. for (i = 0; i < nr_pages; i++) {
  465. if (page_list[i] != NULL) {
  466. if (dirty)
  467. set_page_dirty_lock(page_list[i]);
  468. put_page(page_list[i]);
  469. }
  470. }
  471. return 0;
  472. }
  473. /**
  474. * genwqe_user_vmap() - Map user-space memory to virtual kernel memory
  475. * @cd: pointer to genwqe device
  476. * @m: mapping params
  477. * @uaddr: user virtual address
  478. * @size: size of memory to be mapped
  479. *
  480. * We need to think about how we could speed this up. Of course it is
  481. * not a good idea to do this over and over again, like we are
  482. * currently doing it. Nevertheless, I am curious where on the path
  483. * the performance is spend. Most probably within the memory
  484. * allocation functions, but maybe also in the DMA mapping code.
  485. *
  486. * Restrictions: The maximum size of the possible mapping currently depends
  487. * on the amount of memory we can get using kzalloc() for the
  488. * page_list and pci_alloc_consistent for the sg_list.
  489. * The sg_list is currently itself not scattered, which could
  490. * be fixed with some effort. The page_list must be split into
  491. * PAGE_SIZE chunks too. All that will make the complicated
  492. * code more complicated.
  493. *
  494. * Return: 0 if success
  495. */
  496. int genwqe_user_vmap(struct genwqe_dev *cd, struct dma_mapping *m, void *uaddr,
  497. unsigned long size)
  498. {
  499. int rc = -EINVAL;
  500. unsigned long data, offs;
  501. struct pci_dev *pci_dev = cd->pci_dev;
  502. if ((uaddr == NULL) || (size == 0)) {
  503. m->size = 0; /* mark unused and not added */
  504. return -EINVAL;
  505. }
  506. m->u_vaddr = uaddr;
  507. m->size = size;
  508. /* determine space needed for page_list. */
  509. data = (unsigned long)uaddr;
  510. offs = offset_in_page(data);
  511. if (size > ULONG_MAX - PAGE_SIZE - offs) {
  512. m->size = 0; /* mark unused and not added */
  513. return -EINVAL;
  514. }
  515. m->nr_pages = DIV_ROUND_UP(offs + size, PAGE_SIZE);
  516. m->page_list = kcalloc(m->nr_pages,
  517. sizeof(struct page *) + sizeof(dma_addr_t),
  518. GFP_KERNEL);
  519. if (!m->page_list) {
  520. dev_err(&pci_dev->dev, "err: alloc page_list failed\n");
  521. m->nr_pages = 0;
  522. m->u_vaddr = NULL;
  523. m->size = 0; /* mark unused and not added */
  524. return -ENOMEM;
  525. }
  526. m->dma_list = (dma_addr_t *)(m->page_list + m->nr_pages);
  527. /* pin user pages in memory */
  528. rc = get_user_pages_fast(data & PAGE_MASK, /* page aligned addr */
  529. m->nr_pages,
  530. m->write, /* readable/writable */
  531. m->page_list); /* ptrs to pages */
  532. if (rc < 0)
  533. goto fail_get_user_pages;
  534. /* assumption: get_user_pages can be killed by signals. */
  535. if (rc < m->nr_pages) {
  536. genwqe_free_user_pages(m->page_list, rc, m->write);
  537. rc = -EFAULT;
  538. goto fail_get_user_pages;
  539. }
  540. rc = genwqe_map_pages(cd, m->page_list, m->nr_pages, m->dma_list);
  541. if (rc != 0)
  542. goto fail_free_user_pages;
  543. return 0;
  544. fail_free_user_pages:
  545. genwqe_free_user_pages(m->page_list, m->nr_pages, m->write);
  546. fail_get_user_pages:
  547. kfree(m->page_list);
  548. m->page_list = NULL;
  549. m->dma_list = NULL;
  550. m->nr_pages = 0;
  551. m->u_vaddr = NULL;
  552. m->size = 0; /* mark unused and not added */
  553. return rc;
  554. }
  555. /**
  556. * genwqe_user_vunmap() - Undo mapping of user-space mem to virtual kernel
  557. * memory
  558. * @cd: pointer to genwqe device
  559. * @m: mapping params
  560. */
  561. int genwqe_user_vunmap(struct genwqe_dev *cd, struct dma_mapping *m)
  562. {
  563. struct pci_dev *pci_dev = cd->pci_dev;
  564. if (!dma_mapping_used(m)) {
  565. dev_err(&pci_dev->dev, "[%s] err: mapping %p not used!\n",
  566. __func__, m);
  567. return -EINVAL;
  568. }
  569. if (m->dma_list)
  570. genwqe_unmap_pages(cd, m->dma_list, m->nr_pages);
  571. if (m->page_list) {
  572. genwqe_free_user_pages(m->page_list, m->nr_pages, m->write);
  573. kfree(m->page_list);
  574. m->page_list = NULL;
  575. m->dma_list = NULL;
  576. m->nr_pages = 0;
  577. }
  578. m->u_vaddr = NULL;
  579. m->size = 0; /* mark as unused and not added */
  580. return 0;
  581. }
  582. /**
  583. * genwqe_card_type() - Get chip type SLU Configuration Register
  584. * @cd: pointer to the genwqe device descriptor
  585. * Return: 0: Altera Stratix-IV 230
  586. * 1: Altera Stratix-IV 530
  587. * 2: Altera Stratix-V A4
  588. * 3: Altera Stratix-V A7
  589. */
  590. u8 genwqe_card_type(struct genwqe_dev *cd)
  591. {
  592. u64 card_type = cd->slu_unitcfg;
  593. return (u8)((card_type & IO_SLU_UNITCFG_TYPE_MASK) >> 20);
  594. }
  595. /**
  596. * genwqe_card_reset() - Reset the card
  597. * @cd: pointer to the genwqe device descriptor
  598. */
  599. int genwqe_card_reset(struct genwqe_dev *cd)
  600. {
  601. u64 softrst;
  602. struct pci_dev *pci_dev = cd->pci_dev;
  603. if (!genwqe_is_privileged(cd))
  604. return -ENODEV;
  605. /* new SL */
  606. __genwqe_writeq(cd, IO_SLC_CFGREG_SOFTRESET, 0x1ull);
  607. msleep(1000);
  608. __genwqe_readq(cd, IO_HSU_FIR_CLR);
  609. __genwqe_readq(cd, IO_APP_FIR_CLR);
  610. __genwqe_readq(cd, IO_SLU_FIR_CLR);
  611. /*
  612. * Read-modify-write to preserve the stealth bits
  613. *
  614. * For SL >= 039, Stealth WE bit allows removing
  615. * the read-modify-wrote.
  616. * r-m-w may require a mask 0x3C to avoid hitting hard
  617. * reset again for error reset (should be 0, chicken).
  618. */
  619. softrst = __genwqe_readq(cd, IO_SLC_CFGREG_SOFTRESET) & 0x3cull;
  620. __genwqe_writeq(cd, IO_SLC_CFGREG_SOFTRESET, softrst | 0x2ull);
  621. /* give ERRORRESET some time to finish */
  622. msleep(50);
  623. if (genwqe_need_err_masking(cd)) {
  624. dev_info(&pci_dev->dev,
  625. "[%s] masking errors for old bitstreams\n", __func__);
  626. __genwqe_writeq(cd, IO_SLC_MISC_DEBUG, 0x0aull);
  627. }
  628. return 0;
  629. }
  630. int genwqe_read_softreset(struct genwqe_dev *cd)
  631. {
  632. u64 bitstream;
  633. if (!genwqe_is_privileged(cd))
  634. return -ENODEV;
  635. bitstream = __genwqe_readq(cd, IO_SLU_BITSTREAM) & 0x1;
  636. cd->softreset = (bitstream == 0) ? 0x8ull : 0xcull;
  637. return 0;
  638. }
  639. /**
  640. * genwqe_set_interrupt_capability() - Configure MSI capability structure
  641. * @cd: pointer to the device
  642. * Return: 0 if no error
  643. */
  644. int genwqe_set_interrupt_capability(struct genwqe_dev *cd, int count)
  645. {
  646. int rc;
  647. rc = pci_alloc_irq_vectors(cd->pci_dev, 1, count, PCI_IRQ_MSI);
  648. if (rc < 0)
  649. return rc;
  650. return 0;
  651. }
  652. /**
  653. * genwqe_reset_interrupt_capability() - Undo genwqe_set_interrupt_capability()
  654. * @cd: pointer to the device
  655. */
  656. void genwqe_reset_interrupt_capability(struct genwqe_dev *cd)
  657. {
  658. pci_free_irq_vectors(cd->pci_dev);
  659. }
  660. /**
  661. * set_reg_idx() - Fill array with data. Ignore illegal offsets.
  662. * @cd: card device
  663. * @r: debug register array
  664. * @i: index to desired entry
  665. * @m: maximum possible entries
  666. * @addr: addr which is read
  667. * @index: index in debug array
  668. * @val: read value
  669. */
  670. static int set_reg_idx(struct genwqe_dev *cd, struct genwqe_reg *r,
  671. unsigned int *i, unsigned int m, u32 addr, u32 idx,
  672. u64 val)
  673. {
  674. if (WARN_ON_ONCE(*i >= m))
  675. return -EFAULT;
  676. r[*i].addr = addr;
  677. r[*i].idx = idx;
  678. r[*i].val = val;
  679. ++*i;
  680. return 0;
  681. }
  682. static int set_reg(struct genwqe_dev *cd, struct genwqe_reg *r,
  683. unsigned int *i, unsigned int m, u32 addr, u64 val)
  684. {
  685. return set_reg_idx(cd, r, i, m, addr, 0, val);
  686. }
  687. int genwqe_read_ffdc_regs(struct genwqe_dev *cd, struct genwqe_reg *regs,
  688. unsigned int max_regs, int all)
  689. {
  690. unsigned int i, j, idx = 0;
  691. u32 ufir_addr, ufec_addr, sfir_addr, sfec_addr;
  692. u64 gfir, sluid, appid, ufir, ufec, sfir, sfec;
  693. /* Global FIR */
  694. gfir = __genwqe_readq(cd, IO_SLC_CFGREG_GFIR);
  695. set_reg(cd, regs, &idx, max_regs, IO_SLC_CFGREG_GFIR, gfir);
  696. /* UnitCfg for SLU */
  697. sluid = __genwqe_readq(cd, IO_SLU_UNITCFG); /* 0x00000000 */
  698. set_reg(cd, regs, &idx, max_regs, IO_SLU_UNITCFG, sluid);
  699. /* UnitCfg for APP */
  700. appid = __genwqe_readq(cd, IO_APP_UNITCFG); /* 0x02000000 */
  701. set_reg(cd, regs, &idx, max_regs, IO_APP_UNITCFG, appid);
  702. /* Check all chip Units */
  703. for (i = 0; i < GENWQE_MAX_UNITS; i++) {
  704. /* Unit FIR */
  705. ufir_addr = (i << 24) | 0x008;
  706. ufir = __genwqe_readq(cd, ufir_addr);
  707. set_reg(cd, regs, &idx, max_regs, ufir_addr, ufir);
  708. /* Unit FEC */
  709. ufec_addr = (i << 24) | 0x018;
  710. ufec = __genwqe_readq(cd, ufec_addr);
  711. set_reg(cd, regs, &idx, max_regs, ufec_addr, ufec);
  712. for (j = 0; j < 64; j++) {
  713. /* wherever there is a primary 1, read the 2ndary */
  714. if (!all && (!(ufir & (1ull << j))))
  715. continue;
  716. sfir_addr = (i << 24) | (0x100 + 8 * j);
  717. sfir = __genwqe_readq(cd, sfir_addr);
  718. set_reg(cd, regs, &idx, max_regs, sfir_addr, sfir);
  719. sfec_addr = (i << 24) | (0x300 + 8 * j);
  720. sfec = __genwqe_readq(cd, sfec_addr);
  721. set_reg(cd, regs, &idx, max_regs, sfec_addr, sfec);
  722. }
  723. }
  724. /* fill with invalid data until end */
  725. for (i = idx; i < max_regs; i++) {
  726. regs[i].addr = 0xffffffff;
  727. regs[i].val = 0xffffffffffffffffull;
  728. }
  729. return idx;
  730. }
  731. /**
  732. * genwqe_ffdc_buff_size() - Calculates the number of dump registers
  733. */
  734. int genwqe_ffdc_buff_size(struct genwqe_dev *cd, int uid)
  735. {
  736. int entries = 0, ring, traps, traces, trace_entries;
  737. u32 eevptr_addr, l_addr, d_len, d_type;
  738. u64 eevptr, val, addr;
  739. eevptr_addr = GENWQE_UID_OFFS(uid) | IO_EXTENDED_ERROR_POINTER;
  740. eevptr = __genwqe_readq(cd, eevptr_addr);
  741. if ((eevptr != 0x0) && (eevptr != -1ull)) {
  742. l_addr = GENWQE_UID_OFFS(uid) | eevptr;
  743. while (1) {
  744. val = __genwqe_readq(cd, l_addr);
  745. if ((val == 0x0) || (val == -1ull))
  746. break;
  747. /* 38:24 */
  748. d_len = (val & 0x0000007fff000000ull) >> 24;
  749. /* 39 */
  750. d_type = (val & 0x0000008000000000ull) >> 36;
  751. if (d_type) { /* repeat */
  752. entries += d_len;
  753. } else { /* size in bytes! */
  754. entries += d_len >> 3;
  755. }
  756. l_addr += 8;
  757. }
  758. }
  759. for (ring = 0; ring < 8; ring++) {
  760. addr = GENWQE_UID_OFFS(uid) | IO_EXTENDED_DIAG_MAP(ring);
  761. val = __genwqe_readq(cd, addr);
  762. if ((val == 0x0ull) || (val == -1ull))
  763. continue;
  764. traps = (val >> 24) & 0xff;
  765. traces = (val >> 16) & 0xff;
  766. trace_entries = val & 0xffff;
  767. entries += traps + (traces * trace_entries);
  768. }
  769. return entries;
  770. }
  771. /**
  772. * genwqe_ffdc_buff_read() - Implements LogoutExtendedErrorRegisters procedure
  773. */
  774. int genwqe_ffdc_buff_read(struct genwqe_dev *cd, int uid,
  775. struct genwqe_reg *regs, unsigned int max_regs)
  776. {
  777. int i, traps, traces, trace, trace_entries, trace_entry, ring;
  778. unsigned int idx = 0;
  779. u32 eevptr_addr, l_addr, d_addr, d_len, d_type;
  780. u64 eevptr, e, val, addr;
  781. eevptr_addr = GENWQE_UID_OFFS(uid) | IO_EXTENDED_ERROR_POINTER;
  782. eevptr = __genwqe_readq(cd, eevptr_addr);
  783. if ((eevptr != 0x0) && (eevptr != 0xffffffffffffffffull)) {
  784. l_addr = GENWQE_UID_OFFS(uid) | eevptr;
  785. while (1) {
  786. e = __genwqe_readq(cd, l_addr);
  787. if ((e == 0x0) || (e == 0xffffffffffffffffull))
  788. break;
  789. d_addr = (e & 0x0000000000ffffffull); /* 23:0 */
  790. d_len = (e & 0x0000007fff000000ull) >> 24; /* 38:24 */
  791. d_type = (e & 0x0000008000000000ull) >> 36; /* 39 */
  792. d_addr |= GENWQE_UID_OFFS(uid);
  793. if (d_type) {
  794. for (i = 0; i < (int)d_len; i++) {
  795. val = __genwqe_readq(cd, d_addr);
  796. set_reg_idx(cd, regs, &idx, max_regs,
  797. d_addr, i, val);
  798. }
  799. } else {
  800. d_len >>= 3; /* Size in bytes! */
  801. for (i = 0; i < (int)d_len; i++, d_addr += 8) {
  802. val = __genwqe_readq(cd, d_addr);
  803. set_reg_idx(cd, regs, &idx, max_regs,
  804. d_addr, 0, val);
  805. }
  806. }
  807. l_addr += 8;
  808. }
  809. }
  810. /*
  811. * To save time, there are only 6 traces poplulated on Uid=2,
  812. * Ring=1. each with iters=512.
  813. */
  814. for (ring = 0; ring < 8; ring++) { /* 0 is fls, 1 is fds,
  815. 2...7 are ASI rings */
  816. addr = GENWQE_UID_OFFS(uid) | IO_EXTENDED_DIAG_MAP(ring);
  817. val = __genwqe_readq(cd, addr);
  818. if ((val == 0x0ull) || (val == -1ull))
  819. continue;
  820. traps = (val >> 24) & 0xff; /* Number of Traps */
  821. traces = (val >> 16) & 0xff; /* Number of Traces */
  822. trace_entries = val & 0xffff; /* Entries per trace */
  823. /* Note: This is a combined loop that dumps both the traps */
  824. /* (for the trace == 0 case) as well as the traces 1 to */
  825. /* 'traces'. */
  826. for (trace = 0; trace <= traces; trace++) {
  827. u32 diag_sel =
  828. GENWQE_EXTENDED_DIAG_SELECTOR(ring, trace);
  829. addr = (GENWQE_UID_OFFS(uid) |
  830. IO_EXTENDED_DIAG_SELECTOR);
  831. __genwqe_writeq(cd, addr, diag_sel);
  832. for (trace_entry = 0;
  833. trace_entry < (trace ? trace_entries : traps);
  834. trace_entry++) {
  835. addr = (GENWQE_UID_OFFS(uid) |
  836. IO_EXTENDED_DIAG_READ_MBX);
  837. val = __genwqe_readq(cd, addr);
  838. set_reg_idx(cd, regs, &idx, max_regs, addr,
  839. (diag_sel<<16) | trace_entry, val);
  840. }
  841. }
  842. }
  843. return 0;
  844. }
  845. /**
  846. * genwqe_write_vreg() - Write register in virtual window
  847. *
  848. * Note, these registers are only accessible to the PF through the
  849. * VF-window. It is not intended for the VF to access.
  850. */
  851. int genwqe_write_vreg(struct genwqe_dev *cd, u32 reg, u64 val, int func)
  852. {
  853. __genwqe_writeq(cd, IO_PF_SLC_VIRTUAL_WINDOW, func & 0xf);
  854. __genwqe_writeq(cd, reg, val);
  855. return 0;
  856. }
  857. /**
  858. * genwqe_read_vreg() - Read register in virtual window
  859. *
  860. * Note, these registers are only accessible to the PF through the
  861. * VF-window. It is not intended for the VF to access.
  862. */
  863. u64 genwqe_read_vreg(struct genwqe_dev *cd, u32 reg, int func)
  864. {
  865. __genwqe_writeq(cd, IO_PF_SLC_VIRTUAL_WINDOW, func & 0xf);
  866. return __genwqe_readq(cd, reg);
  867. }
  868. /**
  869. * genwqe_base_clock_frequency() - Deteremine base clock frequency of the card
  870. *
  871. * Note: From a design perspective it turned out to be a bad idea to
  872. * use codes here to specifiy the frequency/speed values. An old
  873. * driver cannot understand new codes and is therefore always a
  874. * problem. Better is to measure out the value or put the
  875. * speed/frequency directly into a register which is always a valid
  876. * value for old as well as for new software.
  877. *
  878. * Return: Card clock in MHz
  879. */
  880. int genwqe_base_clock_frequency(struct genwqe_dev *cd)
  881. {
  882. u16 speed; /* MHz MHz MHz MHz */
  883. static const int speed_grade[] = { 250, 200, 166, 175 };
  884. speed = (u16)((cd->slu_unitcfg >> 28) & 0x0full);
  885. if (speed >= ARRAY_SIZE(speed_grade))
  886. return 0; /* illegal value */
  887. return speed_grade[speed];
  888. }
  889. /**
  890. * genwqe_stop_traps() - Stop traps
  891. *
  892. * Before reading out the analysis data, we need to stop the traps.
  893. */
  894. void genwqe_stop_traps(struct genwqe_dev *cd)
  895. {
  896. __genwqe_writeq(cd, IO_SLC_MISC_DEBUG_SET, 0xcull);
  897. }
  898. /**
  899. * genwqe_start_traps() - Start traps
  900. *
  901. * After having read the data, we can/must enable the traps again.
  902. */
  903. void genwqe_start_traps(struct genwqe_dev *cd)
  904. {
  905. __genwqe_writeq(cd, IO_SLC_MISC_DEBUG_CLR, 0xcull);
  906. if (genwqe_need_err_masking(cd))
  907. __genwqe_writeq(cd, IO_SLC_MISC_DEBUG, 0x0aull);
  908. }