3 * Linux MegaRAID device driver
5 * Copyright (c) 2003-2004 LSI Logic Corporation.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version
10 * 2 of the License, or (at your option) any later version.
12 * FILE : megaraid_mm.c
13 * Version : v2.20.2.7 (Jul 16 2006)
15 * Common management module
17 #include <linux/sched.h>
18 #include <linux/slab.h>
19 #include <linux/mutex.h>
20 #include "megaraid_mm.h"
23 // Entry points for char node driver
24 static DEFINE_MUTEX(mraid_mm_mutex
);
25 static int mraid_mm_open(struct inode
*, struct file
*);
26 static long mraid_mm_unlocked_ioctl(struct file
*, uint
, unsigned long);
29 // routines to convert to and from the old the format
30 static int mimd_to_kioc(mimd_t __user
*, mraid_mmadp_t
*, uioc_t
*);
31 static int kioc_to_mimd(uioc_t
*, mimd_t __user
*);
35 static int handle_drvrcmd(void __user
*, uint8_t, int *);
36 static int lld_ioctl(mraid_mmadp_t
*, uioc_t
*);
37 static void ioctl_done(uioc_t
*);
38 static void lld_timedout(unsigned long);
39 static void hinfo_to_cinfo(mraid_hba_info_t
*, mcontroller_t
*);
40 static mraid_mmadp_t
*mraid_mm_get_adapter(mimd_t __user
*, int *);
41 static uioc_t
*mraid_mm_alloc_kioc(mraid_mmadp_t
*);
42 static void mraid_mm_dealloc_kioc(mraid_mmadp_t
*, uioc_t
*);
43 static int mraid_mm_attach_buf(mraid_mmadp_t
*, uioc_t
*, int);
44 static int mraid_mm_setup_dma_pools(mraid_mmadp_t
*);
45 static void mraid_mm_free_adp_resources(mraid_mmadp_t
*);
46 static void mraid_mm_teardown_dma_pools(mraid_mmadp_t
*);
49 static long mraid_mm_compat_ioctl(struct file
*, unsigned int, unsigned long);
52 MODULE_AUTHOR("LSI Logic Corporation");
53 MODULE_DESCRIPTION("LSI Logic Management Module");
54 MODULE_LICENSE("GPL");
55 MODULE_VERSION(LSI_COMMON_MOD_VERSION
);
57 static int dbglevel
= CL_ANN
;
58 module_param_named(dlevel
, dbglevel
, int, 0);
59 MODULE_PARM_DESC(dlevel
, "Debug level (default=0)");
61 EXPORT_SYMBOL(mraid_mm_register_adp
);
62 EXPORT_SYMBOL(mraid_mm_unregister_adp
);
63 EXPORT_SYMBOL(mraid_mm_adapter_app_handle
);
65 static uint32_t drvr_ver
= 0x02200207;
67 static int adapters_count_g
;
68 static struct list_head adapters_list_g
;
70 static wait_queue_head_t wait_q
;
72 static const struct file_operations lsi_fops
= {
73 .open
= mraid_mm_open
,
74 .unlocked_ioctl
= mraid_mm_unlocked_ioctl
,
76 .compat_ioctl
= mraid_mm_compat_ioctl
,
81 static struct miscdevice megaraid_mm_dev
= {
82 .minor
= MISC_DYNAMIC_MINOR
,
88 * mraid_mm_open - open routine for char node interface
92 * Allow ioctl operations by apps only if they have superuser privilege.
95 mraid_mm_open(struct inode
*inode
, struct file
*filep
)
98 * Only allow superuser to access private ioctl interface
100 if (!capable(CAP_SYS_ADMIN
)) return (-EACCES
);
106 * mraid_mm_ioctl - module entry-point for ioctls
107 * @inode : inode (ignored)
108 * @filep : file operations pointer (ignored)
109 * @cmd : ioctl command
110 * @arg : user ioctl packet
113 mraid_mm_ioctl(struct file
*filep
, unsigned int cmd
, unsigned long arg
)
116 char signature
[EXT_IOCTL_SIGN_SZ
] = {0};
121 void __user
*argp
= (void __user
*)arg
;
124 * Make sure only USCSICMD are issued through this interface.
125 * MIMD application would still fire different command.
128 if ((_IOC_TYPE(cmd
) != MEGAIOC_MAGIC
) && (cmd
!= USCSICMD
)) {
133 * Look for signature to see if this is the new or old ioctl format.
135 if (copy_from_user(signature
, argp
, EXT_IOCTL_SIGN_SZ
)) {
136 con_log(CL_ANN
, (KERN_WARNING
137 "megaraid cmm: copy from usr addr failed\n"));
141 if (memcmp(signature
, EXT_IOCTL_SIGN
, EXT_IOCTL_SIGN_SZ
) == 0)
147 * At present, we don't support the new ioctl packet
153 * If it is a driver ioctl (as opposed to fw ioctls), then we can
154 * handle the command locally. rval > 0 means it is not a drvr cmd
156 rval
= handle_drvrcmd(argp
, old_ioctl
, &drvrcmd_rval
);
164 if ((adp
= mraid_mm_get_adapter(argp
, &rval
)) == NULL
) {
169 * Check if adapter can accept ioctl. We may have marked it offline
170 * if any previous kioc had timedout on this controller.
172 if (!adp
->quiescent
) {
173 con_log(CL_ANN
, (KERN_WARNING
174 "megaraid cmm: controller cannot accept cmds due to "
175 "earlier errors\n" ));
180 * The following call will block till a kioc is available
182 kioc
= mraid_mm_alloc_kioc(adp
);
185 * User sent the old mimd_t ioctl packet. Convert it to uioc_t.
187 if ((rval
= mimd_to_kioc(argp
, adp
, kioc
))) {
188 mraid_mm_dealloc_kioc(adp
, kioc
);
192 kioc
->done
= ioctl_done
;
195 * Issue the IOCTL to the low level driver. After the IOCTL completes
196 * release the kioc if and only if it was _not_ timedout. If it was
197 * timedout, that means that resources are still with low level driver.
199 if ((rval
= lld_ioctl(adp
, kioc
))) {
202 mraid_mm_dealloc_kioc(adp
, kioc
);
208 * Convert the kioc back to user space
210 rval
= kioc_to_mimd(kioc
, argp
);
213 * Return the kioc to free pool
215 mraid_mm_dealloc_kioc(adp
, kioc
);
221 mraid_mm_unlocked_ioctl(struct file
*filep
, unsigned int cmd
,
226 /* inconsistant: mraid_mm_compat_ioctl doesn't take the BKL */
227 mutex_lock(&mraid_mm_mutex
);
228 err
= mraid_mm_ioctl(filep
, cmd
, arg
);
229 mutex_unlock(&mraid_mm_mutex
);
235 * mraid_mm_get_adapter - Returns corresponding adapters for the mimd packet
236 * @umimd : User space mimd_t ioctl packet
237 * @rval : returned success/error status
239 * The function return value is a pointer to the located @adapter.
241 static mraid_mmadp_t
*
242 mraid_mm_get_adapter(mimd_t __user
*umimd
, int *rval
)
244 mraid_mmadp_t
*adapter
;
250 if (copy_from_user(&mimd
, umimd
, sizeof(mimd_t
))) {
255 adapno
= GETADAP(mimd
.ui
.fcs
.adapno
);
257 if (adapno
>= adapters_count_g
) {
265 list_for_each_entry(adapter
, &adapters_list_g
, list
) {
266 if (iterator
++ == adapno
) break;
278 * handle_drvrcmd - Checks if the opcode is a driver cmd and if it is, handles it.
279 * @arg : packet sent by the user app
280 * @old_ioctl : mimd if 1; uioc otherwise
281 * @rval : pointer for command's returned value (not function status)
284 handle_drvrcmd(void __user
*arg
, uint8_t old_ioctl
, int *rval
)
286 mimd_t __user
*umimd
;
303 if (copy_from_user(&kmimd
, umimd
, sizeof(mimd_t
)))
306 opcode
= kmimd
.ui
.fcs
.opcode
;
307 subopcode
= kmimd
.ui
.fcs
.subopcode
;
310 * If the opcode is 0x82 and the subopcode is either GET_DRVRVER or
311 * GET_NUMADP, then we can handle. Otherwise we should return 1 to
312 * indicate that we cannot handle this.
319 case MEGAIOC_QDRVRVER
:
321 if (copy_to_user(kmimd
.data
, &drvr_ver
, sizeof(uint32_t)))
328 *rval
= adapters_count_g
;
330 if (copy_to_user(kmimd
.data
, &adapters_count_g
,
346 * mimd_to_kioc - Converter from old to new ioctl format
347 * @umimd : user space old MIMD IOCTL
348 * @adp : adapter softstate
349 * @kioc : kernel space new format IOCTL
351 * Routine to convert MIMD interface IOCTL to new interface IOCTL packet. The
352 * new packet is in kernel space so that driver can perform operations on it
357 mimd_to_kioc(mimd_t __user
*umimd
, mraid_mmadp_t
*adp
, uioc_t
*kioc
)
361 mraid_passthru_t
*pthru32
;
367 if (copy_from_user(&mimd
, umimd
, sizeof(mimd_t
)))
371 * Applications are not allowed to send extd pthru
373 if ((mimd
.mbox
[0] == MBOXCMD_PASSTHRU64
) ||
374 (mimd
.mbox
[0] == MBOXCMD_EXTPTHRU
))
377 opcode
= mimd
.ui
.fcs
.opcode
;
378 subopcode
= mimd
.ui
.fcs
.subopcode
;
379 adapno
= GETADAP(mimd
.ui
.fcs
.adapno
);
381 if (adapno
>= adapters_count_g
)
384 kioc
->adapno
= adapno
;
385 kioc
->mb_type
= MBOX_LEGACY
;
386 kioc
->app_type
= APPTYPE_MIMD
;
392 if (subopcode
== MEGAIOC_QADAPINFO
) {
394 kioc
->opcode
= GET_ADAP_INFO
;
395 kioc
->data_dir
= UIOC_RD
;
396 kioc
->xferlen
= sizeof(mraid_hba_info_t
);
398 if (mraid_mm_attach_buf(adp
, kioc
, kioc
->xferlen
))
402 con_log(CL_ANN
, (KERN_WARNING
403 "megaraid cmm: Invalid subop\n"));
411 kioc
->opcode
= MBOX_CMD
;
412 kioc
->xferlen
= mimd
.ui
.fcs
.length
;
413 kioc
->user_data_len
= kioc
->xferlen
;
414 kioc
->user_data
= mimd
.ui
.fcs
.buffer
;
416 if (mraid_mm_attach_buf(adp
, kioc
, kioc
->xferlen
))
419 if (mimd
.outlen
) kioc
->data_dir
= UIOC_RD
;
420 if (mimd
.inlen
) kioc
->data_dir
|= UIOC_WR
;
426 kioc
->opcode
= MBOX_CMD
;
427 kioc
->xferlen
= (mimd
.outlen
> mimd
.inlen
) ?
428 mimd
.outlen
: mimd
.inlen
;
429 kioc
->user_data_len
= kioc
->xferlen
;
430 kioc
->user_data
= mimd
.data
;
432 if (mraid_mm_attach_buf(adp
, kioc
, kioc
->xferlen
))
435 if (mimd
.outlen
) kioc
->data_dir
= UIOC_RD
;
436 if (mimd
.inlen
) kioc
->data_dir
|= UIOC_WR
;
445 * If driver command, nothing else to do
451 * This is a mailbox cmd; copy the mailbox from mimd
453 mbox64
= (mbox64_t
*)((unsigned long)kioc
->cmdbuf
);
454 mbox
= &mbox64
->mbox32
;
455 memcpy(mbox
, mimd
.mbox
, 14);
457 if (mbox
->cmd
!= MBOXCMD_PASSTHRU
) { // regular DCMD
459 mbox
->xferaddr
= (uint32_t)kioc
->buf_paddr
;
461 if (kioc
->data_dir
& UIOC_WR
) {
462 if (copy_from_user(kioc
->buf_vaddr
, kioc
->user_data
,
472 * This is a regular 32-bit pthru cmd; mbox points to pthru struct.
473 * Just like in above case, the beginning for memblk is treated as
474 * a mailbox. The passthru will begin at next 1K boundary. And the
475 * data will start 1K after that.
477 pthru32
= kioc
->pthru32
;
478 kioc
->user_pthru
= &umimd
->pthru
;
479 mbox
->xferaddr
= (uint32_t)kioc
->pthru32_h
;
481 if (copy_from_user(pthru32
, kioc
->user_pthru
,
482 sizeof(mraid_passthru_t
))) {
486 pthru32
->dataxferaddr
= kioc
->buf_paddr
;
487 if (kioc
->data_dir
& UIOC_WR
) {
488 if (copy_from_user(kioc
->buf_vaddr
, kioc
->user_data
,
489 pthru32
->dataxferlen
)) {
498 * mraid_mm_attch_buf - Attach a free dma buffer for required size
499 * @adp : Adapter softstate
500 * @kioc : kioc that the buffer needs to be attached to
501 * @xferlen : required length for buffer
503 * First we search for a pool with smallest buffer that is >= @xferlen. If
504 * that pool has no free buffer, we will try for the next bigger size. If none
505 * is available, we will try to allocate the smallest buffer that is >=
506 * @xferlen and attach it the pool.
509 mraid_mm_attach_buf(mraid_mmadp_t
*adp
, uioc_t
*kioc
, int xferlen
)
516 kioc
->pool_index
= -1;
517 kioc
->buf_vaddr
= NULL
;
522 * We need xferlen amount of memory. See if we can get it from our
523 * dma pools. If we don't get exact size, we will try bigger buffer
526 for (i
= 0; i
< MAX_DMA_POOLS
; i
++) {
528 pool
= &adp
->dma_pool_list
[i
];
530 if (xferlen
> pool
->buf_size
)
533 if (right_pool
== -1)
536 spin_lock_irqsave(&pool
->lock
, flags
);
541 kioc
->pool_index
= i
;
542 kioc
->buf_vaddr
= pool
->vaddr
;
543 kioc
->buf_paddr
= pool
->paddr
;
545 spin_unlock_irqrestore(&pool
->lock
, flags
);
549 spin_unlock_irqrestore(&pool
->lock
, flags
);
555 * If xferlen doesn't match any of our pools, return error
557 if (right_pool
== -1)
561 * We did not get any buffer from the preallocated pool. Let us try
562 * to allocate one new buffer. NOTE: This is a blocking call.
564 pool
= &adp
->dma_pool_list
[right_pool
];
566 spin_lock_irqsave(&pool
->lock
, flags
);
568 kioc
->pool_index
= right_pool
;
570 kioc
->buf_vaddr
= pci_pool_alloc(pool
->handle
, GFP_KERNEL
,
572 spin_unlock_irqrestore(&pool
->lock
, flags
);
574 if (!kioc
->buf_vaddr
)
581 * mraid_mm_alloc_kioc - Returns a uioc_t from free list
582 * @adp : Adapter softstate for this module
584 * The kioc_semaphore is initialized with number of kioc nodes in the
585 * free kioc pool. If the kioc pool is empty, this function blocks till
586 * a kioc becomes free.
589 mraid_mm_alloc_kioc(mraid_mmadp_t
*adp
)
592 struct list_head
* head
;
595 down(&adp
->kioc_semaphore
);
597 spin_lock_irqsave(&adp
->kioc_pool_lock
, flags
);
599 head
= &adp
->kioc_pool
;
601 if (list_empty(head
)) {
602 up(&adp
->kioc_semaphore
);
603 spin_unlock_irqrestore(&adp
->kioc_pool_lock
, flags
);
605 con_log(CL_ANN
, ("megaraid cmm: kioc list empty!\n"));
609 kioc
= list_entry(head
->next
, uioc_t
, list
);
610 list_del_init(&kioc
->list
);
612 spin_unlock_irqrestore(&adp
->kioc_pool_lock
, flags
);
614 memset((caddr_t
)(unsigned long)kioc
->cmdbuf
, 0, sizeof(mbox64_t
));
615 memset((caddr_t
) kioc
->pthru32
, 0, sizeof(mraid_passthru_t
));
617 kioc
->buf_vaddr
= NULL
;
619 kioc
->pool_index
=-1;
621 kioc
->user_data
= NULL
;
622 kioc
->user_data_len
= 0;
623 kioc
->user_pthru
= NULL
;
630 * mraid_mm_dealloc_kioc - Return kioc to free pool
631 * @adp : Adapter softstate
632 * @kioc : uioc_t node to be returned to free pool
635 mraid_mm_dealloc_kioc(mraid_mmadp_t
*adp
, uioc_t
*kioc
)
640 if (kioc
->pool_index
!= -1) {
641 pool
= &adp
->dma_pool_list
[kioc
->pool_index
];
643 /* This routine may be called in non-isr context also */
644 spin_lock_irqsave(&pool
->lock
, flags
);
647 * While attaching the dma buffer, if we didn't get the
648 * required buffer from the pool, we would have allocated
649 * it at the run time and set the free_buf flag. We must
650 * free that buffer. Otherwise, just mark that the buffer is
653 if (kioc
->free_buf
== 1)
654 pci_pool_free(pool
->handle
, kioc
->buf_vaddr
,
659 spin_unlock_irqrestore(&pool
->lock
, flags
);
662 /* Return the kioc to the free pool */
663 spin_lock_irqsave(&adp
->kioc_pool_lock
, flags
);
664 list_add(&kioc
->list
, &adp
->kioc_pool
);
665 spin_unlock_irqrestore(&adp
->kioc_pool_lock
, flags
);
667 /* increment the free kioc count */
668 up(&adp
->kioc_semaphore
);
674 * lld_ioctl - Routine to issue ioctl to low level drvr
675 * @adp : The adapter handle
676 * @kioc : The ioctl packet with kernel addresses
679 lld_ioctl(mraid_mmadp_t
*adp
, uioc_t
*kioc
)
682 struct timer_list timer
;
683 struct timer_list
*tp
= NULL
;
685 kioc
->status
= -ENODATA
;
686 rval
= adp
->issue_uioc(adp
->drvr_data
, kioc
, IOCTL_ISSUE
);
688 if (rval
) return rval
;
693 if (adp
->timeout
> 0) {
697 tp
->function
= lld_timedout
;
698 tp
->data
= (unsigned long)kioc
;
699 tp
->expires
= jiffies
+ adp
->timeout
* HZ
;
705 * Wait till the low level driver completes the ioctl. After this
706 * call, the ioctl either completed successfully or timedout.
708 wait_event(wait_q
, (kioc
->status
!= -ENODATA
));
714 * If the command had timedout, we mark the controller offline
717 if (kioc
->timedout
) {
726 * ioctl_done - callback from the low level driver
727 * @kioc : completed ioctl packet
730 ioctl_done(uioc_t
*kioc
)
734 mraid_mmadp_t
* adapter
;
737 * When the kioc returns from driver, make sure it still doesn't
738 * have ENODATA in status. Otherwise, driver will hang on wait_event
741 if (kioc
->status
== -ENODATA
) {
742 con_log(CL_ANN
, (KERN_WARNING
743 "megaraid cmm: lld didn't change status!\n"));
745 kioc
->status
= -EINVAL
;
749 * Check if this kioc was timedout before. If so, nobody is waiting
750 * on this kioc. We don't have to wake up anybody. Instead, we just
751 * have to free the kioc
753 if (kioc
->timedout
) {
756 adapno
= kioc
->adapno
;
758 con_log(CL_ANN
, ( KERN_WARNING
"megaraid cmm: completed "
759 "ioctl that was timedout before\n"));
761 list_for_each_entry(adapter
, &adapters_list_g
, list
) {
762 if (iterator
++ == adapno
) break;
768 mraid_mm_dealloc_kioc( adapter
, kioc
);
778 * lld_timedout - callback from the expired timer
779 * @ptr : ioctl packet that timed out
782 lld_timedout(unsigned long ptr
)
784 uioc_t
*kioc
= (uioc_t
*)ptr
;
786 kioc
->status
= -ETIME
;
789 con_log(CL_ANN
, (KERN_WARNING
"megaraid cmm: ioctl timed out\n"));
796 * kioc_to_mimd - Converter from new back to old format
797 * @kioc : Kernel space IOCTL packet (successfully issued)
798 * @mimd : User space MIMD packet
801 kioc_to_mimd(uioc_t
*kioc
, mimd_t __user
*mimd
)
808 mraid_passthru_t __user
*upthru32
;
809 mraid_passthru_t
*kpthru32
;
811 mraid_hba_info_t
*hinfo
;
814 if (copy_from_user(&kmimd
, mimd
, sizeof(mimd_t
)))
817 opcode
= kmimd
.ui
.fcs
.opcode
;
818 subopcode
= kmimd
.ui
.fcs
.subopcode
;
820 if (opcode
== 0x82) {
823 case MEGAIOC_QADAPINFO
:
825 hinfo
= (mraid_hba_info_t
*)(unsigned long)
828 hinfo_to_cinfo(hinfo
, &cinfo
);
830 if (copy_to_user(kmimd
.data
, &cinfo
, sizeof(cinfo
)))
842 mbox64
= (mbox64_t
*)(unsigned long)kioc
->cmdbuf
;
844 if (kioc
->user_pthru
) {
846 upthru32
= kioc
->user_pthru
;
847 kpthru32
= kioc
->pthru32
;
849 if (copy_to_user(&upthru32
->scsistatus
,
850 &kpthru32
->scsistatus
,
856 if (kioc
->user_data
) {
857 if (copy_to_user(kioc
->user_data
, kioc
->buf_vaddr
,
858 kioc
->user_data_len
)) {
863 if (copy_to_user(&mimd
->mbox
[17],
864 &mbox64
->mbox32
.status
, sizeof(uint8_t))) {
873 * hinfo_to_cinfo - Convert new format hba info into old format
874 * @hinfo : New format, more comprehensive adapter info
875 * @cinfo : Old format adapter info to support mimd_t apps
878 hinfo_to_cinfo(mraid_hba_info_t
*hinfo
, mcontroller_t
*cinfo
)
880 if (!hinfo
|| !cinfo
)
883 cinfo
->base
= hinfo
->baseport
;
884 cinfo
->irq
= hinfo
->irq
;
885 cinfo
->numldrv
= hinfo
->num_ldrv
;
886 cinfo
->pcibus
= hinfo
->pci_bus
;
887 cinfo
->pcidev
= hinfo
->pci_slot
;
888 cinfo
->pcifun
= PCI_FUNC(hinfo
->pci_dev_fn
);
889 cinfo
->pciid
= hinfo
->pci_device_id
;
890 cinfo
->pcivendor
= hinfo
->pci_vendor_id
;
891 cinfo
->pcislot
= hinfo
->pci_slot
;
892 cinfo
->uid
= hinfo
->unique_id
;
897 * mraid_mm_register_adp - Registration routine for low level drivers
898 * @lld_adp : Adapter objejct
901 mraid_mm_register_adp(mraid_mmadp_t
*lld_adp
)
903 mraid_mmadp_t
*adapter
;
910 if (lld_adp
->drvr_type
!= DRVRTYPE_MBOX
)
913 adapter
= kzalloc(sizeof(mraid_mmadp_t
), GFP_KERNEL
);
919 adapter
->unique_id
= lld_adp
->unique_id
;
920 adapter
->drvr_type
= lld_adp
->drvr_type
;
921 adapter
->drvr_data
= lld_adp
->drvr_data
;
922 adapter
->pdev
= lld_adp
->pdev
;
923 adapter
->issue_uioc
= lld_adp
->issue_uioc
;
924 adapter
->timeout
= lld_adp
->timeout
;
925 adapter
->max_kioc
= lld_adp
->max_kioc
;
926 adapter
->quiescent
= 1;
929 * Allocate single blocks of memory for all required kiocs,
930 * mailboxes and passthru structures.
932 adapter
->kioc_list
= kmalloc(sizeof(uioc_t
) * lld_adp
->max_kioc
,
934 adapter
->mbox_list
= kmalloc(sizeof(mbox64_t
) * lld_adp
->max_kioc
,
936 adapter
->pthru_dma_pool
= pci_pool_create("megaraid mm pthru pool",
938 sizeof(mraid_passthru_t
),
941 if (!adapter
->kioc_list
|| !adapter
->mbox_list
||
942 !adapter
->pthru_dma_pool
) {
944 con_log(CL_ANN
, (KERN_WARNING
945 "megaraid cmm: out of memory, %s %d\n", __func__
,
954 * Slice kioc_list and make a kioc_pool with the individiual kiocs
956 INIT_LIST_HEAD(&adapter
->kioc_pool
);
957 spin_lock_init(&adapter
->kioc_pool_lock
);
958 sema_init(&adapter
->kioc_semaphore
, lld_adp
->max_kioc
);
960 mbox_list
= (mbox64_t
*)adapter
->mbox_list
;
962 for (i
= 0; i
< lld_adp
->max_kioc
; i
++) {
964 kioc
= adapter
->kioc_list
+ i
;
965 kioc
->cmdbuf
= (uint64_t)(unsigned long)(mbox_list
+ i
);
966 kioc
->pthru32
= pci_pool_alloc(adapter
->pthru_dma_pool
,
967 GFP_KERNEL
, &kioc
->pthru32_h
);
969 if (!kioc
->pthru32
) {
971 con_log(CL_ANN
, (KERN_WARNING
972 "megaraid cmm: out of memory, %s %d\n",
973 __func__
, __LINE__
));
977 goto pthru_dma_pool_error
;
980 list_add_tail(&kioc
->list
, &adapter
->kioc_pool
);
983 // Setup the dma pools for data buffers
984 if ((rval
= mraid_mm_setup_dma_pools(adapter
)) != 0) {
988 list_add_tail(&adapter
->list
, &adapters_list_g
);
997 pthru_dma_pool_error
:
999 for (i
= 0; i
< lld_adp
->max_kioc
; i
++) {
1000 kioc
= adapter
->kioc_list
+ i
;
1001 if (kioc
->pthru32
) {
1002 pci_pool_free(adapter
->pthru_dma_pool
, kioc
->pthru32
,
1009 kfree(adapter
->kioc_list
);
1010 kfree(adapter
->mbox_list
);
1012 if (adapter
->pthru_dma_pool
)
1013 pci_pool_destroy(adapter
->pthru_dma_pool
);
1022 * mraid_mm_adapter_app_handle - return the application handle for this adapter
1023 * @unique_id : adapter unique identifier
1025 * For the given driver data, locate the adapter in our global list and
1026 * return the corresponding handle, which is also used by applications to
1027 * uniquely identify an adapter.
1029 * Return adapter handle if found in the list.
1030 * Return 0 if adapter could not be located, should never happen though.
1033 mraid_mm_adapter_app_handle(uint32_t unique_id
)
1035 mraid_mmadp_t
*adapter
;
1039 list_for_each_entry_safe(adapter
, tmp
, &adapters_list_g
, list
) {
1041 if (adapter
->unique_id
== unique_id
) {
1043 return MKADAP(index
);
1054 * mraid_mm_setup_dma_pools - Set up dma buffer pools per adapter
1055 * @adp : Adapter softstate
1057 * We maintain a pool of dma buffers per each adapter. Each pool has one
1058 * buffer. E.g, we may have 5 dma pools - one each for 4k, 8k ... 64k buffers.
1059 * We have just one 4k buffer in 4k pool, one 8k buffer in 8k pool etc. We
1060 * dont' want to waste too much memory by allocating more buffers per each
1064 mraid_mm_setup_dma_pools(mraid_mmadp_t
*adp
)
1071 * Create MAX_DMA_POOLS number of pools
1073 bufsize
= MRAID_MM_INIT_BUFF_SIZE
;
1075 for (i
= 0; i
< MAX_DMA_POOLS
; i
++){
1077 pool
= &adp
->dma_pool_list
[i
];
1079 pool
->buf_size
= bufsize
;
1080 spin_lock_init(&pool
->lock
);
1082 pool
->handle
= pci_pool_create("megaraid mm data buffer",
1083 adp
->pdev
, bufsize
, 16, 0);
1085 if (!pool
->handle
) {
1086 goto dma_pool_setup_error
;
1089 pool
->vaddr
= pci_pool_alloc(pool
->handle
, GFP_KERNEL
,
1093 goto dma_pool_setup_error
;
1095 bufsize
= bufsize
* 2;
1100 dma_pool_setup_error
:
1102 mraid_mm_teardown_dma_pools(adp
);
1108 * mraid_mm_unregister_adp - Unregister routine for low level drivers
1109 * @unique_id : UID of the adpater
1111 * Assumes no outstanding ioctls to llds.
1114 mraid_mm_unregister_adp(uint32_t unique_id
)
1116 mraid_mmadp_t
*adapter
;
1119 list_for_each_entry_safe(adapter
, tmp
, &adapters_list_g
, list
) {
1122 if (adapter
->unique_id
== unique_id
) {
1126 list_del_init(&adapter
->list
);
1128 mraid_mm_free_adp_resources(adapter
);
1133 "megaraid cmm: Unregistered one adapter:%#x\n",
1144 * mraid_mm_free_adp_resources - Free adapter softstate
1145 * @adp : Adapter softstate
1148 mraid_mm_free_adp_resources(mraid_mmadp_t
*adp
)
1153 mraid_mm_teardown_dma_pools(adp
);
1155 for (i
= 0; i
< adp
->max_kioc
; i
++) {
1157 kioc
= adp
->kioc_list
+ i
;
1159 pci_pool_free(adp
->pthru_dma_pool
, kioc
->pthru32
,
1163 kfree(adp
->kioc_list
);
1164 kfree(adp
->mbox_list
);
1166 pci_pool_destroy(adp
->pthru_dma_pool
);
1174 * mraid_mm_teardown_dma_pools - Free all per adapter dma buffers
1175 * @adp : Adapter softstate
1178 mraid_mm_teardown_dma_pools(mraid_mmadp_t
*adp
)
1183 for (i
= 0; i
< MAX_DMA_POOLS
; i
++) {
1185 pool
= &adp
->dma_pool_list
[i
];
1190 pci_pool_free(pool
->handle
, pool
->vaddr
,
1193 pci_pool_destroy(pool
->handle
);
1194 pool
->handle
= NULL
;
1202 * mraid_mm_init - Module entry point
1209 // Announce the driver version
1210 con_log(CL_ANN
, (KERN_INFO
"megaraid cmm: %s %s\n",
1211 LSI_COMMON_MOD_VERSION
, LSI_COMMON_MOD_EXT_VERSION
));
1213 err
= misc_register(&megaraid_mm_dev
);
1215 con_log(CL_ANN
, ("megaraid cmm: cannot register misc device\n"));
1219 init_waitqueue_head(&wait_q
);
1221 INIT_LIST_HEAD(&adapters_list_g
);
1227 #ifdef CONFIG_COMPAT
1229 * mraid_mm_compat_ioctl - 32bit to 64bit ioctl conversion routine
1230 * @filep : file operations pointer (ignored)
1231 * @cmd : ioctl command
1232 * @arg : user ioctl packet
1235 mraid_mm_compat_ioctl(struct file
*filep
, unsigned int cmd
,
1240 err
= mraid_mm_ioctl(filep
, cmd
, arg
);
1247 * mraid_mm_exit - Module exit point
1252 con_log(CL_DLEVEL1
, ("exiting common mod\n"));
1254 misc_deregister(&megaraid_mm_dev
);
1257 module_init(mraid_mm_init
);
1258 module_exit(mraid_mm_exit
);
1260 /* vi: set ts=8 sw=8 tw=78: */