x86/amd-iommu: Add per IOMMU reference counting
[linux/fpc-iii.git] / drivers / char / mbcs.c
blob87c67b42bc08b03a17aaf410705f8c3e6e2d3638
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.
6 * Copyright (c) 2005 Silicon Graphics, Inc. All rights reserved.
7 */
9 /*
10 * MOATB Core Services driver.
13 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/moduleparam.h>
16 #include <linux/types.h>
17 #include <linux/ioport.h>
18 #include <linux/kernel.h>
19 #include <linux/notifier.h>
20 #include <linux/reboot.h>
21 #include <linux/init.h>
22 #include <linux/fs.h>
23 #include <linux/delay.h>
24 #include <linux/device.h>
25 #include <linux/mm.h>
26 #include <linux/uio.h>
27 #include <linux/mutex.h>
28 #include <linux/smp_lock.h>
29 #include <asm/io.h>
30 #include <asm/uaccess.h>
31 #include <asm/system.h>
32 #include <asm/pgtable.h>
33 #include <asm/sn/addrs.h>
34 #include <asm/sn/intr.h>
35 #include <asm/sn/tiocx.h>
36 #include "mbcs.h"
38 #define MBCS_DEBUG 0
39 #if MBCS_DEBUG
40 #define DBG(fmt...) printk(KERN_ALERT fmt)
41 #else
42 #define DBG(fmt...)
43 #endif
44 static int mbcs_major;
46 static LIST_HEAD(soft_list);
49 * file operations
51 static const struct file_operations mbcs_ops = {
52 .open = mbcs_open,
53 .llseek = mbcs_sram_llseek,
54 .read = mbcs_sram_read,
55 .write = mbcs_sram_write,
56 .mmap = mbcs_gscr_mmap,
59 struct mbcs_callback_arg {
60 int minor;
61 struct cx_dev *cx_dev;
64 static inline void mbcs_getdma_init(struct getdma *gdma)
66 memset(gdma, 0, sizeof(struct getdma));
67 gdma->DoneIntEnable = 1;
70 static inline void mbcs_putdma_init(struct putdma *pdma)
72 memset(pdma, 0, sizeof(struct putdma));
73 pdma->DoneIntEnable = 1;
76 static inline void mbcs_algo_init(struct algoblock *algo_soft)
78 memset(algo_soft, 0, sizeof(struct algoblock));
81 static inline void mbcs_getdma_set(void *mmr,
82 uint64_t hostAddr,
83 uint64_t localAddr,
84 uint64_t localRamSel,
85 uint64_t numPkts,
86 uint64_t amoEnable,
87 uint64_t intrEnable,
88 uint64_t peerIO,
89 uint64_t amoHostDest,
90 uint64_t amoModType, uint64_t intrHostDest,
91 uint64_t intrVector)
93 union dma_control rdma_control;
94 union dma_amo_dest amo_dest;
95 union intr_dest intr_dest;
96 union dma_localaddr local_addr;
97 union dma_hostaddr host_addr;
99 rdma_control.dma_control_reg = 0;
100 amo_dest.dma_amo_dest_reg = 0;
101 intr_dest.intr_dest_reg = 0;
102 local_addr.dma_localaddr_reg = 0;
103 host_addr.dma_hostaddr_reg = 0;
105 host_addr.dma_sys_addr = hostAddr;
106 MBCS_MMR_SET(mmr, MBCS_RD_DMA_SYS_ADDR, host_addr.dma_hostaddr_reg);
108 local_addr.dma_ram_addr = localAddr;
109 local_addr.dma_ram_sel = localRamSel;
110 MBCS_MMR_SET(mmr, MBCS_RD_DMA_LOC_ADDR, local_addr.dma_localaddr_reg);
112 rdma_control.dma_op_length = numPkts;
113 rdma_control.done_amo_en = amoEnable;
114 rdma_control.done_int_en = intrEnable;
115 rdma_control.pio_mem_n = peerIO;
116 MBCS_MMR_SET(mmr, MBCS_RD_DMA_CTRL, rdma_control.dma_control_reg);
118 amo_dest.dma_amo_sys_addr = amoHostDest;
119 amo_dest.dma_amo_mod_type = amoModType;
120 MBCS_MMR_SET(mmr, MBCS_RD_DMA_AMO_DEST, amo_dest.dma_amo_dest_reg);
122 intr_dest.address = intrHostDest;
123 intr_dest.int_vector = intrVector;
124 MBCS_MMR_SET(mmr, MBCS_RD_DMA_INT_DEST, intr_dest.intr_dest_reg);
128 static inline void mbcs_putdma_set(void *mmr,
129 uint64_t hostAddr,
130 uint64_t localAddr,
131 uint64_t localRamSel,
132 uint64_t numPkts,
133 uint64_t amoEnable,
134 uint64_t intrEnable,
135 uint64_t peerIO,
136 uint64_t amoHostDest,
137 uint64_t amoModType,
138 uint64_t intrHostDest, uint64_t intrVector)
140 union dma_control wdma_control;
141 union dma_amo_dest amo_dest;
142 union intr_dest intr_dest;
143 union dma_localaddr local_addr;
144 union dma_hostaddr host_addr;
146 wdma_control.dma_control_reg = 0;
147 amo_dest.dma_amo_dest_reg = 0;
148 intr_dest.intr_dest_reg = 0;
149 local_addr.dma_localaddr_reg = 0;
150 host_addr.dma_hostaddr_reg = 0;
152 host_addr.dma_sys_addr = hostAddr;
153 MBCS_MMR_SET(mmr, MBCS_WR_DMA_SYS_ADDR, host_addr.dma_hostaddr_reg);
155 local_addr.dma_ram_addr = localAddr;
156 local_addr.dma_ram_sel = localRamSel;
157 MBCS_MMR_SET(mmr, MBCS_WR_DMA_LOC_ADDR, local_addr.dma_localaddr_reg);
159 wdma_control.dma_op_length = numPkts;
160 wdma_control.done_amo_en = amoEnable;
161 wdma_control.done_int_en = intrEnable;
162 wdma_control.pio_mem_n = peerIO;
163 MBCS_MMR_SET(mmr, MBCS_WR_DMA_CTRL, wdma_control.dma_control_reg);
165 amo_dest.dma_amo_sys_addr = amoHostDest;
166 amo_dest.dma_amo_mod_type = amoModType;
167 MBCS_MMR_SET(mmr, MBCS_WR_DMA_AMO_DEST, amo_dest.dma_amo_dest_reg);
169 intr_dest.address = intrHostDest;
170 intr_dest.int_vector = intrVector;
171 MBCS_MMR_SET(mmr, MBCS_WR_DMA_INT_DEST, intr_dest.intr_dest_reg);
175 static inline void mbcs_algo_set(void *mmr,
176 uint64_t amoHostDest,
177 uint64_t amoModType,
178 uint64_t intrHostDest,
179 uint64_t intrVector, uint64_t algoStepCount)
181 union dma_amo_dest amo_dest;
182 union intr_dest intr_dest;
183 union algo_step step;
185 step.algo_step_reg = 0;
186 intr_dest.intr_dest_reg = 0;
187 amo_dest.dma_amo_dest_reg = 0;
189 amo_dest.dma_amo_sys_addr = amoHostDest;
190 amo_dest.dma_amo_mod_type = amoModType;
191 MBCS_MMR_SET(mmr, MBCS_ALG_AMO_DEST, amo_dest.dma_amo_dest_reg);
193 intr_dest.address = intrHostDest;
194 intr_dest.int_vector = intrVector;
195 MBCS_MMR_SET(mmr, MBCS_ALG_INT_DEST, intr_dest.intr_dest_reg);
197 step.alg_step_cnt = algoStepCount;
198 MBCS_MMR_SET(mmr, MBCS_ALG_STEP, step.algo_step_reg);
201 static inline int mbcs_getdma_start(struct mbcs_soft *soft)
203 void *mmr_base;
204 struct getdma *gdma;
205 uint64_t numPkts;
206 union cm_control cm_control;
208 mmr_base = soft->mmr_base;
209 gdma = &soft->getdma;
211 /* check that host address got setup */
212 if (!gdma->hostAddr)
213 return -1;
215 numPkts =
216 (gdma->bytes + (MBCS_CACHELINE_SIZE - 1)) / MBCS_CACHELINE_SIZE;
218 /* program engine */
219 mbcs_getdma_set(mmr_base, tiocx_dma_addr(gdma->hostAddr),
220 gdma->localAddr,
221 (gdma->localAddr < MB2) ? 0 :
222 (gdma->localAddr < MB4) ? 1 :
223 (gdma->localAddr < MB6) ? 2 : 3,
224 numPkts,
225 gdma->DoneAmoEnable,
226 gdma->DoneIntEnable,
227 gdma->peerIO,
228 gdma->amoHostDest,
229 gdma->amoModType,
230 gdma->intrHostDest, gdma->intrVector);
232 /* start engine */
233 cm_control.cm_control_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
234 cm_control.rd_dma_go = 1;
235 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL, cm_control.cm_control_reg);
237 return 0;
241 static inline int mbcs_putdma_start(struct mbcs_soft *soft)
243 void *mmr_base;
244 struct putdma *pdma;
245 uint64_t numPkts;
246 union cm_control cm_control;
248 mmr_base = soft->mmr_base;
249 pdma = &soft->putdma;
251 /* check that host address got setup */
252 if (!pdma->hostAddr)
253 return -1;
255 numPkts =
256 (pdma->bytes + (MBCS_CACHELINE_SIZE - 1)) / MBCS_CACHELINE_SIZE;
258 /* program engine */
259 mbcs_putdma_set(mmr_base, tiocx_dma_addr(pdma->hostAddr),
260 pdma->localAddr,
261 (pdma->localAddr < MB2) ? 0 :
262 (pdma->localAddr < MB4) ? 1 :
263 (pdma->localAddr < MB6) ? 2 : 3,
264 numPkts,
265 pdma->DoneAmoEnable,
266 pdma->DoneIntEnable,
267 pdma->peerIO,
268 pdma->amoHostDest,
269 pdma->amoModType,
270 pdma->intrHostDest, pdma->intrVector);
272 /* start engine */
273 cm_control.cm_control_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
274 cm_control.wr_dma_go = 1;
275 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL, cm_control.cm_control_reg);
277 return 0;
281 static inline int mbcs_algo_start(struct mbcs_soft *soft)
283 struct algoblock *algo_soft = &soft->algo;
284 void *mmr_base = soft->mmr_base;
285 union cm_control cm_control;
287 if (mutex_lock_interruptible(&soft->algolock))
288 return -ERESTARTSYS;
290 atomic_set(&soft->algo_done, 0);
292 mbcs_algo_set(mmr_base,
293 algo_soft->amoHostDest,
294 algo_soft->amoModType,
295 algo_soft->intrHostDest,
296 algo_soft->intrVector, algo_soft->algoStepCount);
298 /* start algorithm */
299 cm_control.cm_control_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
300 cm_control.alg_done_int_en = 1;
301 cm_control.alg_go = 1;
302 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL, cm_control.cm_control_reg);
304 mutex_unlock(&soft->algolock);
306 return 0;
309 static inline ssize_t
310 do_mbcs_sram_dmawrite(struct mbcs_soft *soft, uint64_t hostAddr,
311 size_t len, loff_t * off)
313 int rv = 0;
315 if (mutex_lock_interruptible(&soft->dmawritelock))
316 return -ERESTARTSYS;
318 atomic_set(&soft->dmawrite_done, 0);
320 soft->putdma.hostAddr = hostAddr;
321 soft->putdma.localAddr = *off;
322 soft->putdma.bytes = len;
324 if (mbcs_putdma_start(soft) < 0) {
325 DBG(KERN_ALERT "do_mbcs_sram_dmawrite: "
326 "mbcs_putdma_start failed\n");
327 rv = -EAGAIN;
328 goto dmawrite_exit;
331 if (wait_event_interruptible(soft->dmawrite_queue,
332 atomic_read(&soft->dmawrite_done))) {
333 rv = -ERESTARTSYS;
334 goto dmawrite_exit;
337 rv = len;
338 *off += len;
340 dmawrite_exit:
341 mutex_unlock(&soft->dmawritelock);
343 return rv;
346 static inline ssize_t
347 do_mbcs_sram_dmaread(struct mbcs_soft *soft, uint64_t hostAddr,
348 size_t len, loff_t * off)
350 int rv = 0;
352 if (mutex_lock_interruptible(&soft->dmareadlock))
353 return -ERESTARTSYS;
355 atomic_set(&soft->dmawrite_done, 0);
357 soft->getdma.hostAddr = hostAddr;
358 soft->getdma.localAddr = *off;
359 soft->getdma.bytes = len;
361 if (mbcs_getdma_start(soft) < 0) {
362 DBG(KERN_ALERT "mbcs_strategy: mbcs_getdma_start failed\n");
363 rv = -EAGAIN;
364 goto dmaread_exit;
367 if (wait_event_interruptible(soft->dmaread_queue,
368 atomic_read(&soft->dmaread_done))) {
369 rv = -ERESTARTSYS;
370 goto dmaread_exit;
373 rv = len;
374 *off += len;
376 dmaread_exit:
377 mutex_unlock(&soft->dmareadlock);
379 return rv;
382 static int mbcs_open(struct inode *ip, struct file *fp)
384 struct mbcs_soft *soft;
385 int minor;
387 lock_kernel();
388 minor = iminor(ip);
390 /* Nothing protects access to this list... */
391 list_for_each_entry(soft, &soft_list, list) {
392 if (soft->nasid == minor) {
393 fp->private_data = soft->cxdev;
394 unlock_kernel();
395 return 0;
399 unlock_kernel();
400 return -ENODEV;
403 static ssize_t mbcs_sram_read(struct file * fp, char __user *buf, size_t len, loff_t * off)
405 struct cx_dev *cx_dev = fp->private_data;
406 struct mbcs_soft *soft = cx_dev->soft;
407 uint64_t hostAddr;
408 int rv = 0;
410 hostAddr = __get_dma_pages(GFP_KERNEL, get_order(len));
411 if (hostAddr == 0)
412 return -ENOMEM;
414 rv = do_mbcs_sram_dmawrite(soft, hostAddr, len, off);
415 if (rv < 0)
416 goto exit;
418 if (copy_to_user(buf, (void *)hostAddr, len))
419 rv = -EFAULT;
421 exit:
422 free_pages(hostAddr, get_order(len));
424 return rv;
427 static ssize_t
428 mbcs_sram_write(struct file * fp, const char __user *buf, size_t len, loff_t * off)
430 struct cx_dev *cx_dev = fp->private_data;
431 struct mbcs_soft *soft = cx_dev->soft;
432 uint64_t hostAddr;
433 int rv = 0;
435 hostAddr = __get_dma_pages(GFP_KERNEL, get_order(len));
436 if (hostAddr == 0)
437 return -ENOMEM;
439 if (copy_from_user((void *)hostAddr, buf, len)) {
440 rv = -EFAULT;
441 goto exit;
444 rv = do_mbcs_sram_dmaread(soft, hostAddr, len, off);
446 exit:
447 free_pages(hostAddr, get_order(len));
449 return rv;
452 static loff_t mbcs_sram_llseek(struct file * filp, loff_t off, int whence)
454 loff_t newpos;
456 switch (whence) {
457 case SEEK_SET:
458 newpos = off;
459 break;
461 case SEEK_CUR:
462 newpos = filp->f_pos + off;
463 break;
465 case SEEK_END:
466 newpos = MBCS_SRAM_SIZE + off;
467 break;
469 default: /* can't happen */
470 return -EINVAL;
473 if (newpos < 0)
474 return -EINVAL;
476 filp->f_pos = newpos;
478 return newpos;
481 static uint64_t mbcs_pioaddr(struct mbcs_soft *soft, uint64_t offset)
483 uint64_t mmr_base;
485 mmr_base = (uint64_t) (soft->mmr_base + offset);
487 return mmr_base;
490 static void mbcs_debug_pioaddr_set(struct mbcs_soft *soft)
492 soft->debug_addr = mbcs_pioaddr(soft, MBCS_DEBUG_START);
495 static void mbcs_gscr_pioaddr_set(struct mbcs_soft *soft)
497 soft->gscr_addr = mbcs_pioaddr(soft, MBCS_GSCR_START);
500 static int mbcs_gscr_mmap(struct file *fp, struct vm_area_struct *vma)
502 struct cx_dev *cx_dev = fp->private_data;
503 struct mbcs_soft *soft = cx_dev->soft;
505 if (vma->vm_pgoff != 0)
506 return -EINVAL;
508 vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
510 /* Remap-pfn-range will mark the range VM_IO and VM_RESERVED */
511 if (remap_pfn_range(vma,
512 vma->vm_start,
513 __pa(soft->gscr_addr) >> PAGE_SHIFT,
514 PAGE_SIZE,
515 vma->vm_page_prot))
516 return -EAGAIN;
518 return 0;
522 * mbcs_completion_intr_handler - Primary completion handler.
523 * @irq: irq
524 * @arg: soft struct for device
527 static irqreturn_t
528 mbcs_completion_intr_handler(int irq, void *arg)
530 struct mbcs_soft *soft = (struct mbcs_soft *)arg;
531 void *mmr_base;
532 union cm_status cm_status;
533 union cm_control cm_control;
535 mmr_base = soft->mmr_base;
536 cm_status.cm_status_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_STATUS);
538 if (cm_status.rd_dma_done) {
539 /* stop dma-read engine, clear status */
540 cm_control.cm_control_reg =
541 MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
542 cm_control.rd_dma_clr = 1;
543 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL,
544 cm_control.cm_control_reg);
545 atomic_set(&soft->dmaread_done, 1);
546 wake_up(&soft->dmaread_queue);
548 if (cm_status.wr_dma_done) {
549 /* stop dma-write engine, clear status */
550 cm_control.cm_control_reg =
551 MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
552 cm_control.wr_dma_clr = 1;
553 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL,
554 cm_control.cm_control_reg);
555 atomic_set(&soft->dmawrite_done, 1);
556 wake_up(&soft->dmawrite_queue);
558 if (cm_status.alg_done) {
559 /* clear status */
560 cm_control.cm_control_reg =
561 MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
562 cm_control.alg_done_clr = 1;
563 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL,
564 cm_control.cm_control_reg);
565 atomic_set(&soft->algo_done, 1);
566 wake_up(&soft->algo_queue);
569 return IRQ_HANDLED;
573 * mbcs_intr_alloc - Allocate interrupts.
574 * @dev: device pointer
577 static int mbcs_intr_alloc(struct cx_dev *dev)
579 struct sn_irq_info *sn_irq;
580 struct mbcs_soft *soft;
581 struct getdma *getdma;
582 struct putdma *putdma;
583 struct algoblock *algo;
585 soft = dev->soft;
586 getdma = &soft->getdma;
587 putdma = &soft->putdma;
588 algo = &soft->algo;
590 soft->get_sn_irq = NULL;
591 soft->put_sn_irq = NULL;
592 soft->algo_sn_irq = NULL;
594 sn_irq = tiocx_irq_alloc(dev->cx_id.nasid, TIOCX_CORELET, -1, -1, -1);
595 if (sn_irq == NULL)
596 return -EAGAIN;
597 soft->get_sn_irq = sn_irq;
598 getdma->intrHostDest = sn_irq->irq_xtalkaddr;
599 getdma->intrVector = sn_irq->irq_irq;
600 if (request_irq(sn_irq->irq_irq,
601 (void *)mbcs_completion_intr_handler, IRQF_SHARED,
602 "MBCS get intr", (void *)soft)) {
603 tiocx_irq_free(soft->get_sn_irq);
604 return -EAGAIN;
607 sn_irq = tiocx_irq_alloc(dev->cx_id.nasid, TIOCX_CORELET, -1, -1, -1);
608 if (sn_irq == NULL) {
609 free_irq(soft->get_sn_irq->irq_irq, soft);
610 tiocx_irq_free(soft->get_sn_irq);
611 return -EAGAIN;
613 soft->put_sn_irq = sn_irq;
614 putdma->intrHostDest = sn_irq->irq_xtalkaddr;
615 putdma->intrVector = sn_irq->irq_irq;
616 if (request_irq(sn_irq->irq_irq,
617 (void *)mbcs_completion_intr_handler, IRQF_SHARED,
618 "MBCS put intr", (void *)soft)) {
619 tiocx_irq_free(soft->put_sn_irq);
620 free_irq(soft->get_sn_irq->irq_irq, soft);
621 tiocx_irq_free(soft->get_sn_irq);
622 return -EAGAIN;
625 sn_irq = tiocx_irq_alloc(dev->cx_id.nasid, TIOCX_CORELET, -1, -1, -1);
626 if (sn_irq == NULL) {
627 free_irq(soft->put_sn_irq->irq_irq, soft);
628 tiocx_irq_free(soft->put_sn_irq);
629 free_irq(soft->get_sn_irq->irq_irq, soft);
630 tiocx_irq_free(soft->get_sn_irq);
631 return -EAGAIN;
633 soft->algo_sn_irq = sn_irq;
634 algo->intrHostDest = sn_irq->irq_xtalkaddr;
635 algo->intrVector = sn_irq->irq_irq;
636 if (request_irq(sn_irq->irq_irq,
637 (void *)mbcs_completion_intr_handler, IRQF_SHARED,
638 "MBCS algo intr", (void *)soft)) {
639 tiocx_irq_free(soft->algo_sn_irq);
640 free_irq(soft->put_sn_irq->irq_irq, soft);
641 tiocx_irq_free(soft->put_sn_irq);
642 free_irq(soft->get_sn_irq->irq_irq, soft);
643 tiocx_irq_free(soft->get_sn_irq);
644 return -EAGAIN;
647 return 0;
651 * mbcs_intr_dealloc - Remove interrupts.
652 * @dev: device pointer
655 static void mbcs_intr_dealloc(struct cx_dev *dev)
657 struct mbcs_soft *soft;
659 soft = dev->soft;
661 free_irq(soft->get_sn_irq->irq_irq, soft);
662 tiocx_irq_free(soft->get_sn_irq);
663 free_irq(soft->put_sn_irq->irq_irq, soft);
664 tiocx_irq_free(soft->put_sn_irq);
665 free_irq(soft->algo_sn_irq->irq_irq, soft);
666 tiocx_irq_free(soft->algo_sn_irq);
669 static inline int mbcs_hw_init(struct mbcs_soft *soft)
671 void *mmr_base = soft->mmr_base;
672 union cm_control cm_control;
673 union cm_req_timeout cm_req_timeout;
674 uint64_t err_stat;
676 cm_req_timeout.cm_req_timeout_reg =
677 MBCS_MMR_GET(mmr_base, MBCS_CM_REQ_TOUT);
679 cm_req_timeout.time_out = MBCS_CM_CONTROL_REQ_TOUT_MASK;
680 MBCS_MMR_SET(mmr_base, MBCS_CM_REQ_TOUT,
681 cm_req_timeout.cm_req_timeout_reg);
683 mbcs_gscr_pioaddr_set(soft);
684 mbcs_debug_pioaddr_set(soft);
686 /* clear errors */
687 err_stat = MBCS_MMR_GET(mmr_base, MBCS_CM_ERR_STAT);
688 MBCS_MMR_SET(mmr_base, MBCS_CM_CLR_ERR_STAT, err_stat);
689 MBCS_MMR_ZERO(mmr_base, MBCS_CM_ERROR_DETAIL1);
691 /* enable interrupts */
692 /* turn off 2^23 (INT_EN_PIO_REQ_ADDR_INV) */
693 MBCS_MMR_SET(mmr_base, MBCS_CM_ERR_INT_EN, 0x3ffffff7e00ffUL);
695 /* arm status regs and clear engines */
696 cm_control.cm_control_reg = MBCS_MMR_GET(mmr_base, MBCS_CM_CONTROL);
697 cm_control.rearm_stat_regs = 1;
698 cm_control.alg_clr = 1;
699 cm_control.wr_dma_clr = 1;
700 cm_control.rd_dma_clr = 1;
702 MBCS_MMR_SET(mmr_base, MBCS_CM_CONTROL, cm_control.cm_control_reg);
704 return 0;
707 static ssize_t show_algo(struct device *dev, struct device_attribute *attr, char *buf)
709 struct cx_dev *cx_dev = to_cx_dev(dev);
710 struct mbcs_soft *soft = cx_dev->soft;
711 uint64_t debug0;
714 * By convention, the first debug register contains the
715 * algorithm number and revision.
717 debug0 = *(uint64_t *) soft->debug_addr;
719 return sprintf(buf, "0x%x 0x%x\n",
720 upper_32_bits(debug0), lower_32_bits(debug0));
723 static ssize_t store_algo(struct device *dev, struct device_attribute *attr, const char *buf, size_t count)
725 int n;
726 struct cx_dev *cx_dev = to_cx_dev(dev);
727 struct mbcs_soft *soft = cx_dev->soft;
729 if (count <= 0)
730 return 0;
732 n = simple_strtoul(buf, NULL, 0);
734 if (n == 1) {
735 mbcs_algo_start(soft);
736 if (wait_event_interruptible(soft->algo_queue,
737 atomic_read(&soft->algo_done)))
738 return -ERESTARTSYS;
741 return count;
744 DEVICE_ATTR(algo, 0644, show_algo, store_algo);
747 * mbcs_probe - Initialize for device
748 * @dev: device pointer
749 * @device_id: id table pointer
752 static int mbcs_probe(struct cx_dev *dev, const struct cx_device_id *id)
754 struct mbcs_soft *soft;
756 dev->soft = NULL;
758 soft = kzalloc(sizeof(struct mbcs_soft), GFP_KERNEL);
759 if (soft == NULL)
760 return -ENOMEM;
762 soft->nasid = dev->cx_id.nasid;
763 list_add(&soft->list, &soft_list);
764 soft->mmr_base = (void *)tiocx_swin_base(dev->cx_id.nasid);
765 dev->soft = soft;
766 soft->cxdev = dev;
768 init_waitqueue_head(&soft->dmawrite_queue);
769 init_waitqueue_head(&soft->dmaread_queue);
770 init_waitqueue_head(&soft->algo_queue);
772 mutex_init(&soft->dmawritelock);
773 mutex_init(&soft->dmareadlock);
774 mutex_init(&soft->algolock);
776 mbcs_getdma_init(&soft->getdma);
777 mbcs_putdma_init(&soft->putdma);
778 mbcs_algo_init(&soft->algo);
780 mbcs_hw_init(soft);
782 /* Allocate interrupts */
783 mbcs_intr_alloc(dev);
785 device_create_file(&dev->dev, &dev_attr_algo);
787 return 0;
790 static int mbcs_remove(struct cx_dev *dev)
792 if (dev->soft) {
793 mbcs_intr_dealloc(dev);
794 kfree(dev->soft);
797 device_remove_file(&dev->dev, &dev_attr_algo);
799 return 0;
802 static const struct cx_device_id __devinitdata mbcs_id_table[] = {
804 .part_num = MBCS_PART_NUM,
805 .mfg_num = MBCS_MFG_NUM,
808 .part_num = MBCS_PART_NUM_ALG0,
809 .mfg_num = MBCS_MFG_NUM,
811 {0, 0}
814 MODULE_DEVICE_TABLE(cx, mbcs_id_table);
816 static struct cx_drv mbcs_driver = {
817 .name = DEVICE_NAME,
818 .id_table = mbcs_id_table,
819 .probe = mbcs_probe,
820 .remove = mbcs_remove,
823 static void __exit mbcs_exit(void)
825 unregister_chrdev(mbcs_major, DEVICE_NAME);
826 cx_driver_unregister(&mbcs_driver);
829 static int __init mbcs_init(void)
831 int rv;
833 if (!ia64_platform_is("sn2"))
834 return -ENODEV;
836 // Put driver into chrdevs[]. Get major number.
837 rv = register_chrdev(mbcs_major, DEVICE_NAME, &mbcs_ops);
838 if (rv < 0) {
839 DBG(KERN_ALERT "mbcs_init: can't get major number. %d\n", rv);
840 return rv;
842 mbcs_major = rv;
844 return cx_driver_register(&mbcs_driver);
847 module_init(mbcs_init);
848 module_exit(mbcs_exit);
850 MODULE_AUTHOR("Bruce Losure <blosure@sgi.com>");
851 MODULE_DESCRIPTION("Driver for MOATB Core Services");
852 MODULE_LICENSE("GPL");