mbcs.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (c) 2005 Silicon Graphics, Inc. All rights reserved.
  7. */
  8. /*
  9. * MOATB Core Services driver.
  10. */
  11. #include <linux/interrupt.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/types.h>
  15. #include <linux/ioport.h>
  16. #include <linux/kernel.h>
  17. #include <linux/notifier.h>
  18. #include <linux/reboot.h>
  19. #include <linux/init.h>
  20. #include <linux/fs.h>
  21. #include <linux/delay.h>
  22. #include <linux/device.h>
  23. #include <linux/mm.h>
  24. #include <linux/uio.h>
  25. #include <linux/mutex.h>
  26. #include <linux/slab.h>
  27. #include <linux/pagemap.h>
  28. #include <asm/io.h>
  29. #include <asm/uaccess.h>
  30. #include <asm/pgtable.h>
  31. #include <asm/sn/addrs.h>
  32. #include <asm/sn/intr.h>
  33. #include <asm/sn/tiocx.h>
  34. #include "mbcs.h"
  35. #define MBCS_DEBUG 0
  36. #if MBCS_DEBUG
  37. #define DBG(fmt...) printk(KERN_ALERT fmt)
  38. #else
  39. #define DBG(fmt...)
  40. #endif
  41. static DEFINE_MUTEX(mbcs_mutex);
  42. static int mbcs_major;
  43. static LIST_HEAD(soft_list);
  44. /*
  45. * file operations
  46. */
  47. static const struct file_operations mbcs_ops = {
  48. .open = mbcs_open,
  49. .llseek = mbcs_sram_llseek,
  50. .read = mbcs_sram_read,
  51. .write = mbcs_sram_write,
  52. .mmap = mbcs_gscr_mmap,
  53. };
  54. struct mbcs_callback_arg {
  55. int minor;
  56. struct cx_dev *cx_dev;
  57. };
  58. static inline void mbcs_getdma_init(struct getdma *gdma)
  59. {
  60. memset(gdma, 0, sizeof(struct getdma));
  61. gdma->DoneIntEnable = 1;
  62. }
  63. static inline void mbcs_putdma_init(struct putdma *pdma)
  64. {
  65. memset(pdma, 0, sizeof(struct putdma));
  66. pdma->DoneIntEnable = 1;
  67. }
  68. static inline void mbcs_algo_init(struct algoblock *algo_soft)
  69. {
  70. memset(algo_soft, 0, sizeof(struct algoblock));
  71. }
  72. static inline void mbcs_getdma_set(void *mmr,
  73. uint64_t hostAddr,
  74. uint64_t localAddr,
  75. uint64_t localRamSel,
  76. uint64_t numPkts,
  77. uint64_t amoEnable,
  78. uint64_t intrEnable,
  79. uint64_t peerIO,
  80. uint64_t amoHostDest,
  81. uint64_t amoModType, uint64_t intrHostDest,
  82. uint64_t intrVector)
  83. {
  84. union dma_control rdma_control;
  85. union dma_amo_dest amo_dest;
  86. union intr_dest intr_dest;
  87. union dma_localaddr local_addr;
  88. union dma_hostaddr host_addr;
  89. rdma_control.dma_control_reg = 0;
  90. amo_dest.dma_amo_dest_reg = 0;
  91. intr_dest.intr_dest_reg = 0;
  92. local_addr.dma_localaddr_reg = 0;
  93. host_addr.dma_hostaddr_reg = 0;
  94. host_addr.dma_sys_addr = hostAddr;
  95. MBCS_MMR_SET(mmr, MBCS_RD_DMA_SYS_ADDR, host_addr.dma_hostaddr_reg);
  96. local_addr.dma_ram_addr = localAddr;
  97. local_addr.dma_ram_sel = localRamSel;
  98. MBCS_MMR_SET(mmr, MBCS_RD_DMA_LOC_ADDR, local_addr.dma_localaddr_reg);
  99. rdma_control.dma_op_length = numPkts;
  100. rdma_control.done_amo_en = amoEnable;
  101. rdma_control.done_int_en = intrEnable;
  102. rdma_control.pio_mem_n = peerIO;
  103. MBCS_MMR_SET(mmr, MBCS_RD_DMA_CTRL, rdma_control.dma_control_reg);
  104. amo_dest.dma_amo_sys_addr = amoHostDest;
  105. amo_dest.dma_amo_mod_type = amoModType;
  106. MBCS_MMR_SET(mmr, MBCS_RD_DMA_AMO_DEST, amo_dest.dma_amo_dest_reg);
  107. intr_dest.address = intrHostDest;
  108. intr_dest.int_vector = intrVector;
  109. MBCS_MMR_SET(mmr, MBCS_RD_DMA_INT_DEST, intr_dest.intr_dest_reg);
  110. }
  111. static inline void mbcs_putdma_set(void *mmr,
  112. uint64_t hostAddr,
  113. uint64_t localAddr,
  114. uint64_t localRamSel,
  115. uint64_t numPkts,
  116. uint64_t amoEnable,
  117. uint64_t intrEnable,
  118. uint64_t peerIO,
  119. uint64_t amoHostDest,
  120. uint64_t amoModType,
  121. uint64_t intrHostDest, uint64_t intrVector)
  122. {
  123. union dma_control wdma_control;
  124. union dma_amo_dest amo_dest;
  125. union intr_dest intr_dest;
  126. union dma_localaddr local_addr;
  127. union dma_hostaddr host_addr;
  128. wdma_control.dma_control_reg = 0;
  129. amo_dest.dma_amo_dest_reg = 0;
  130. intr_dest.intr_dest_reg = 0;
  131. local_addr.dma_localaddr_reg = 0;
  132. host_addr.dma_hostaddr_reg = 0;
  133. host_addr.dma_sys_addr = hostAddr;
  134. MBCS_MMR_SET(mmr, MBCS_WR_DMA_SYS_ADDR, host_addr.dma_hostaddr_reg);
  135. local_addr.dma_ram_addr = localAddr;
  136. local_addr.dma_ram_sel = localRamSel;
  137. MBCS_MMR_SET(mmr, MBCS_WR_DMA_LOC_ADDR, local_addr.dma_localaddr_reg);
  138. wdma_control.dma_op_length = numPkts;
  139. wdma_control.done_amo_en = amoEnable;
  140. wdma_control.done_int_en = intrEnable;
  141. wdma_control.pio_mem_n = peerIO;
  142. MBCS_MMR_SET(mmr, MBCS_WR_DMA_CTRL, wdma_control.dma_control_reg);
  143. amo_dest.dma_amo_sys_addr = amoHostDest;
  144. amo_dest.dma_amo_mod_type = amoModType;
  145. MBCS_MMR_SET(mmr, MBCS_WR_DMA_AMO_DEST, amo_dest.dma_amo_dest_reg);
  146. intr_dest.address = intrHostDest;
  147. intr_dest.int_vector = intrVector;
  148. MBCS_MMR_SET(mmr, MBCS_WR_DMA_INT_DEST, intr_dest.intr_dest_reg);
  149. }
  150. static inline void mbcs_algo_set(void *mmr,
  151. uint64_t amoHostDest,
  152. uint64_t amoModType,
  153. uint64_t intrHostDest,
  154. uint64_t intrVector, uint64_t algoStepCount)
  155. {
  156. union dma_amo_dest amo_dest;
  157. union intr_dest intr_dest;
  158. union algo_step step;
  159. step.algo_step_reg = 0;
  160. intr_dest.intr_dest_reg = 0;
  161. amo_dest.dma_amo_dest_reg = 0;
  162. amo_dest.dma_amo_sys_addr = amoHostDest;
  163. amo_dest.dma_amo_mod_type = amoModType;
  164. MBCS_MMR_SET(mmr, MBCS_ALG_AMO_DEST, amo_dest.dma_amo_dest_reg);
  165. intr_dest.address = intrHostDest;
  166. intr_dest.int_vector = intrVector;
  167. MBCS_MMR_SET(mmr, MBCS_ALG_INT_DEST, intr_dest.intr_dest_reg);
  168. step.alg_step_cnt = algoStepCount;
  169. MBCS_MMR_SET(mmr, MBCS_ALG_STEP, step.algo_step_reg);
  170. }
  171. static inline int mbcs_getdma_start(struct mbcs_soft *soft)
  172. {
  173. void *mmr_base;
  174. struct getdma *gdma;
  175. uint64_t numPkts;
  176. union cm_control cm_control;
  177. mmr_base = soft->mmr_base;
  178. gdma = &soft->getdma;
  179. /* check that host address got setup */
  180. if (!gdma->hostAddr)
  181. return -1;
  182. numPkts =
  183. (gdma->bytes + (MBCS_CACHELINE_SIZE - 1)) / MBCS_CACHELINE_SIZE;
  184. /* program engine */
  185. mbcs_getdma_set(mmr_base, tiocx_dma_addr(gdma->hostAddr),
  186. gdma->localAddr,
  187. (gdma->localAddr < MB2) ? 0 :
  188. (gdma->localAddr < MB4) ? 1 :
  189. (gdma->localAddr < MB6) ? 2 : 3,
  190. numPkts,
  191. gdma->DoneAmoEnable,
  192. gdma->DoneIntEnable,
  193. gdma->peerIO,
  194. gdma->amoHostDest,
  195. gdma->amoModType,
  196. gdma->intrHostDest, gdma->intrVector);
  197. /* start engine */
  198. cm_control.cm_control_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
  199. cm_control.rd_dma_go = 1;
  200. MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL, cm_control.cm_control_reg);
  201. return 0;
  202. }
  203. static inline int mbcs_putdma_start(struct mbcs_soft *soft)
  204. {
  205. void *mmr_base;
  206. struct putdma *pdma;
  207. uint64_t numPkts;
  208. union cm_control cm_control;
  209. mmr_base = soft->mmr_base;
  210. pdma = &soft->putdma;
  211. /* check that host address got setup */
  212. if (!pdma->hostAddr)
  213. return -1;
  214. numPkts =
  215. (pdma->bytes + (MBCS_CACHELINE_SIZE - 1)) / MBCS_CACHELINE_SIZE;
  216. /* program engine */
  217. mbcs_putdma_set(mmr_base, tiocx_dma_addr(pdma->hostAddr),
  218. pdma->localAddr,
  219. (pdma->localAddr < MB2) ? 0 :
  220. (pdma->localAddr < MB4) ? 1 :
  221. (pdma->localAddr < MB6) ? 2 : 3,
  222. numPkts,
  223. pdma->DoneAmoEnable,
  224. pdma->DoneIntEnable,
  225. pdma->peerIO,
  226. pdma->amoHostDest,
  227. pdma->amoModType,
  228. pdma->intrHostDest, pdma->intrVector);
  229. /* start engine */
  230. cm_control.cm_control_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
  231. cm_control.wr_dma_go = 1;
  232. MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL, cm_control.cm_control_reg);
  233. return 0;
  234. }
  235. static inline int mbcs_algo_start(struct mbcs_soft *soft)
  236. {
  237. struct algoblock *algo_soft = &soft->algo;
  238. void *mmr_base = soft->mmr_base;
  239. union cm_control cm_control;
  240. if (mutex_lock_interruptible(&soft->algolock))
  241. return -ERESTARTSYS;
  242. atomic_set(&soft->algo_done, 0);
  243. mbcs_algo_set(mmr_base,
  244. algo_soft->amoHostDest,
  245. algo_soft->amoModType,
  246. algo_soft->intrHostDest,
  247. algo_soft->intrVector, algo_soft->algoStepCount);
  248. /* start algorithm */
  249. cm_control.cm_control_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
  250. cm_control.alg_done_int_en = 1;
  251. cm_control.alg_go = 1;
  252. MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL, cm_control.cm_control_reg);
  253. mutex_unlock(&soft->algolock);
  254. return 0;
  255. }
  256. static inline ssize_t
  257. do_mbcs_sram_dmawrite(struct mbcs_soft *soft, uint64_t hostAddr,
  258. size_t len, loff_t * off)
  259. {
  260. int rv = 0;
  261. if (mutex_lock_interruptible(&soft->dmawritelock))
  262. return -ERESTARTSYS;
  263. atomic_set(&soft->dmawrite_done, 0);
  264. soft->putdma.hostAddr = hostAddr;
  265. soft->putdma.localAddr = *off;
  266. soft->putdma.bytes = len;
  267. if (mbcs_putdma_start(soft) < 0) {
  268. DBG(KERN_ALERT "do_mbcs_sram_dmawrite: "
  269. "mbcs_putdma_start failed\n");
  270. rv = -EAGAIN;
  271. goto dmawrite_exit;
  272. }
  273. if (wait_event_interruptible(soft->dmawrite_queue,
  274. atomic_read(&soft->dmawrite_done))) {
  275. rv = -ERESTARTSYS;
  276. goto dmawrite_exit;
  277. }
  278. rv = len;
  279. *off += len;
  280. dmawrite_exit:
  281. mutex_unlock(&soft->dmawritelock);
  282. return rv;
  283. }
  284. static inline ssize_t
  285. do_mbcs_sram_dmaread(struct mbcs_soft *soft, uint64_t hostAddr,
  286. size_t len, loff_t * off)
  287. {
  288. int rv = 0;
  289. if (mutex_lock_interruptible(&soft->dmareadlock))
  290. return -ERESTARTSYS;
  291. atomic_set(&soft->dmawrite_done, 0);
  292. soft->getdma.hostAddr = hostAddr;
  293. soft->getdma.localAddr = *off;
  294. soft->getdma.bytes = len;
  295. if (mbcs_getdma_start(soft) < 0) {
  296. DBG(KERN_ALERT "mbcs_strategy: mbcs_getdma_start failed\n");
  297. rv = -EAGAIN;
  298. goto dmaread_exit;
  299. }
  300. if (wait_event_interruptible(soft->dmaread_queue,
  301. atomic_read(&soft->dmaread_done))) {
  302. rv = -ERESTARTSYS;
  303. goto dmaread_exit;
  304. }
  305. rv = len;
  306. *off += len;
  307. dmaread_exit:
  308. mutex_unlock(&soft->dmareadlock);
  309. return rv;
  310. }
  311. static int mbcs_open(struct inode *ip, struct file *fp)
  312. {
  313. struct mbcs_soft *soft;
  314. int minor;
  315. mutex_lock(&mbcs_mutex);
  316. minor = iminor(ip);
  317. /* Nothing protects access to this list... */
  318. list_for_each_entry(soft, &soft_list, list) {
  319. if (soft->nasid == minor) {
  320. fp->private_data = soft->cxdev;
  321. mutex_unlock(&mbcs_mutex);
  322. return 0;
  323. }
  324. }
  325. mutex_unlock(&mbcs_mutex);
  326. return -ENODEV;
  327. }
  328. static ssize_t mbcs_sram_read(struct file * fp, char __user *buf, size_t len, loff_t * off)
  329. {
  330. struct cx_dev *cx_dev = fp->private_data;
  331. struct mbcs_soft *soft = cx_dev->soft;
  332. uint64_t hostAddr;
  333. int rv = 0;
  334. hostAddr = __get_dma_pages(GFP_KERNEL, get_order(len));
  335. if (hostAddr == 0)
  336. return -ENOMEM;
  337. rv = do_mbcs_sram_dmawrite(soft, hostAddr, len, off);
  338. if (rv < 0)
  339. goto exit;
  340. if (copy_to_user(buf, (void *)hostAddr, len))
  341. rv = -EFAULT;
  342. exit:
  343. free_pages(hostAddr, get_order(len));
  344. return rv;
  345. }
  346. static ssize_t
  347. mbcs_sram_write(struct file * fp, const char __user *buf, size_t len, loff_t * off)
  348. {
  349. struct cx_dev *cx_dev = fp->private_data;
  350. struct mbcs_soft *soft = cx_dev->soft;
  351. uint64_t hostAddr;
  352. int rv = 0;
  353. hostAddr = __get_dma_pages(GFP_KERNEL, get_order(len));
  354. if (hostAddr == 0)
  355. return -ENOMEM;
  356. if (copy_from_user((void *)hostAddr, buf, len)) {
  357. rv = -EFAULT;
  358. goto exit;
  359. }
  360. rv = do_mbcs_sram_dmaread(soft, hostAddr, len, off);
  361. exit:
  362. free_pages(hostAddr, get_order(len));
  363. return rv;
  364. }
  365. static loff_t mbcs_sram_llseek(struct file * filp, loff_t off, int whence)
  366. {
  367. return generic_file_llseek_size(filp, off, whence, MAX_LFS_FILESIZE,
  368. MBCS_SRAM_SIZE);
  369. }
  370. static uint64_t mbcs_pioaddr(struct mbcs_soft *soft, uint64_t offset)
  371. {
  372. uint64_t mmr_base;
  373. mmr_base = (uint64_t) (soft->mmr_base + offset);
  374. return mmr_base;
  375. }
  376. static void mbcs_debug_pioaddr_set(struct mbcs_soft *soft)
  377. {
  378. soft->debug_addr = mbcs_pioaddr(soft, MBCS_DEBUG_START);
  379. }
  380. static void mbcs_gscr_pioaddr_set(struct mbcs_soft *soft)
  381. {
  382. soft->gscr_addr = mbcs_pioaddr(soft, MBCS_GSCR_START);
  383. }
  384. static int mbcs_gscr_mmap(struct file *fp, struct vm_area_struct *vma)
  385. {
  386. struct cx_dev *cx_dev = fp->private_data;
  387. struct mbcs_soft *soft = cx_dev->soft;
  388. if (vma->vm_pgoff != 0)
  389. return -EINVAL;
  390. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  391. /* Remap-pfn-range will mark the range VM_IO */
  392. if (remap_pfn_range(vma,
  393. vma->vm_start,
  394. __pa(soft->gscr_addr) >> PAGE_SHIFT,
  395. PAGE_SIZE,
  396. vma->vm_page_prot))
  397. return -EAGAIN;
  398. return 0;
  399. }
  400. /**
  401. * mbcs_completion_intr_handler - Primary completion handler.
  402. * @irq: irq
  403. * @arg: soft struct for device
  404. *
  405. */
  406. static irqreturn_t
  407. mbcs_completion_intr_handler(int irq, void *arg)
  408. {
  409. struct mbcs_soft *soft = (struct mbcs_soft *)arg;
  410. void *mmr_base;
  411. union cm_status cm_status;
  412. union cm_control cm_control;
  413. mmr_base = soft->mmr_base;
  414. cm_status.cm_status_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_STATUS);
  415. if (cm_status.rd_dma_done) {
  416. /* stop dma-read engine, clear status */
  417. cm_control.cm_control_reg =
  418. MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
  419. cm_control.rd_dma_clr = 1;
  420. MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL,
  421. cm_control.cm_control_reg);
  422. atomic_set(&soft->dmaread_done, 1);
  423. wake_up(&soft->dmaread_queue);
  424. }
  425. if (cm_status.wr_dma_done) {
  426. /* stop dma-write engine, clear status */
  427. cm_control.cm_control_reg =
  428. MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
  429. cm_control.wr_dma_clr = 1;
  430. MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL,
  431. cm_control.cm_control_reg);
  432. atomic_set(&soft->dmawrite_done, 1);
  433. wake_up(&soft->dmawrite_queue);
  434. }
  435. if (cm_status.alg_done) {
  436. /* clear status */
  437. cm_control.cm_control_reg =
  438. MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
  439. cm_control.alg_done_clr = 1;
  440. MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL,
  441. cm_control.cm_control_reg);
  442. atomic_set(&soft->algo_done, 1);
  443. wake_up(&soft->algo_queue);
  444. }
  445. return IRQ_HANDLED;
  446. }
  447. /**
  448. * mbcs_intr_alloc - Allocate interrupts.
  449. * @dev: device pointer
  450. *
  451. */
  452. static int mbcs_intr_alloc(struct cx_dev *dev)
  453. {
  454. struct sn_irq_info *sn_irq;
  455. struct mbcs_soft *soft;
  456. struct getdma *getdma;
  457. struct putdma *putdma;
  458. struct algoblock *algo;
  459. soft = dev->soft;
  460. getdma = &soft->getdma;
  461. putdma = &soft->putdma;
  462. algo = &soft->algo;
  463. soft->get_sn_irq = NULL;
  464. soft->put_sn_irq = NULL;
  465. soft->algo_sn_irq = NULL;
  466. sn_irq = tiocx_irq_alloc(dev->cx_id.nasid, TIOCX_CORELET, -1, -1, -1);
  467. if (sn_irq == NULL)
  468. return -EAGAIN;
  469. soft->get_sn_irq = sn_irq;
  470. getdma->intrHostDest = sn_irq->irq_xtalkaddr;
  471. getdma->intrVector = sn_irq->irq_irq;
  472. if (request_irq(sn_irq->irq_irq,
  473. (void *)mbcs_completion_intr_handler, IRQF_SHARED,
  474. "MBCS get intr", (void *)soft)) {
  475. tiocx_irq_free(soft->get_sn_irq);
  476. return -EAGAIN;
  477. }
  478. sn_irq = tiocx_irq_alloc(dev->cx_id.nasid, TIOCX_CORELET, -1, -1, -1);
  479. if (sn_irq == NULL) {
  480. free_irq(soft->get_sn_irq->irq_irq, soft);
  481. tiocx_irq_free(soft->get_sn_irq);
  482. return -EAGAIN;
  483. }
  484. soft->put_sn_irq = sn_irq;
  485. putdma->intrHostDest = sn_irq->irq_xtalkaddr;
  486. putdma->intrVector = sn_irq->irq_irq;
  487. if (request_irq(sn_irq->irq_irq,
  488. (void *)mbcs_completion_intr_handler, IRQF_SHARED,
  489. "MBCS put intr", (void *)soft)) {
  490. tiocx_irq_free(soft->put_sn_irq);
  491. free_irq(soft->get_sn_irq->irq_irq, soft);
  492. tiocx_irq_free(soft->get_sn_irq);
  493. return -EAGAIN;
  494. }
  495. sn_irq = tiocx_irq_alloc(dev->cx_id.nasid, TIOCX_CORELET, -1, -1, -1);
  496. if (sn_irq == NULL) {
  497. free_irq(soft->put_sn_irq->irq_irq, soft);
  498. tiocx_irq_free(soft->put_sn_irq);
  499. free_irq(soft->get_sn_irq->irq_irq, soft);
  500. tiocx_irq_free(soft->get_sn_irq);
  501. return -EAGAIN;
  502. }
  503. soft->algo_sn_irq = sn_irq;
  504. algo->intrHostDest = sn_irq->irq_xtalkaddr;
  505. algo->intrVector = sn_irq->irq_irq;
  506. if (request_irq(sn_irq->irq_irq,
  507. (void *)mbcs_completion_intr_handler, IRQF_SHARED,
  508. "MBCS algo intr", (void *)soft)) {
  509. tiocx_irq_free(soft->algo_sn_irq);
  510. free_irq(soft->put_sn_irq->irq_irq, soft);
  511. tiocx_irq_free(soft->put_sn_irq);
  512. free_irq(soft->get_sn_irq->irq_irq, soft);
  513. tiocx_irq_free(soft->get_sn_irq);
  514. return -EAGAIN;
  515. }
  516. return 0;
  517. }
  518. /**
  519. * mbcs_intr_dealloc - Remove interrupts.
  520. * @dev: device pointer
  521. *
  522. */
  523. static void mbcs_intr_dealloc(struct cx_dev *dev)
  524. {
  525. struct mbcs_soft *soft;
  526. soft = dev->soft;
  527. free_irq(soft->get_sn_irq->irq_irq, soft);
  528. tiocx_irq_free(soft->get_sn_irq);
  529. free_irq(soft->put_sn_irq->irq_irq, soft);
  530. tiocx_irq_free(soft->put_sn_irq);
  531. free_irq(soft->algo_sn_irq->irq_irq, soft);
  532. tiocx_irq_free(soft->algo_sn_irq);
  533. }
  534. static inline int mbcs_hw_init(struct mbcs_soft *soft)
  535. {
  536. void *mmr_base = soft->mmr_base;
  537. union cm_control cm_control;
  538. union cm_req_timeout cm_req_timeout;
  539. uint64_t err_stat;
  540. cm_req_timeout.cm_req_timeout_reg =
  541. MBCS_MMR_GET(mmr_base, MBCS_CM_REQ_TOUT);
  542. cm_req_timeout.time_out = MBCS_CM_CONTROL_REQ_TOUT_MASK;
  543. MBCS_MMR_SET(mmr_base, MBCS_CM_REQ_TOUT,
  544. cm_req_timeout.cm_req_timeout_reg);
  545. mbcs_gscr_pioaddr_set(soft);
  546. mbcs_debug_pioaddr_set(soft);
  547. /* clear errors */
  548. err_stat = MBCS_MMR_GET(mmr_base, MBCS_CM_ERR_STAT);
  549. MBCS_MMR_SET(mmr_base, MBCS_CM_CLR_ERR_STAT, err_stat);
  550. MBCS_MMR_ZERO(mmr_base, MBCS_CM_ERROR_DETAIL1);
  551. /* enable interrupts */
  552. /* turn off 2^23 (INT_EN_PIO_REQ_ADDR_INV) */
  553. MBCS_MMR_SET(mmr_base, MBCS_CM_ERR_INT_EN, 0x3ffffff7e00ffUL);
  554. /* arm status regs and clear engines */
  555. cm_control.cm_control_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
  556. cm_control.rearm_stat_regs = 1;
  557. cm_control.alg_clr = 1;
  558. cm_control.wr_dma_clr = 1;
  559. cm_control.rd_dma_clr = 1;
  560. MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL, cm_control.cm_control_reg);
  561. return 0;
  562. }
  563. static ssize_t show_algo(struct device *dev, struct device_attribute *attr, char *buf)
  564. {
  565. struct cx_dev *cx_dev = to_cx_dev(dev);
  566. struct mbcs_soft *soft = cx_dev->soft;
  567. uint64_t debug0;
  568. /*
  569. * By convention, the first debug register contains the
  570. * algorithm number and revision.
  571. */
  572. debug0 = *(uint64_t *) soft->debug_addr;
  573. return sprintf(buf, "0x%x 0x%x\n",
  574. upper_32_bits(debug0), lower_32_bits(debug0));
  575. }
  576. static ssize_t store_algo(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
  577. {
  578. int n;
  579. struct cx_dev *cx_dev = to_cx_dev(dev);
  580. struct mbcs_soft *soft = cx_dev->soft;
  581. if (count <= 0)
  582. return 0;
  583. n = simple_strtoul(buf, NULL, 0);
  584. if (n == 1) {
  585. mbcs_algo_start(soft);
  586. if (wait_event_interruptible(soft->algo_queue,
  587. atomic_read(&soft->algo_done)))
  588. return -ERESTARTSYS;
  589. }
  590. return count;
  591. }
  592. DEVICE_ATTR(algo, 0644, show_algo, store_algo);
  593. /**
  594. * mbcs_probe - Initialize for device
  595. * @dev: device pointer
  596. * @device_id: id table pointer
  597. *
  598. */
  599. static int mbcs_probe(struct cx_dev *dev, const struct cx_device_id *id)
  600. {
  601. struct mbcs_soft *soft;
  602. dev->soft = NULL;
  603. soft = kzalloc(sizeof(struct mbcs_soft), GFP_KERNEL);
  604. if (soft == NULL)
  605. return -ENOMEM;
  606. soft->nasid = dev->cx_id.nasid;
  607. list_add(&soft->list, &soft_list);
  608. soft->mmr_base = (void *)tiocx_swin_base(dev->cx_id.nasid);
  609. dev->soft = soft;
  610. soft->cxdev = dev;
  611. init_waitqueue_head(&soft->dmawrite_queue);
  612. init_waitqueue_head(&soft->dmaread_queue);
  613. init_waitqueue_head(&soft->algo_queue);
  614. mutex_init(&soft->dmawritelock);
  615. mutex_init(&soft->dmareadlock);
  616. mutex_init(&soft->algolock);
  617. mbcs_getdma_init(&soft->getdma);
  618. mbcs_putdma_init(&soft->putdma);
  619. mbcs_algo_init(&soft->algo);
  620. mbcs_hw_init(soft);
  621. /* Allocate interrupts */
  622. mbcs_intr_alloc(dev);
  623. device_create_file(&dev->dev, &dev_attr_algo);
  624. return 0;
  625. }
  626. static int mbcs_remove(struct cx_dev *dev)
  627. {
  628. if (dev->soft) {
  629. mbcs_intr_dealloc(dev);
  630. kfree(dev->soft);
  631. }
  632. device_remove_file(&dev->dev, &dev_attr_algo);
  633. return 0;
  634. }
  635. static const struct cx_device_id mbcs_id_table[] = {
  636. {
  637. .part_num = MBCS_PART_NUM,
  638. .mfg_num = MBCS_MFG_NUM,
  639. },
  640. {
  641. .part_num = MBCS_PART_NUM_ALG0,
  642. .mfg_num = MBCS_MFG_NUM,
  643. },
  644. {0, 0}
  645. };
  646. MODULE_DEVICE_TABLE(cx, mbcs_id_table);
  647. static struct cx_drv mbcs_driver = {
  648. .name = DEVICE_NAME,
  649. .id_table = mbcs_id_table,
  650. .probe = mbcs_probe,
  651. .remove = mbcs_remove,
  652. };
  653. static void __exit mbcs_exit(void)
  654. {
  655. unregister_chrdev(mbcs_major, DEVICE_NAME);
  656. cx_driver_unregister(&mbcs_driver);
  657. }
  658. static int __init mbcs_init(void)
  659. {
  660. int rv;
  661. if (!ia64_platform_is("sn2"))
  662. return -ENODEV;
  663. // Put driver into chrdevs[]. Get major number.
  664. rv = register_chrdev(mbcs_major, DEVICE_NAME, &mbcs_ops);
  665. if (rv < 0) {
  666. DBG(KERN_ALERT "mbcs_init: can't get major number. %d\n", rv);
  667. return rv;
  668. }
  669. mbcs_major = rv;
  670. return cx_driver_register(&mbcs_driver);
  671. }
  672. module_init(mbcs_init);
  673. module_exit(mbcs_exit);
  674. MODULE_AUTHOR("Bruce Losure <blosure@sgi.com>");
  675. MODULE_DESCRIPTION("Driver for MOATB Core Services");
  676. MODULE_LICENSE("GPL");